@payloadcms/storage-gcs 4.0.0-internal.5b1e7cd → 4.0.0-internal.c2b57ce
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/dist/client/GcsClientUploadHandler.d.ts +1 -1
- package/dist/client/GcsClientUploadHandler.d.ts.map +1 -1
- package/dist/client/GcsClientUploadHandler.js +5 -2
- package/dist/client/GcsClientUploadHandler.js.map +1 -1
- package/dist/generateSignedURL.d.ts.map +1 -1
- package/dist/generateSignedURL.js +5 -2
- package/dist/generateSignedURL.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -59
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const GcsClientUploadHandler: ({ children, collectionSlug, enabled, extra, prefix, serverHandlerPath, }: {
|
|
2
2
|
children: import("react").ReactNode;
|
|
3
|
-
collectionSlug: import("
|
|
3
|
+
collectionSlug: import("payload").UploadCollectionSlug;
|
|
4
4
|
enabled?: boolean;
|
|
5
5
|
extra: Record<string, unknown>;
|
|
6
6
|
prefix?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GcsClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/GcsClientUploadHandler.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,sBAAsB;;;;;;;
|
|
1
|
+
{"version":3,"file":"GcsClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/GcsClientUploadHandler.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,sBAAsB;;;;;;;iCAkDjC,CAAA"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client';
|
|
3
3
|
import { formatAdminURL } from 'payload/shared';
|
|
4
4
|
export const GcsClientUploadHandler = createClientUploadHandler({
|
|
5
|
-
handler: async ({ apiRoute, collectionSlug, docPrefix, file, serverHandlerPath, serverURL })=>{
|
|
5
|
+
handler: async ({ apiRoute, collectionSlug, docPrefix, file, serverHandlerPath, serverURL, updateFilename })=>{
|
|
6
6
|
const endpointRoute = formatAdminURL({
|
|
7
7
|
apiRoute,
|
|
8
8
|
path: serverHandlerPath,
|
|
@@ -18,7 +18,10 @@ export const GcsClientUploadHandler = createClientUploadHandler({
|
|
|
18
18
|
credentials: 'include',
|
|
19
19
|
method: 'POST'
|
|
20
20
|
});
|
|
21
|
-
const { docPrefix: sanitizedDocPrefix, url } = await response.json();
|
|
21
|
+
const { docPrefix: sanitizedDocPrefix, filename: sanitizedFilename, url } = await response.json();
|
|
22
|
+
if (sanitizedFilename && sanitizedFilename !== file.name) {
|
|
23
|
+
updateFilename(sanitizedFilename);
|
|
24
|
+
}
|
|
22
25
|
await fetch(url, {
|
|
23
26
|
body: file,
|
|
24
27
|
headers: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/GcsClientUploadHandler.ts"],"sourcesContent":["'use client'\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { formatAdminURL } from 'payload/shared'\n\nexport const GcsClientUploadHandler = createClientUploadHandler({\n handler: async ({
|
|
1
|
+
{"version":3,"sources":["../../src/client/GcsClientUploadHandler.ts"],"sourcesContent":["'use client'\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { formatAdminURL } from 'payload/shared'\n\nexport const GcsClientUploadHandler = createClientUploadHandler({\n handler: async ({\n apiRoute,\n collectionSlug,\n docPrefix,\n file,\n serverHandlerPath,\n serverURL,\n updateFilename,\n }) => {\n const endpointRoute = formatAdminURL({\n apiRoute,\n path: serverHandlerPath,\n serverURL,\n })\n const response = await fetch(endpointRoute, {\n body: JSON.stringify({\n collectionSlug,\n docPrefix,\n filename: file.name,\n mimeType: file.type,\n }),\n credentials: 'include',\n method: 'POST',\n })\n\n const {\n docPrefix: sanitizedDocPrefix,\n filename: sanitizedFilename,\n url,\n } = (await response.json()) as {\n docPrefix: string\n filename?: string\n url: string\n }\n\n if (sanitizedFilename && sanitizedFilename !== file.name) {\n updateFilename(sanitizedFilename)\n }\n\n await fetch(url, {\n body: file,\n headers: { 'Content-Length': file.size.toString(), 'Content-Type': file.type },\n method: 'PUT',\n })\n\n return {\n prefix: sanitizedDocPrefix,\n }\n },\n})\n"],"names":["createClientUploadHandler","formatAdminURL","GcsClientUploadHandler","handler","apiRoute","collectionSlug","docPrefix","file","serverHandlerPath","serverURL","updateFilename","endpointRoute","path","response","fetch","body","JSON","stringify","filename","name","mimeType","type","credentials","method","sanitizedDocPrefix","sanitizedFilename","url","json","headers","size","toString","prefix"],"mappings":"AAAA;AACA,SAASA,yBAAyB,QAAQ,0CAAyC;AACnF,SAASC,cAAc,QAAQ,iBAAgB;AAE/C,OAAO,MAAMC,yBAAyBF,0BAA0B;IAC9DG,SAAS,OAAO,EACdC,QAAQ,EACRC,cAAc,EACdC,SAAS,EACTC,IAAI,EACJC,iBAAiB,EACjBC,SAAS,EACTC,cAAc,EACf;QACC,MAAMC,gBAAgBV,eAAe;YACnCG;YACAQ,MAAMJ;YACNC;QACF;QACA,MAAMI,WAAW,MAAMC,MAAMH,eAAe;YAC1CI,MAAMC,KAAKC,SAAS,CAAC;gBACnBZ;gBACAC;gBACAY,UAAUX,KAAKY,IAAI;gBACnBC,UAAUb,KAAKc,IAAI;YACrB;YACAC,aAAa;YACbC,QAAQ;QACV;QAEA,MAAM,EACJjB,WAAWkB,kBAAkB,EAC7BN,UAAUO,iBAAiB,EAC3BC,GAAG,EACJ,GAAI,MAAMb,SAASc,IAAI;QAMxB,IAAIF,qBAAqBA,sBAAsBlB,KAAKY,IAAI,EAAE;YACxDT,eAAee;QACjB;QAEA,MAAMX,MAAMY,KAAK;YACfX,MAAMR;YACNqB,SAAS;gBAAE,kBAAkBrB,KAAKsB,IAAI,CAACC,QAAQ;gBAAI,gBAAgBvB,KAAKc,IAAI;YAAC;YAC7EE,QAAQ;QACV;QAEA,OAAO;YACLQ,QAAQP;QACV;IACF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateSignedURL.d.ts","sourceRoot":"","sources":["../src/generateSignedURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD,UAAU,IAAI;IACZ,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,GAAG,CAAC,EAAE,SAAS,GAAG,aAAa,CAAA;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAA;IAC7C,gBAAgB,EAAE,MAAM,OAAO,CAAA;IAC/B,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAID,eAAO,MAAM,2BAA2B,
|
|
1
|
+
{"version":3,"file":"generateSignedURL.d.ts","sourceRoot":"","sources":["../src/generateSignedURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD,UAAU,IAAI;IACZ,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,GAAG,CAAC,EAAE,SAAS,GAAG,aAAa,CAAA;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,iBAAiB,CAAC,aAAa,CAAC,CAAA;IAC7C,gBAAgB,EAAE,MAAM,OAAO,CAAA;IAC/B,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAID,eAAO,MAAM,2BAA2B,GAAI,0EAMzC,IAAI,KAAG,cAkDT,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveSignedURLKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
2
|
import { APIError, Forbidden } from 'payload';
|
|
3
3
|
const defaultAccess = ({ req })=>!!req.user;
|
|
4
4
|
export const getGenerateSignedURLHandler = ({ access = defaultAccess, bucket, collections, getStorageClient, useCompositePrefixes = false })=>{
|
|
@@ -18,10 +18,12 @@ export const getGenerateSignedURLHandler = ({ access = defaultAccess, bucket, co
|
|
|
18
18
|
})) {
|
|
19
19
|
throw new Forbidden();
|
|
20
20
|
}
|
|
21
|
-
const { fileKey, sanitizedDocPrefix } =
|
|
21
|
+
const { fileKey, sanitizedDocPrefix, sanitizedFilename } = await resolveSignedURLKey({
|
|
22
22
|
collectionPrefix,
|
|
23
|
+
collectionSlug,
|
|
23
24
|
docPrefix,
|
|
24
25
|
filename,
|
|
26
|
+
req,
|
|
25
27
|
useCompositePrefixes
|
|
26
28
|
});
|
|
27
29
|
const [url] = await getStorageClient().bucket(bucket).file(fileKey).getSignedUrl({
|
|
@@ -32,6 +34,7 @@ export const getGenerateSignedURLHandler = ({ access = defaultAccess, bucket, co
|
|
|
32
34
|
});
|
|
33
35
|
return Response.json({
|
|
34
36
|
docPrefix: sanitizedDocPrefix,
|
|
37
|
+
filename: sanitizedFilename,
|
|
35
38
|
url
|
|
36
39
|
});
|
|
37
40
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generateSignedURL.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/generateSignedURL.ts"],"sourcesContent":["import type { Storage } from '@google-cloud/storage'\nimport type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport { resolveSignedURLKey } from '@payloadcms/plugin-cloud-storage/utilities'\nimport { APIError, Forbidden } from 'payload'\n\nimport type { GcsStorageOptions } from './index.js'\n\ninterface Args {\n access?: ClientUploadsAccess\n acl?: 'private' | 'public-read'\n bucket: string\n collections: GcsStorageOptions['collections']\n getStorageClient: () => Storage\n useCompositePrefixes?: boolean\n}\n\nconst defaultAccess: Args['access'] = ({ req }) => !!req.user\n\nexport const getGenerateSignedURLHandler = ({\n access = defaultAccess,\n bucket,\n collections,\n getStorageClient,\n useCompositePrefixes = false,\n}: Args): PayloadHandler => {\n return async (req) => {\n if (!req.json) {\n throw new APIError('Unreachable')\n }\n\n const { collectionSlug, docPrefix, filename, mimeType } = (await req.json()) as {\n collectionSlug: string\n docPrefix?: string\n filename: string\n mimeType: string\n }\n\n const collectionStorageConfig = collections[collectionSlug]\n if (!collectionStorageConfig) {\n throw new APIError(`Collection ${collectionSlug} was not found in GCS storage options`)\n }\n\n const collectionPrefix =\n (typeof collectionStorageConfig === 'object' && collectionStorageConfig.prefix) || ''\n\n if (!(await access({ collectionSlug, req }))) {\n throw new Forbidden()\n }\n\n const { fileKey, sanitizedDocPrefix, sanitizedFilename } = await resolveSignedURLKey({\n collectionPrefix,\n collectionSlug,\n docPrefix,\n filename,\n req,\n useCompositePrefixes,\n })\n\n const [url] = await getStorageClient()\n .bucket(bucket)\n .file(fileKey)\n .getSignedUrl({\n action: 'write',\n contentType: mimeType,\n expires: Date.now() + 60 * 60 * 5,\n version: 'v4',\n })\n\n return Response.json({\n docPrefix: sanitizedDocPrefix,\n filename: sanitizedFilename,\n url,\n })\n }\n}\n"],"names":["resolveSignedURLKey","APIError","Forbidden","defaultAccess","req","user","getGenerateSignedURLHandler","access","bucket","collections","getStorageClient","useCompositePrefixes","json","collectionSlug","docPrefix","filename","mimeType","collectionStorageConfig","collectionPrefix","prefix","fileKey","sanitizedDocPrefix","sanitizedFilename","url","file","getSignedUrl","action","contentType","expires","Date","now","version","Response"],"mappings":"AAIA,SAASA,mBAAmB,QAAQ,6CAA4C;AAChF,SAASC,QAAQ,EAAEC,SAAS,QAAQ,UAAS;AAa7C,MAAMC,gBAAgC,CAAC,EAAEC,GAAG,EAAE,GAAK,CAAC,CAACA,IAAIC,IAAI;AAE7D,OAAO,MAAMC,8BAA8B,CAAC,EAC1CC,SAASJ,aAAa,EACtBK,MAAM,EACNC,WAAW,EACXC,gBAAgB,EAChBC,uBAAuB,KAAK,EACvB;IACL,OAAO,OAAOP;QACZ,IAAI,CAACA,IAAIQ,IAAI,EAAE;YACb,MAAM,IAAIX,SAAS;QACrB;QAEA,MAAM,EAAEY,cAAc,EAAEC,SAAS,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,GAAI,MAAMZ,IAAIQ,IAAI;QAOzE,MAAMK,0BAA0BR,WAAW,CAACI,eAAe;QAC3D,IAAI,CAACI,yBAAyB;YAC5B,MAAM,IAAIhB,SAAS,CAAC,WAAW,EAAEY,eAAe,qCAAqC,CAAC;QACxF;QAEA,MAAMK,mBACJ,AAAC,OAAOD,4BAA4B,YAAYA,wBAAwBE,MAAM,IAAK;QAErF,IAAI,CAAE,MAAMZ,OAAO;YAAEM;YAAgBT;QAAI,IAAK;YAC5C,MAAM,IAAIF;QACZ;QAEA,MAAM,EAAEkB,OAAO,EAAEC,kBAAkB,EAAEC,iBAAiB,EAAE,GAAG,MAAMtB,oBAAoB;YACnFkB;YACAL;YACAC;YACAC;YACAX;YACAO;QACF;QAEA,MAAM,CAACY,IAAI,GAAG,MAAMb,mBACjBF,MAAM,CAACA,QACPgB,IAAI,CAACJ,SACLK,YAAY,CAAC;YACZC,QAAQ;YACRC,aAAaX;YACbY,SAASC,KAAKC,GAAG,KAAK,KAAK,KAAK;YAChCC,SAAS;QACX;QAEF,OAAOC,SAASpB,IAAI,CAAC;YACnBE,WAAWO;YACXN,UAAUO;YACVC;QACF;IACF;AACF,EAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { StorageOptions } from '@google-cloud/storage';
|
|
2
2
|
import type { ClientUploadsConfig, CollectionOptions } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
-
import type {
|
|
3
|
+
import type { StorageAdapter, UploadCollectionSlug } from 'payload';
|
|
4
4
|
export interface GcsStorageOptions {
|
|
5
5
|
acl?: 'Private' | 'Public';
|
|
6
6
|
/**
|
|
@@ -59,7 +59,7 @@ export interface GcsStorageOptions {
|
|
|
59
59
|
*/
|
|
60
60
|
useCompositePrefixes?: boolean;
|
|
61
61
|
}
|
|
62
|
-
type
|
|
63
|
-
export declare const gcsStorage:
|
|
62
|
+
type GcsStorageFactory = (gcsStorageArgs: GcsStorageOptions) => StorageAdapter;
|
|
63
|
+
export declare const gcsStorage: GcsStorageFactory;
|
|
64
64
|
export {};
|
|
65
65
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,
|
|
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,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAS3E,MAAM,WAAW,iBAAiB;IAChC,GAAG,CAAC,EAAE,SAAS,GAAG,QAAQ,CAAA;IAE1B;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAE5B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;OAEG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAA;IACnC;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IAC7F;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;OAIG;IACH,OAAO,EAAE,cAAc,CAAA;IAEvB;;;;;;;;;;;;OAYG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,KAAK,iBAAiB,GAAG,CAAC,cAAc,EAAE,iBAAiB,KAAK,cAAc,CAAA;AAI9E,eAAO,MAAM,UAAU,EAAE,iBAuFvB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -4,69 +4,73 @@ import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
|
4
4
|
import { createGcsAdapter } from './adapter.js';
|
|
5
5
|
import { getGenerateSignedURLHandler } from './generateSignedURL.js';
|
|
6
6
|
const gcsClients = new Map();
|
|
7
|
-
export const gcsStorage = (gcsStorageOptions)=>(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export const gcsStorage = (gcsStorageOptions)=>({
|
|
8
|
+
name: 'gcs',
|
|
9
|
+
collections: Object.keys(gcsStorageOptions.collections),
|
|
10
|
+
init: (incomingConfig)=>{
|
|
11
|
+
const cacheKey = gcsStorageOptions.clientCacheKey || `gcs:${gcsStorageOptions.bucket}`;
|
|
12
|
+
const getStorageClient = ()=>{
|
|
13
|
+
if (gcsClients.has(cacheKey)) {
|
|
14
|
+
return gcsClients.get(cacheKey);
|
|
15
|
+
}
|
|
16
|
+
gcsClients.set(cacheKey, new Storage(gcsStorageOptions.options));
|
|
11
17
|
return gcsClients.get(cacheKey);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
const adapter = createGcsAdapter({
|
|
17
|
-
acl: gcsStorageOptions.acl,
|
|
18
|
-
bucket: gcsStorageOptions.bucket,
|
|
19
|
-
clientUploads: gcsStorageOptions.clientUploads,
|
|
20
|
-
getStorageClient,
|
|
21
|
-
useCompositePrefixes: gcsStorageOptions.useCompositePrefixes
|
|
22
|
-
});
|
|
23
|
-
const isPluginDisabled = gcsStorageOptions.enabled === false;
|
|
24
|
-
initClientUploads({
|
|
25
|
-
clientHandler: '@payloadcms/storage-gcs/client#GcsClientUploadHandler',
|
|
26
|
-
collections: gcsStorageOptions.collections,
|
|
27
|
-
config: incomingConfig,
|
|
28
|
-
enabled: !isPluginDisabled && Boolean(gcsStorageOptions.clientUploads),
|
|
29
|
-
serverHandler: getGenerateSignedURLHandler({
|
|
30
|
-
access: typeof gcsStorageOptions.clientUploads === 'object' ? gcsStorageOptions.clientUploads.access : undefined,
|
|
18
|
+
};
|
|
19
|
+
const adapter = createGcsAdapter({
|
|
20
|
+
acl: gcsStorageOptions.acl,
|
|
31
21
|
bucket: gcsStorageOptions.bucket,
|
|
32
|
-
|
|
22
|
+
clientUploads: gcsStorageOptions.clientUploads,
|
|
33
23
|
getStorageClient,
|
|
34
24
|
useCompositePrefixes: gcsStorageOptions.useCompositePrefixes
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
25
|
+
});
|
|
26
|
+
const isPluginDisabled = gcsStorageOptions.enabled === false;
|
|
27
|
+
initClientUploads({
|
|
28
|
+
clientHandler: '@payloadcms/storage-gcs/client#GcsClientUploadHandler',
|
|
29
|
+
collections: gcsStorageOptions.collections,
|
|
30
|
+
config: incomingConfig,
|
|
31
|
+
enabled: !isPluginDisabled && Boolean(gcsStorageOptions.clientUploads),
|
|
32
|
+
serverHandler: getGenerateSignedURLHandler({
|
|
33
|
+
access: typeof gcsStorageOptions.clientUploads === 'object' ? gcsStorageOptions.clientUploads.access : undefined,
|
|
34
|
+
bucket: gcsStorageOptions.bucket,
|
|
35
|
+
collections: gcsStorageOptions.collections,
|
|
36
|
+
getStorageClient,
|
|
37
|
+
useCompositePrefixes: gcsStorageOptions.useCompositePrefixes
|
|
38
|
+
}),
|
|
39
|
+
serverHandlerPath: '/storage-gcs-generate-signed-url'
|
|
40
|
+
});
|
|
41
|
+
if (isPluginDisabled) {
|
|
42
|
+
return incomingConfig;
|
|
43
|
+
}
|
|
44
|
+
// Add adapter to each collection option object
|
|
45
|
+
const collectionsWithAdapter = Object.entries(gcsStorageOptions.collections).reduce((acc, [slug, collOptions])=>({
|
|
46
|
+
...acc,
|
|
47
|
+
[slug]: {
|
|
48
|
+
...collOptions === true ? {} : collOptions,
|
|
49
|
+
adapter
|
|
50
|
+
}
|
|
51
|
+
}), {});
|
|
52
|
+
// Set disableLocalStorage: true for collections specified in the plugin options
|
|
53
|
+
const config = {
|
|
54
|
+
...incomingConfig,
|
|
55
|
+
collections: (incomingConfig.collections || []).map((collection)=>{
|
|
56
|
+
if (!collectionsWithAdapter[collection.slug]) {
|
|
57
|
+
return collection;
|
|
61
58
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
return {
|
|
60
|
+
...collection,
|
|
61
|
+
upload: {
|
|
62
|
+
...typeof collection.upload === 'object' ? collection.upload : {},
|
|
63
|
+
disableLocalStorage: true
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
})
|
|
67
|
+
};
|
|
68
|
+
return cloudStoragePlugin({
|
|
69
|
+
alwaysInsertFields: gcsStorageOptions.alwaysInsertFields,
|
|
70
|
+
collections: collectionsWithAdapter,
|
|
71
|
+
useCompositePrefixes: gcsStorageOptions.useCompositePrefixes
|
|
72
|
+
})(config);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
71
75
|
|
|
72
76
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { StorageOptions } from '@google-cloud/storage'\nimport type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { StorageOptions } from '@google-cloud/storage'\nimport type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, StorageAdapter, UploadCollectionSlug } from 'payload'\n\nimport { Storage } from '@google-cloud/storage'\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\nimport { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport { createGcsAdapter } from './adapter.js'\nimport { getGenerateSignedURLHandler } from './generateSignedURL.js'\n\nexport interface GcsStorageOptions {\n acl?: 'Private' | 'Public'\n\n /**\n * When enabled, fields (like the prefix field) will always be inserted into\n * the collection schema regardless of whether the plugin is enabled. This\n * ensures a consistent schema across all environments.\n *\n * This will be enabled by default in Payload v4.\n *\n * @default false\n */\n alwaysInsertFields?: boolean\n\n /**\n * The name of the bucket to use.\n */\n bucket: string\n /**\n * Optional cache key to identify the GCS storage client instance.\n * If not provided, a default key will be used.\n *\n * @default `gcs:containerName`\n */\n clientCacheKey?: string\n /**\n * Do uploads directly on the client to bypass limits on Vercel. You must allow CORS PUT method for the bucket to your website.\n */\n clientUploads?: ClientUploadsConfig\n /**\n * Collection options to apply the S3 adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n /**\n * Google Cloud Storage client configuration.\n *\n * @see https://github.com/googleapis/nodejs-storage\n */\n options: StorageOptions\n\n /**\n * When true, the collection-level prefix and document-level prefix are combined\n * (compositional). When false (default), document prefix overrides collection\n * prefix entirely.\n *\n * Example:\n * - collection prefix: `collection-prefix/`\n * - document prefix: `document-prefix/`\n * - resulting prefix with useCompositePrefixes=true: `collection-prefix/document-prefix/`\n * - resulting prefix with useCompositePrefixes=false: `document-prefix/`\n *\n * @default false\n */\n useCompositePrefixes?: boolean\n}\n\ntype GcsStorageFactory = (gcsStorageArgs: GcsStorageOptions) => StorageAdapter\n\nconst gcsClients = new Map<string, Storage>()\n\nexport const gcsStorage: GcsStorageFactory = (\n gcsStorageOptions: GcsStorageOptions,\n): StorageAdapter => ({\n name: 'gcs',\n collections: Object.keys(gcsStorageOptions.collections),\n init: (incomingConfig: Config): Config => {\n const cacheKey = gcsStorageOptions.clientCacheKey || `gcs:${gcsStorageOptions.bucket}`\n\n const getStorageClient = (): Storage => {\n if (gcsClients.has(cacheKey)) {\n return gcsClients.get(cacheKey)!\n }\n gcsClients.set(cacheKey, new Storage(gcsStorageOptions.options))\n\n return gcsClients.get(cacheKey)!\n }\n\n const adapter = createGcsAdapter({\n acl: gcsStorageOptions.acl,\n bucket: gcsStorageOptions.bucket,\n clientUploads: gcsStorageOptions.clientUploads,\n getStorageClient,\n useCompositePrefixes: gcsStorageOptions.useCompositePrefixes,\n })\n\n const isPluginDisabled = gcsStorageOptions.enabled === false\n\n initClientUploads({\n clientHandler: '@payloadcms/storage-gcs/client#GcsClientUploadHandler',\n collections: gcsStorageOptions.collections,\n config: incomingConfig,\n enabled: !isPluginDisabled && Boolean(gcsStorageOptions.clientUploads),\n serverHandler: getGenerateSignedURLHandler({\n access:\n typeof gcsStorageOptions.clientUploads === 'object'\n ? gcsStorageOptions.clientUploads.access\n : undefined,\n bucket: gcsStorageOptions.bucket,\n collections: gcsStorageOptions.collections,\n getStorageClient,\n useCompositePrefixes: gcsStorageOptions.useCompositePrefixes,\n }),\n serverHandlerPath: '/storage-gcs-generate-signed-url',\n })\n\n if (isPluginDisabled) {\n return incomingConfig\n }\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 // Set disableLocalStorage: true for collections specified in the plugin options\n const config = {\n ...incomingConfig,\n collections: (incomingConfig.collections || []).map((collection) => {\n if (!collectionsWithAdapter[collection.slug]) {\n return collection\n }\n\n return {\n ...collection,\n upload: {\n ...(typeof collection.upload === 'object' ? collection.upload : {}),\n disableLocalStorage: true,\n },\n }\n }),\n }\n\n return cloudStoragePlugin({\n alwaysInsertFields: gcsStorageOptions.alwaysInsertFields,\n collections: collectionsWithAdapter,\n useCompositePrefixes: gcsStorageOptions.useCompositePrefixes,\n })(config)\n },\n})\n"],"names":["Storage","cloudStoragePlugin","initClientUploads","createGcsAdapter","getGenerateSignedURLHandler","gcsClients","Map","gcsStorage","gcsStorageOptions","name","collections","Object","keys","init","incomingConfig","cacheKey","clientCacheKey","bucket","getStorageClient","has","get","set","options","adapter","acl","clientUploads","useCompositePrefixes","isPluginDisabled","enabled","clientHandler","config","Boolean","serverHandler","access","undefined","serverHandlerPath","collectionsWithAdapter","entries","reduce","acc","slug","collOptions","map","collection","upload","disableLocalStorage","alwaysInsertFields"],"mappings":"AAQA,SAASA,OAAO,QAAQ,wBAAuB;AAC/C,SAASC,kBAAkB,QAAQ,mCAAkC;AACrE,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SAASC,gBAAgB,QAAQ,eAAc;AAC/C,SAASC,2BAA2B,QAAQ,yBAAwB;AAkEpE,MAAMC,aAAa,IAAIC;AAEvB,OAAO,MAAMC,aAAgC,CAC3CC,oBACoB,CAAA;QACpBC,MAAM;QACNC,aAAaC,OAAOC,IAAI,CAACJ,kBAAkBE,WAAW;QACtDG,MAAM,CAACC;YACL,MAAMC,WAAWP,kBAAkBQ,cAAc,IAAI,CAAC,IAAI,EAAER,kBAAkBS,MAAM,EAAE;YAEtF,MAAMC,mBAAmB;gBACvB,IAAIb,WAAWc,GAAG,CAACJ,WAAW;oBAC5B,OAAOV,WAAWe,GAAG,CAACL;gBACxB;gBACAV,WAAWgB,GAAG,CAACN,UAAU,IAAIf,QAAQQ,kBAAkBc,OAAO;gBAE9D,OAAOjB,WAAWe,GAAG,CAACL;YACxB;YAEA,MAAMQ,UAAUpB,iBAAiB;gBAC/BqB,KAAKhB,kBAAkBgB,GAAG;gBAC1BP,QAAQT,kBAAkBS,MAAM;gBAChCQ,eAAejB,kBAAkBiB,aAAa;gBAC9CP;gBACAQ,sBAAsBlB,kBAAkBkB,oBAAoB;YAC9D;YAEA,MAAMC,mBAAmBnB,kBAAkBoB,OAAO,KAAK;YAEvD1B,kBAAkB;gBAChB2B,eAAe;gBACfnB,aAAaF,kBAAkBE,WAAW;gBAC1CoB,QAAQhB;gBACRc,SAAS,CAACD,oBAAoBI,QAAQvB,kBAAkBiB,aAAa;gBACrEO,eAAe5B,4BAA4B;oBACzC6B,QACE,OAAOzB,kBAAkBiB,aAAa,KAAK,WACvCjB,kBAAkBiB,aAAa,CAACQ,MAAM,GACtCC;oBACNjB,QAAQT,kBAAkBS,MAAM;oBAChCP,aAAaF,kBAAkBE,WAAW;oBAC1CQ;oBACAQ,sBAAsBlB,kBAAkBkB,oBAAoB;gBAC9D;gBACAS,mBAAmB;YACrB;YAEA,IAAIR,kBAAkB;gBACpB,OAAOb;YACT;YAEA,+CAA+C;YAC/C,MAAMsB,yBAAmEzB,OAAO0B,OAAO,CACrF7B,kBAAkBE,WAAW,EAC7B4B,MAAM,CACN,CAACC,KAAK,CAACC,MAAMC,YAAY,GAAM,CAAA;oBAC7B,GAAGF,GAAG;oBACN,CAACC,KAAK,EAAE;wBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;wBAC3ClB;oBACF;gBACF,CAAA,GACA,CAAC;YAGH,gFAAgF;YAChF,MAAMO,SAAS;gBACb,GAAGhB,cAAc;gBACjBJ,aAAa,AAACI,CAAAA,eAAeJ,WAAW,IAAI,EAAE,AAAD,EAAGgC,GAAG,CAAC,CAACC;oBACnD,IAAI,CAACP,sBAAsB,CAACO,WAAWH,IAAI,CAAC,EAAE;wBAC5C,OAAOG;oBACT;oBAEA,OAAO;wBACL,GAAGA,UAAU;wBACbC,QAAQ;4BACN,GAAI,OAAOD,WAAWC,MAAM,KAAK,WAAWD,WAAWC,MAAM,GAAG,CAAC,CAAC;4BAClEC,qBAAqB;wBACvB;oBACF;gBACF;YACF;YAEA,OAAO5C,mBAAmB;gBACxB6C,oBAAoBtC,kBAAkBsC,kBAAkB;gBACxDpC,aAAa0B;gBACbV,sBAAsBlB,kBAAkBkB,oBAAoB;YAC9D,GAAGI;QACL;IACF,CAAA,EAAE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-gcs",
|
|
3
|
-
"version": "4.0.0-internal.
|
|
3
|
+
"version": "4.0.0-internal.c2b57ce",
|
|
4
4
|
"description": "Payload storage adapter for Google Cloud Storage",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -38,16 +38,16 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@google-cloud/storage": "7.19.0",
|
|
41
|
-
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.
|
|
41
|
+
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.c2b57ce"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"payload": "4.0.0-internal.
|
|
44
|
+
"payload": "4.0.0-internal.c2b57ce"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"payload": "4.0.0-internal.
|
|
47
|
+
"payload": "4.0.0-internal.c2b57ce"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
|
-
"node": "
|
|
50
|
+
"node": ">=24.15.0"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "pnpm build:types && pnpm build:swc",
|