@genoacms/adapter-gcp 0.3.8 → 0.3.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,9 @@
1
- import type { storage as storageT } from '@genoacms/cloudabstraction';
2
- declare const getObject: storageT.getObject;
3
- declare const uploadObject: storageT.uploadObject;
4
- declare const deleteObject: storageT.deleteObject;
5
- declare const listDirectory: storageT.listDirectory;
6
- declare const createDirectory: storageT.createDirectory;
7
- export { getObject, uploadObject, deleteObject, listDirectory, createDirectory };
1
+ import type { Adapter } from '@genoacms/cloudabstraction/storage';
2
+ declare const getObject: Adapter['getObject'];
3
+ declare const getPublicURL: Adapter['getPublicURL'];
4
+ declare const getSignedURL: Adapter['getSignedURL'];
5
+ declare const uploadObject: Adapter['uploadObject'];
6
+ declare const deleteObject: Adapter['deleteObject'];
7
+ declare const listDirectory: Adapter['listDirectory'];
8
+ declare const createDirectory: Adapter['createDirectory'];
9
+ export { getObject, getPublicURL, getSignedURL, uploadObject, deleteObject, listDirectory, createDirectory };
@@ -16,6 +16,22 @@ const getObject = async ({ bucket, name }) => {
16
16
  data: file.createReadStream()
17
17
  };
18
18
  };
19
+ const getPublicURL = async ({ bucket, name }) => {
20
+ const bucketInstance = getBucket(bucket);
21
+ const file = bucketInstance.file(name);
22
+ return file.publicUrl();
23
+ };
24
+ const getSignedURL = async ({ bucket, name }) => {
25
+ const bucketInstance = getBucket(bucket);
26
+ const file = bucketInstance.file(name);
27
+ const expires = new Date();
28
+ expires.setTime(expires.getTime() + 60 * 60);
29
+ const [url] = await file.getSignedUrl({
30
+ action: 'read',
31
+ expires
32
+ });
33
+ return url;
34
+ };
19
35
  const uploadObject = async ({ bucket, name }, stream) => {
20
36
  const bucketInstance = getBucket(bucket);
21
37
  const file = bucketInstance.file(name);
@@ -28,22 +44,30 @@ const deleteObject = async ({ bucket, name }) => {
28
44
  };
29
45
  const listDirectory = async ({ bucket, name }, listingParams = {}) => {
30
46
  const bucketInstance = getBucket(bucket);
31
- const [files] = await bucketInstance.getFiles({
47
+ const options = {
48
+ autoPaginate: false,
32
49
  prefix: name,
33
50
  maxResults: listingParams?.limit,
34
- startOffset: listingParams?.startAfter
35
- });
36
- return files.map((file) => {
37
- return {
38
- name: file.name,
39
- size: file.metadata.size ? parseInt(file.metadata.size) : 0,
40
- lastModified: new Date(file.metadata.updated)
41
- };
42
- });
51
+ startOffset: listingParams?.startAfter,
52
+ delimiter: '/'
53
+ };
54
+ let [files, , apiResponse] = (await bucketInstance.getFiles(options));
55
+ files = files.filter((file) => !file.name.endsWith('.folderPlaceholder'));
56
+ return {
57
+ files: files.map((file) => {
58
+ return {
59
+ name: file.name,
60
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
61
+ size: file.metadata.size ? parseInt(file.metadata.size) : 0,
62
+ lastModified: new Date(file.metadata.updated)
63
+ };
64
+ }),
65
+ directories: apiResponse?.prefixes ?? []
66
+ };
43
67
  };
44
68
  const createDirectory = async ({ bucket, name }) => {
45
69
  const bucketInstance = getBucket(bucket);
46
70
  const file = bucketInstance.file(`${name}/.folderPlaceholder`);
47
71
  await file.save('');
48
72
  };
49
- export { getObject, uploadObject, deleteObject, listDirectory, createDirectory };
73
+ export { getObject, getPublicURL, getSignedURL, uploadObject, deleteObject, listDirectory, createDirectory };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genoacms/adapter-gcp",
3
- "version": "0.3.8",
3
+ "version": "0.3.13",
4
4
  "description": "Implementation of abstraction layer of GenoaCMS for GCP",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,9 +17,9 @@
17
17
  "homepage": "https://github.com/GenoaCMS/adapter-gcp#readme",
18
18
  "type": "module",
19
19
  "dependencies": {
20
- "@genoacms/cloudabstraction": "^0.3.8",
20
+ "@genoacms/cloudabstraction": "^0.3.13",
21
21
  "@google-cloud/firestore": "^7.1.0",
22
- "@google-cloud/storage": "^7.4.0"
22
+ "@google-cloud/storage": "^7.7.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@typescript-eslint/eslint-plugin": "^6.9.0",
@@ -38,16 +38,16 @@
38
38
  ],
39
39
  "exports": {
40
40
  "./auth": {
41
- "import": "./dist/auth/index.js",
42
- "types": "./dist/auth/index.d.ts"
41
+ "import": "./dist/services/auth/index.js",
42
+ "types": "./dist/services/auth/index.d.ts"
43
43
  },
44
44
  "./database": {
45
- "import": "./dist/database/index.js",
46
- "types": "./dist/database/index.d.ts"
45
+ "import": "./dist/services/database/index.js",
46
+ "types": "./dist/services/database/index.d.ts"
47
47
  },
48
48
  "./storage": {
49
- "import": "./dist/storage/index.js",
50
- "types": "./dist/storage/index.d.ts"
49
+ "import": "./dist/services/storage/index.js",
50
+ "types": "./dist/services/storage/index.d.ts"
51
51
  }
52
52
  },
53
53
  "scripts": {
@@ -1,7 +1,8 @@
1
1
  import type {
2
- storage as storageT
3
- } from '@genoacms/cloudabstraction'
4
- import { type Bucket, Storage } from '@google-cloud/storage'
2
+ StorageObject,
3
+ Adapter
4
+ } from '@genoacms/cloudabstraction/storage'
5
+ import { type Bucket, Storage, type File } from '@google-cloud/storage'
5
6
  import config from '../../config.js'
6
7
 
7
8
  const storage = new Storage({
@@ -14,7 +15,7 @@ const getBucket = (name: string): Bucket => {
14
15
  return bucket
15
16
  }
16
17
 
17
- const getObject: storageT.getObject = async ({ bucket, name }) => {
18
+ const getObject: Adapter['getObject'] = async ({ bucket, name }) => {
18
19
  const bucketInstance = getBucket(bucket)
19
20
  const file = bucketInstance.file(name)
20
21
 
@@ -23,35 +24,64 @@ const getObject: storageT.getObject = async ({ bucket, name }) => {
23
24
  }
24
25
  }
25
26
 
26
- const uploadObject: storageT.uploadObject = async ({ bucket, name }, stream) => {
27
+ const getPublicURL: Adapter['getPublicURL'] = async ({ bucket, name }) => {
28
+ const bucketInstance = getBucket(bucket)
29
+ const file = bucketInstance.file(name)
30
+ return file.publicUrl()
31
+ }
32
+
33
+ const getSignedURL: Adapter['getSignedURL'] = async ({ bucket, name }) => {
34
+ const bucketInstance = getBucket(bucket)
35
+ const file = bucketInstance.file(name)
36
+ const expires = new Date()
37
+ expires.setTime(expires.getTime() + 60 * 60)
38
+ const [url] = await file.getSignedUrl({
39
+ action: 'read',
40
+ expires
41
+ })
42
+ return url
43
+ }
44
+
45
+ const uploadObject: Adapter['uploadObject'] = async ({ bucket, name }, stream) => {
27
46
  const bucketInstance = getBucket(bucket)
28
47
  const file = bucketInstance.file(name)
29
48
  await file.save(stream)
30
49
  }
31
50
 
32
- const deleteObject: storageT.deleteObject = async ({ bucket, name }) => {
51
+ const deleteObject: Adapter['deleteObject'] = async ({ bucket, name }) => {
33
52
  const bucketInstance = getBucket(bucket)
34
53
  const file = bucketInstance.file(name)
35
54
  await file.delete()
36
55
  }
37
56
 
38
- const listDirectory: storageT.listDirectory = async ({ bucket, name }, listingParams = {}) => {
57
+ const listDirectory: Adapter['listDirectory'] = async ({ bucket, name }, listingParams = {}) => {
39
58
  const bucketInstance = getBucket(bucket)
40
- const [files] = await bucketInstance.getFiles({
59
+ const options = {
60
+ autoPaginate: false,
41
61
  prefix: name,
42
62
  maxResults: listingParams?.limit,
43
- startOffset: listingParams?.startAfter
44
- })
45
- return files.map((file) => {
46
- return {
47
- name: file.name,
48
- size: file.metadata.size ? parseInt(file.metadata.size as string) : 0,
49
- lastModified: new Date(file.metadata.updated as string)
50
- } satisfies storageT.StorageObject
51
- })
63
+ startOffset: listingParams?.startAfter,
64
+ delimiter: '/'
65
+
66
+ }
67
+ let [files, , apiResponse] =
68
+ (await bucketInstance.getFiles(options)) as [File[], object, { prefixes: string[] } | undefined]
69
+ files = files.filter((file) => !file.name.endsWith('.folderPlaceholder'))
70
+
71
+ return {
72
+ files: files.map((file) => {
73
+ return {
74
+ name: file.name,
75
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
76
+ size: file.metadata.size ? parseInt(file.metadata.size as string) : 0,
77
+ lastModified: new Date(file.metadata.updated as string)
78
+ } satisfies StorageObject
79
+ }),
80
+ directories: apiResponse?.prefixes ?? []
81
+ }
52
82
  }
53
83
 
54
- const createDirectory: storageT.createDirectory = async ({ bucket, name }) => {
84
+ const createDirectory: Adapter['createDirectory'] = async ({ bucket, name }) => {
55
85
  const bucketInstance = getBucket(bucket)
56
86
  const file = bucketInstance.file(`${name}/.folderPlaceholder`)
57
87
  await file.save('')
@@ -59,6 +89,8 @@ const createDirectory: storageT.createDirectory = async ({ bucket, name }) => {
59
89
 
60
90
  export {
61
91
  getObject,
92
+ getPublicURL,
93
+ getSignedURL,
62
94
  uploadObject,
63
95
  deleteObject,
64
96
  listDirectory,