@genoacms/adapter-gcp 0.3.9 → 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);
@@ -35,7 +51,8 @@ const listDirectory = async ({ bucket, name }, listingParams = {}) => {
35
51
  startOffset: listingParams?.startAfter,
36
52
  delimiter: '/'
37
53
  };
38
- const [files, , apiResponse] = (await bucketInstance.getFiles(options));
54
+ let [files, , apiResponse] = (await bucketInstance.getFiles(options));
55
+ files = files.filter((file) => !file.name.endsWith('.folderPlaceholder'));
39
56
  return {
40
57
  files: files.map((file) => {
41
58
  return {
@@ -53,4 +70,4 @@ const createDirectory = async ({ bucket, name }) => {
53
70
  const file = bucketInstance.file(`${name}/.folderPlaceholder`);
54
71
  await file.save('');
55
72
  };
56
- 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.9",
3
+ "version": "0.3.13",
4
4
  "description": "Implementation of abstraction layer of GenoaCMS for GCP",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "homepage": "https://github.com/GenoaCMS/adapter-gcp#readme",
18
18
  "type": "module",
19
19
  "dependencies": {
20
- "@genoacms/cloudabstraction": "^0.3.9",
20
+ "@genoacms/cloudabstraction": "^0.3.13",
21
21
  "@google-cloud/firestore": "^7.1.0",
22
22
  "@google-cloud/storage": "^7.7.0"
23
23
  },
@@ -1,6 +1,7 @@
1
1
  import type {
2
- storage as storageT
3
- } from '@genoacms/cloudabstraction'
2
+ StorageObject,
3
+ Adapter
4
+ } from '@genoacms/cloudabstraction/storage'
4
5
  import { type Bucket, Storage, type File } from '@google-cloud/storage'
5
6
  import config from '../../config.js'
6
7
 
@@ -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,19 +24,37 @@ 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
59
  const options = {
41
60
  autoPaginate: false,
@@ -43,9 +62,12 @@ const listDirectory: storageT.listDirectory = async ({ bucket, name }, listingPa
43
62
  maxResults: listingParams?.limit,
44
63
  startOffset: listingParams?.startAfter,
45
64
  delimiter: '/'
65
+
46
66
  }
47
- const [files, , apiResponse] =
48
- (await bucketInstance.getFiles(options)) as [File[], object, { prefixes: string[] } | undefined ]
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
+
49
71
  return {
50
72
  files: files.map((file) => {
51
73
  return {
@@ -53,13 +75,13 @@ const listDirectory: storageT.listDirectory = async ({ bucket, name }, listingPa
53
75
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
54
76
  size: file.metadata.size ? parseInt(file.metadata.size as string) : 0,
55
77
  lastModified: new Date(file.metadata.updated as string)
56
- } satisfies storageT.StorageObject
78
+ } satisfies StorageObject
57
79
  }),
58
80
  directories: apiResponse?.prefixes ?? []
59
81
  }
60
82
  }
61
83
 
62
- const createDirectory: storageT.createDirectory = async ({ bucket, name }) => {
84
+ const createDirectory: Adapter['createDirectory'] = async ({ bucket, name }) => {
63
85
  const bucketInstance = getBucket(bucket)
64
86
  const file = bucketInstance.file(`${name}/.folderPlaceholder`)
65
87
  await file.save('')
@@ -67,6 +89,8 @@ const createDirectory: storageT.createDirectory = async ({ bucket, name }) => {
67
89
 
68
90
  export {
69
91
  getObject,
92
+ getPublicURL,
93
+ getSignedURL,
70
94
  uploadObject,
71
95
  deleteObject,
72
96
  listDirectory,