@payloadcms/storage-gcs 3.0.0-alpha.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +22 -0
- package/README.md +1 -0
- package/dist/generateURL.d.ts +9 -0
- package/dist/generateURL.d.ts.map +1 -0
- package/dist/generateURL.js +6 -0
- package/dist/generateURL.js.map +1 -0
- package/dist/handleDelete.d.ts +9 -0
- package/dist/handleDelete.d.ts.map +1 -0
- package/dist/handleDelete.js +10 -0
- package/dist/handleDelete.js.map +1 -0
- package/dist/handleUpload.d.ts +13 -0
- package/dist/handleUpload.d.ts.map +1 -0
- package/dist/handleUpload.js +18 -0
- package/dist/handleUpload.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/staticHandler.d.ts +11 -0
- package/dist/staticHandler.d.ts.map +1 -0
- package/dist/staticHandler.js +45 -0
- package/dist/staticHandler.js.map +1 -0
- package/package.json +46 -0
- package/src/index.ts +97 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2022 Payload CMS, LLC <info@payloadcms.com>
|
|
4
|
+
Portions Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Nodemailer Email Adapter
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Storage } from '@google-cloud/storage';
|
|
2
|
+
import type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
+
interface Args {
|
|
4
|
+
bucket: string;
|
|
5
|
+
getStorageClient: () => Storage;
|
|
6
|
+
}
|
|
7
|
+
export declare const getGenerateURL: ({ bucket, getStorageClient }: Args) => GenerateURL;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=generateURL.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateURL.d.ts","sourceRoot":"","sources":["../src/generateURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wCAAwC,CAAA;AAIzE,UAAU,IAAI;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,MAAM,OAAO,CAAA;CAChC;AAED,eAAO,MAAM,cAAc,iCACM,IAAI,KAAG,WAKrC,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export const getGenerateURL = ({ bucket, getStorageClient })=>({ filename, prefix = '' })=>{
|
|
3
|
+
return decodeURIComponent(getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl());
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
//# sourceMappingURL=generateURL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/generateURL.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types'\n\nimport path from 'path'\n\ninterface Args {\n bucket: string\n getStorageClient: () => Storage\n}\n\nexport const getGenerateURL =\n ({ bucket, getStorageClient }: Args): GenerateURL =>\n ({ filename, prefix = '' }) => {\n return decodeURIComponent(\n getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl(),\n )\n }\n"],"names":["path","getGenerateURL","bucket","getStorageClient","filename","prefix","decodeURIComponent","file","posix","join","publicUrl"],"rangeMappings":";;;","mappings":"AAGA,OAAOA,UAAU,OAAM;AAOvB,OAAO,MAAMC,iBACX,CAAC,EAAEC,MAAM,EAAEC,gBAAgB,EAAQ,GACnC,CAAC,EAAEC,QAAQ,EAAEC,SAAS,EAAE,EAAE;QACxB,OAAOC,mBACLH,mBAAmBD,MAAM,CAACA,QAAQK,IAAI,CAACP,KAAKQ,KAAK,CAACC,IAAI,CAACJ,QAAQD,WAAWM,SAAS;IAEvF,EAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Storage } from '@google-cloud/storage';
|
|
2
|
+
import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
+
interface Args {
|
|
4
|
+
bucket: string;
|
|
5
|
+
getStorageClient: () => Storage;
|
|
6
|
+
}
|
|
7
|
+
export declare const getHandleDelete: ({ bucket, getStorageClient }: Args) => HandleDelete;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=handleDelete.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleDelete.d.ts","sourceRoot":"","sources":["../src/handleDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAI1E,UAAU,IAAI;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,MAAM,OAAO,CAAA;CAChC;AAED,eAAO,MAAM,eAAe,iCAAkC,IAAI,KAAG,YAMpE,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export const getHandleDelete = ({ bucket, getStorageClient })=>{
|
|
3
|
+
return async ({ doc: { prefix = '' }, filename })=>{
|
|
4
|
+
await getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).delete({
|
|
5
|
+
ignoreNotFound: true
|
|
6
|
+
});
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=handleDelete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/handleDelete.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types'\n\nimport path from 'path'\n\ninterface Args {\n bucket: string\n getStorageClient: () => Storage\n}\n\nexport const getHandleDelete = ({ bucket, getStorageClient }: Args): HandleDelete => {\n return async ({ doc: { prefix = '' }, filename }) => {\n await getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).delete({\n ignoreNotFound: true,\n })\n }\n}\n"],"names":["path","getHandleDelete","bucket","getStorageClient","doc","prefix","filename","file","posix","join","delete","ignoreNotFound"],"rangeMappings":";;;;;;;","mappings":"AAGA,OAAOA,UAAU,OAAM;AAOvB,OAAO,MAAMC,kBAAkB,CAAC,EAAEC,MAAM,EAAEC,gBAAgB,EAAQ;IAChE,OAAO,OAAO,EAAEC,KAAK,EAAEC,SAAS,EAAE,EAAE,EAAEC,QAAQ,EAAE;QAC9C,MAAMH,mBAAmBD,MAAM,CAACA,QAAQK,IAAI,CAACP,KAAKQ,KAAK,CAACC,IAAI,CAACJ,QAAQC,WAAWI,MAAM,CAAC;YACrFC,gBAAgB;QAClB;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Storage } from '@google-cloud/storage';
|
|
2
|
+
import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
+
import type { CollectionConfig } from 'payload/types';
|
|
4
|
+
interface Args {
|
|
5
|
+
acl?: 'Private' | 'Public';
|
|
6
|
+
bucket: string;
|
|
7
|
+
collection: CollectionConfig;
|
|
8
|
+
getStorageClient: () => Storage;
|
|
9
|
+
prefix?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const getHandleUpload: ({ acl, bucket, getStorageClient, prefix, }: Args) => HandleUpload;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=handleUpload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleUpload.d.ts","sourceRoot":"","sources":["../src/handleUpload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAIrD,UAAU,IAAI;IACZ,GAAG,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAA;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,OAAO,CAAA;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,eAAO,MAAM,eAAe,+CAKzB,IAAI,KAAG,YAiBT,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export const getHandleUpload = ({ acl, bucket, getStorageClient, prefix = '' })=>{
|
|
3
|
+
return async ({ data, file })=>{
|
|
4
|
+
const fileKey = path.posix.join(data.prefix || prefix, file.filename);
|
|
5
|
+
const gcsFile = getStorageClient().bucket(bucket).file(fileKey);
|
|
6
|
+
await gcsFile.save(file.buffer, {
|
|
7
|
+
metadata: {
|
|
8
|
+
contentType: file.mimeType
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
if (acl) {
|
|
12
|
+
await gcsFile[`make${acl}`]();
|
|
13
|
+
}
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=handleUpload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/handleUpload.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload/types'\n\nimport path from 'path'\n\ninterface Args {\n acl?: 'Private' | 'Public'\n bucket: string\n collection: CollectionConfig\n getStorageClient: () => Storage\n prefix?: string\n}\n\nexport const getHandleUpload = ({\n acl,\n bucket,\n getStorageClient,\n prefix = '',\n}: Args): HandleUpload => {\n return async ({ data, file }) => {\n const fileKey = path.posix.join(data.prefix || prefix, file.filename)\n\n const gcsFile = getStorageClient().bucket(bucket).file(fileKey)\n await gcsFile.save(file.buffer, {\n metadata: {\n contentType: file.mimeType,\n },\n })\n\n if (acl) {\n await gcsFile[`make${acl}`]()\n }\n\n return data\n }\n}\n"],"names":["path","getHandleUpload","acl","bucket","getStorageClient","prefix","data","file","fileKey","posix","join","filename","gcsFile","save","buffer","metadata","contentType","mimeType"],"rangeMappings":";;;;;;;;;;;;;;;","mappings":"AAIA,OAAOA,UAAU,OAAM;AAUvB,OAAO,MAAMC,kBAAkB,CAAC,EAC9BC,GAAG,EACHC,MAAM,EACNC,gBAAgB,EAChBC,SAAS,EAAE,EACN;IACL,OAAO,OAAO,EAAEC,IAAI,EAAEC,IAAI,EAAE;QAC1B,MAAMC,UAAUR,KAAKS,KAAK,CAACC,IAAI,CAACJ,KAAKD,MAAM,IAAIA,QAAQE,KAAKI,QAAQ;QAEpE,MAAMC,UAAUR,mBAAmBD,MAAM,CAACA,QAAQI,IAAI,CAACC;QACvD,MAAMI,QAAQC,IAAI,CAACN,KAAKO,MAAM,EAAE;YAC9BC,UAAU;gBACRC,aAAaT,KAAKU,QAAQ;YAC5B;QACF;QAEA,IAAIf,KAAK;YACP,MAAMU,OAAO,CAAC,CAAC,IAAI,EAAEV,IAAI,CAAC,CAAC;QAC7B;QAEA,OAAOI;IACT;AACF,EAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { StorageOptions } from '@google-cloud/storage';
|
|
2
|
+
import type { CollectionOptions } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
+
import type { Plugin } from 'payload/config';
|
|
4
|
+
export interface GcsStorageOptions {
|
|
5
|
+
acl?: 'Private' | 'Public';
|
|
6
|
+
/**
|
|
7
|
+
* The name of the bucket to use.
|
|
8
|
+
*/
|
|
9
|
+
bucket: string;
|
|
10
|
+
/**
|
|
11
|
+
* Collection options to apply the S3 adapter to.
|
|
12
|
+
*/
|
|
13
|
+
collections: Record<string, Omit<CollectionOptions, 'adapter'> | true>;
|
|
14
|
+
/**
|
|
15
|
+
* Whether or not to enable the plugin
|
|
16
|
+
*
|
|
17
|
+
* Default: true
|
|
18
|
+
*/
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Google Cloud Storage client configuration.
|
|
22
|
+
*
|
|
23
|
+
* @see https://github.com/googleapis/nodejs-storage
|
|
24
|
+
*/
|
|
25
|
+
options: StorageOptions;
|
|
26
|
+
}
|
|
27
|
+
type GcsStoragePlugin = (gcsStorageArgs: GcsStorageOptions) => Plugin;
|
|
28
|
+
export declare const gcsStorage: GcsStoragePlugin;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,KAAK,EAGV,iBAAiB,EAElB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAUpD,MAAM,WAAW,iBAAiB;IAChC,GAAG,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAA;IAE1B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,CAAA;IACtE;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB;;;;OAIG;IACH,OAAO,EAAE,cAAc,CAAA;CACxB;AAED,KAAK,gBAAgB,GAAG,CAAC,cAAc,EAAE,iBAAiB,KAAK,MAAM,CAAA;AAErE,eAAO,MAAM,UAAU,EAAE,gBA0BtB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Storage } from '@google-cloud/storage';
|
|
2
|
+
import { cloudStorage } from '@payloadcms/plugin-cloud-storage';
|
|
3
|
+
import { getGenerateURL } from './generateURL.js';
|
|
4
|
+
import { getHandleDelete } from './handleDelete.js';
|
|
5
|
+
import { getHandleUpload } from './handleUpload.js';
|
|
6
|
+
import { getHandler } from './staticHandler.js';
|
|
7
|
+
export const gcsStorage = (gcsStorageOptions)=>(incomingConfig)=>{
|
|
8
|
+
if (gcsStorageOptions.enabled === false) {
|
|
9
|
+
return incomingConfig;
|
|
10
|
+
}
|
|
11
|
+
const adapter = gcsStorageInternal(gcsStorageOptions);
|
|
12
|
+
// Add adapter to each collection option object
|
|
13
|
+
const collectionsWithAdapter = Object.entries(gcsStorageOptions.collections).reduce((acc, [slug, collOptions])=>({
|
|
14
|
+
...acc,
|
|
15
|
+
[slug]: {
|
|
16
|
+
...collOptions === true ? {} : collOptions,
|
|
17
|
+
adapter
|
|
18
|
+
}
|
|
19
|
+
}), {});
|
|
20
|
+
return cloudStorage({
|
|
21
|
+
collections: collectionsWithAdapter
|
|
22
|
+
})(incomingConfig);
|
|
23
|
+
};
|
|
24
|
+
function gcsStorageInternal({ acl, bucket, options }) {
|
|
25
|
+
return ({ collection, prefix })=>{
|
|
26
|
+
let storageClient = null;
|
|
27
|
+
const getStorageClient = ()=>{
|
|
28
|
+
if (storageClient) return storageClient;
|
|
29
|
+
storageClient = new Storage(options);
|
|
30
|
+
return storageClient;
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
generateURL: getGenerateURL({
|
|
34
|
+
bucket,
|
|
35
|
+
getStorageClient
|
|
36
|
+
}),
|
|
37
|
+
handleDelete: getHandleDelete({
|
|
38
|
+
bucket,
|
|
39
|
+
getStorageClient
|
|
40
|
+
}),
|
|
41
|
+
handleUpload: getHandleUpload({
|
|
42
|
+
acl,
|
|
43
|
+
bucket,
|
|
44
|
+
collection,
|
|
45
|
+
getStorageClient,
|
|
46
|
+
prefix
|
|
47
|
+
}),
|
|
48
|
+
staticHandler: getHandler({
|
|
49
|
+
bucket,
|
|
50
|
+
collection,
|
|
51
|
+
getStorageClient
|
|
52
|
+
})
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { StorageOptions } from '@google-cloud/storage'\nimport type {\n Adapter,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, Plugin } from 'payload/config'\n\nimport { Storage } from '@google-cloud/storage'\nimport { cloudStorage } from '@payloadcms/plugin-cloud-storage'\n\nimport { getGenerateURL } from './generateURL.js'\nimport { getHandleDelete } from './handleDelete.js'\nimport { getHandleUpload } from './handleUpload.js'\nimport { getHandler } from './staticHandler.js'\n\nexport interface GcsStorageOptions {\n acl?: 'Private' | 'Public'\n\n /**\n * The name of the bucket to use.\n */\n bucket: string\n /**\n * Collection options to apply the S3 adapter to.\n */\n collections: Record<string, Omit<CollectionOptions, 'adapter'> | true>\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n\n /**\n * Google Cloud Storage client configuration.\n *\n * @see https://github.com/googleapis/nodejs-storage\n */\n options: StorageOptions\n}\n\ntype GcsStoragePlugin = (gcsStorageArgs: GcsStorageOptions) => Plugin\n\nexport const gcsStorage: GcsStoragePlugin =\n (gcsStorageOptions: GcsStorageOptions) =>\n (incomingConfig: Config): Config => {\n if (gcsStorageOptions.enabled === false) {\n return incomingConfig\n }\n\n const adapter = gcsStorageInternal(gcsStorageOptions)\n\n // Add adapter to each collection option object\n const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n gcsStorageOptions.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: {\n ...(collOptions === true ? {} : collOptions),\n adapter,\n },\n }),\n {} as Record<string, CollectionOptions>,\n )\n\n return cloudStorage({\n collections: collectionsWithAdapter,\n })(incomingConfig)\n }\n\nfunction gcsStorageInternal({ acl, bucket, options }: GcsStorageOptions): Adapter {\n return ({ collection, prefix }): GeneratedAdapter => {\n let storageClient: Storage | null = null\n\n const getStorageClient = (): Storage => {\n if (storageClient) return storageClient\n storageClient = new Storage(options)\n return storageClient\n }\n\n return {\n generateURL: getGenerateURL({ bucket, getStorageClient }),\n handleDelete: getHandleDelete({ bucket, getStorageClient }),\n handleUpload: getHandleUpload({\n acl,\n bucket,\n collection,\n getStorageClient,\n prefix,\n }),\n staticHandler: getHandler({ bucket, collection, getStorageClient }),\n }\n }\n}\n"],"names":["Storage","cloudStorage","getGenerateURL","getHandleDelete","getHandleUpload","getHandler","gcsStorage","gcsStorageOptions","incomingConfig","enabled","adapter","gcsStorageInternal","collectionsWithAdapter","Object","entries","collections","reduce","acc","slug","collOptions","acl","bucket","options","collection","prefix","storageClient","getStorageClient","generateURL","handleDelete","handleUpload","staticHandler"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AASA,SAASA,OAAO,QAAQ,wBAAuB;AAC/C,SAASC,YAAY,QAAQ,mCAAkC;AAE/D,SAASC,cAAc,QAAQ,mBAAkB;AACjD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,UAAU,QAAQ,qBAAoB;AA8B/C,OAAO,MAAMC,aACX,CAACC,oBACD,CAACC;QACC,IAAID,kBAAkBE,OAAO,KAAK,OAAO;YACvC,OAAOD;QACT;QAEA,MAAME,UAAUC,mBAAmBJ;QAEnC,+CAA+C;QAC/C,MAAMK,yBAAmEC,OAAOC,OAAO,CACrFP,kBAAkBQ,WAAW,EAC7BC,MAAM,CACN,CAACC,KAAK,CAACC,MAAMC,YAAY,GAAM,CAAA;gBAC7B,GAAGF,GAAG;gBACN,CAACC,KAAK,EAAE;oBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;oBAC3CT;gBACF;YACF,CAAA,GACA,CAAC;QAGH,OAAOT,aAAa;YAClBc,aAAaH;QACf,GAAGJ;IACL,EAAC;AAEH,SAASG,mBAAmB,EAAES,GAAG,EAAEC,MAAM,EAAEC,OAAO,EAAqB;IACrE,OAAO,CAAC,EAAEC,UAAU,EAAEC,MAAM,EAAE;QAC5B,IAAIC,gBAAgC;QAEpC,MAAMC,mBAAmB;YACvB,IAAID,eAAe,OAAOA;YAC1BA,gBAAgB,IAAIzB,QAAQsB;YAC5B,OAAOG;QACT;QAEA,OAAO;YACLE,aAAazB,eAAe;gBAAEmB;gBAAQK;YAAiB;YACvDE,cAAczB,gBAAgB;gBAAEkB;gBAAQK;YAAiB;YACzDG,cAAczB,gBAAgB;gBAC5BgB;gBACAC;gBACAE;gBACAG;gBACAF;YACF;YACAM,eAAezB,WAAW;gBAAEgB;gBAAQE;gBAAYG;YAAiB;QACnE;IACF;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Storage } from '@google-cloud/storage';
|
|
2
|
+
import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
+
import type { CollectionConfig } from 'payload/types';
|
|
4
|
+
interface Args {
|
|
5
|
+
bucket: string;
|
|
6
|
+
collection: CollectionConfig;
|
|
7
|
+
getStorageClient: () => Storage;
|
|
8
|
+
}
|
|
9
|
+
export declare const getHandler: ({ bucket, collection, getStorageClient }: Args) => StaticHandler;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=staticHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"staticHandler.d.ts","sourceRoot":"","sources":["../src/staticHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAA;AAC3E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAKrD,UAAU,IAAI;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,OAAO,CAAA;CAChC;AAED,eAAO,MAAM,UAAU,6CAA8C,IAAI,KAAG,aAqC3E,CAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export const getHandler = ({ bucket, collection, getStorageClient })=>{
|
|
4
|
+
return async (req, { params: { filename } })=>{
|
|
5
|
+
try {
|
|
6
|
+
const prefix = await getFilePrefix({
|
|
7
|
+
collection,
|
|
8
|
+
filename,
|
|
9
|
+
req
|
|
10
|
+
});
|
|
11
|
+
const file = getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename));
|
|
12
|
+
const [metadata] = await file.getMetadata();
|
|
13
|
+
// Manually create a ReadableStream for the web from a Node.js stream.
|
|
14
|
+
const readableStream = new ReadableStream({
|
|
15
|
+
start (controller) {
|
|
16
|
+
const nodeStream = file.createReadStream();
|
|
17
|
+
nodeStream.on('data', (chunk)=>{
|
|
18
|
+
controller.enqueue(new Uint8Array(chunk));
|
|
19
|
+
});
|
|
20
|
+
nodeStream.on('end', ()=>{
|
|
21
|
+
controller.close();
|
|
22
|
+
});
|
|
23
|
+
nodeStream.on('error', (err)=>{
|
|
24
|
+
controller.error(err);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return new Response(readableStream, {
|
|
29
|
+
headers: new Headers({
|
|
30
|
+
'Content-Length': String(metadata.size),
|
|
31
|
+
'Content-Type': String(metadata.contentType),
|
|
32
|
+
ETag: String(metadata.etag)
|
|
33
|
+
}),
|
|
34
|
+
status: 200
|
|
35
|
+
});
|
|
36
|
+
} catch (err) {
|
|
37
|
+
req.payload.logger.error(err);
|
|
38
|
+
return new Response('Internal Server Error', {
|
|
39
|
+
status: 500
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=staticHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/staticHandler.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload/types'\n\nimport { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities'\nimport path from 'path'\n\ninterface Args {\n bucket: string\n collection: CollectionConfig\n getStorageClient: () => Storage\n}\n\nexport const getHandler = ({ bucket, collection, getStorageClient }: Args): StaticHandler => {\n return async (req, { params: { filename } }) => {\n try {\n const prefix = await getFilePrefix({ collection, filename, req })\n const file = getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename))\n\n const [metadata] = await file.getMetadata()\n\n // Manually create a ReadableStream for the web from a Node.js stream.\n const readableStream = new ReadableStream({\n start(controller) {\n const nodeStream = file.createReadStream()\n nodeStream.on('data', (chunk) => {\n controller.enqueue(new Uint8Array(chunk))\n })\n nodeStream.on('end', () => {\n controller.close()\n })\n nodeStream.on('error', (err) => {\n controller.error(err)\n })\n },\n })\n\n return new Response(readableStream, {\n headers: new Headers({\n 'Content-Length': String(metadata.size),\n 'Content-Type': String(metadata.contentType),\n ETag: String(metadata.etag),\n }),\n status: 200,\n })\n } catch (err: unknown) {\n req.payload.logger.error(err)\n return new Response('Internal Server Error', { status: 500 })\n }\n }\n}\n"],"names":["getFilePrefix","path","getHandler","bucket","collection","getStorageClient","req","params","filename","prefix","file","posix","join","metadata","getMetadata","readableStream","ReadableStream","start","controller","nodeStream","createReadStream","on","chunk","enqueue","Uint8Array","close","err","error","Response","headers","Headers","String","size","contentType","ETag","etag","status","payload","logger"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAIA,SAASA,aAAa,QAAQ,6CAA4C;AAC1E,OAAOC,UAAU,OAAM;AAQvB,OAAO,MAAMC,aAAa,CAAC,EAAEC,MAAM,EAAEC,UAAU,EAAEC,gBAAgB,EAAQ;IACvE,OAAO,OAAOC,KAAK,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,EAAE;QACzC,IAAI;YACF,MAAMC,SAAS,MAAMT,cAAc;gBAAEI;gBAAYI;gBAAUF;YAAI;YAC/D,MAAMI,OAAOL,mBAAmBF,MAAM,CAACA,QAAQO,IAAI,CAACT,KAAKU,KAAK,CAACC,IAAI,CAACH,QAAQD;YAE5E,MAAM,CAACK,SAAS,GAAG,MAAMH,KAAKI,WAAW;YAEzC,sEAAsE;YACtE,MAAMC,iBAAiB,IAAIC,eAAe;gBACxCC,OAAMC,UAAU;oBACd,MAAMC,aAAaT,KAAKU,gBAAgB;oBACxCD,WAAWE,EAAE,CAAC,QAAQ,CAACC;wBACrBJ,WAAWK,OAAO,CAAC,IAAIC,WAAWF;oBACpC;oBACAH,WAAWE,EAAE,CAAC,OAAO;wBACnBH,WAAWO,KAAK;oBAClB;oBACAN,WAAWE,EAAE,CAAC,SAAS,CAACK;wBACtBR,WAAWS,KAAK,CAACD;oBACnB;gBACF;YACF;YAEA,OAAO,IAAIE,SAASb,gBAAgB;gBAClCc,SAAS,IAAIC,QAAQ;oBACnB,kBAAkBC,OAAOlB,SAASmB,IAAI;oBACtC,gBAAgBD,OAAOlB,SAASoB,WAAW;oBAC3CC,MAAMH,OAAOlB,SAASsB,IAAI;gBAC5B;gBACAC,QAAQ;YACV;QACF,EAAE,OAAOV,KAAc;YACrBpB,IAAI+B,OAAO,CAACC,MAAM,CAACX,KAAK,CAACD;YACzB,OAAO,IAAIE,SAAS,yBAAyB;gBAAEQ,QAAQ;YAAI;QAC7D;IACF;AACF,EAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@payloadcms/storage-gcs",
|
|
3
|
+
"version": "3.0.0-alpha.62",
|
|
4
|
+
"description": "Payload storage adapter for Google Cloud Storage",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/payloadcms/payload.git",
|
|
8
|
+
"directory": "packages/storage-gcs"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://payloadcms.com",
|
|
12
|
+
"author": "Payload CMS, Inc.",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@google-cloud/storage": "^7.7.0",
|
|
25
|
+
"@payloadcms/plugin-cloud-storage": "3.0.0-alpha.62"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"payload": "3.0.0-alpha.62"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"payload": "3.0.0-alpha.62"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.20.2"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "pnpm build:swc && pnpm build:types",
|
|
41
|
+
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
|
|
42
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
43
|
+
"build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build",
|
|
44
|
+
"clean": "rimraf {dist,*.tsbuildinfo}"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { StorageOptions } from '@google-cloud/storage'
|
|
2
|
+
import type {
|
|
3
|
+
Adapter,
|
|
4
|
+
PluginOptions as CloudStoragePluginOptions,
|
|
5
|
+
CollectionOptions,
|
|
6
|
+
GeneratedAdapter,
|
|
7
|
+
} from '@payloadcms/plugin-cloud-storage/types'
|
|
8
|
+
import type { Config, Plugin } from 'payload/config'
|
|
9
|
+
|
|
10
|
+
import { Storage } from '@google-cloud/storage'
|
|
11
|
+
import { cloudStorage } from '@payloadcms/plugin-cloud-storage'
|
|
12
|
+
|
|
13
|
+
import { getGenerateURL } from './generateURL.js'
|
|
14
|
+
import { getHandleDelete } from './handleDelete.js'
|
|
15
|
+
import { getHandleUpload } from './handleUpload.js'
|
|
16
|
+
import { getHandler } from './staticHandler.js'
|
|
17
|
+
|
|
18
|
+
export interface GcsStorageOptions {
|
|
19
|
+
acl?: 'Private' | 'Public'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The name of the bucket to use.
|
|
23
|
+
*/
|
|
24
|
+
bucket: string
|
|
25
|
+
/**
|
|
26
|
+
* Collection options to apply the S3 adapter to.
|
|
27
|
+
*/
|
|
28
|
+
collections: Record<string, Omit<CollectionOptions, 'adapter'> | true>
|
|
29
|
+
/**
|
|
30
|
+
* Whether or not to enable the plugin
|
|
31
|
+
*
|
|
32
|
+
* Default: true
|
|
33
|
+
*/
|
|
34
|
+
enabled?: boolean
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Google Cloud Storage client configuration.
|
|
38
|
+
*
|
|
39
|
+
* @see https://github.com/googleapis/nodejs-storage
|
|
40
|
+
*/
|
|
41
|
+
options: StorageOptions
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type GcsStoragePlugin = (gcsStorageArgs: GcsStorageOptions) => Plugin
|
|
45
|
+
|
|
46
|
+
export const gcsStorage: GcsStoragePlugin =
|
|
47
|
+
(gcsStorageOptions: GcsStorageOptions) =>
|
|
48
|
+
(incomingConfig: Config): Config => {
|
|
49
|
+
if (gcsStorageOptions.enabled === false) {
|
|
50
|
+
return incomingConfig
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const adapter = gcsStorageInternal(gcsStorageOptions)
|
|
54
|
+
|
|
55
|
+
// Add adapter to each collection option object
|
|
56
|
+
const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(
|
|
57
|
+
gcsStorageOptions.collections,
|
|
58
|
+
).reduce(
|
|
59
|
+
(acc, [slug, collOptions]) => ({
|
|
60
|
+
...acc,
|
|
61
|
+
[slug]: {
|
|
62
|
+
...(collOptions === true ? {} : collOptions),
|
|
63
|
+
adapter,
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
{} as Record<string, CollectionOptions>,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return cloudStorage({
|
|
70
|
+
collections: collectionsWithAdapter,
|
|
71
|
+
})(incomingConfig)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function gcsStorageInternal({ acl, bucket, options }: GcsStorageOptions): Adapter {
|
|
75
|
+
return ({ collection, prefix }): GeneratedAdapter => {
|
|
76
|
+
let storageClient: Storage | null = null
|
|
77
|
+
|
|
78
|
+
const getStorageClient = (): Storage => {
|
|
79
|
+
if (storageClient) return storageClient
|
|
80
|
+
storageClient = new Storage(options)
|
|
81
|
+
return storageClient
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
generateURL: getGenerateURL({ bucket, getStorageClient }),
|
|
86
|
+
handleDelete: getHandleDelete({ bucket, getStorageClient }),
|
|
87
|
+
handleUpload: getHandleUpload({
|
|
88
|
+
acl,
|
|
89
|
+
bucket,
|
|
90
|
+
collection,
|
|
91
|
+
getStorageClient,
|
|
92
|
+
prefix,
|
|
93
|
+
}),
|
|
94
|
+
staticHandler: getHandler({ bucket, collection, getStorageClient }),
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|