@payloadcms/storage-r2 3.84.0-canary.0 → 3.84.0-internal.d5d6e43
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/R2ClientUploadHandler.d.ts.map +1 -1
- package/dist/client/R2ClientUploadHandler.js +11 -2
- package/dist/client/R2ClientUploadHandler.js.map +1 -1
- package/dist/deleteFile.js +2 -2
- package/dist/deleteFile.js.map +1 -1
- package/dist/getFile.d.ts.map +1 -1
- package/dist/getFile.js +6 -7
- package/dist/getFile.js.map +1 -1
- package/dist/handleMultiPartUpload.d.ts +2 -1
- package/dist/handleMultiPartUpload.d.ts.map +1 -1
- package/dist/handleMultiPartUpload.js +9 -6
- package/dist/handleMultiPartUpload.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/uploadFile.js +3 -3
- package/dist/uploadFile.js.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"R2ClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/R2ClientUploadHandler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"R2ClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/R2ClientUploadHandler.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAGV,kCAAkC,EAGnC,MAAM,aAAa,CAAA;AAEpB,eAAO,MAAM,qBAAqB;;;;;;;aAiB7B,OAAO,aAgEV,CAAA"}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client';
|
|
3
|
+
import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
3
4
|
import { formatAdminURL } from 'payload/shared';
|
|
4
5
|
export const R2ClientUploadHandler = createClientUploadHandler({
|
|
5
|
-
handler: async ({ apiRoute, collectionSlug, extra: { chunkSize = 5 * 1024 * 1024
|
|
6
|
+
handler: async ({ apiRoute, collectionSlug, docPrefix, extra: { chunkSize = 5 * 1024 * 1024 }, file, prefix, serverHandlerPath, serverURL })=>{
|
|
7
|
+
const { sanitizedDocPrefix } = getFileKey({
|
|
8
|
+
collectionPrefix: prefix,
|
|
9
|
+
docPrefix,
|
|
10
|
+
filename: file.name
|
|
11
|
+
});
|
|
6
12
|
const params = {
|
|
7
13
|
collection: collectionSlug,
|
|
14
|
+
docPrefix: sanitizedDocPrefix,
|
|
8
15
|
fileName: file.name,
|
|
9
16
|
fileType: file.type
|
|
10
17
|
};
|
|
@@ -14,6 +21,7 @@ export const R2ClientUploadHandler = createClientUploadHandler({
|
|
|
14
21
|
serverURL
|
|
15
22
|
});
|
|
16
23
|
const endpoint = `${baseURL}?${String(new URLSearchParams(params))}`;
|
|
24
|
+
// upload the file directly to R2 using the signed URL
|
|
17
25
|
const multipart = await fetch(endpoint, {
|
|
18
26
|
method: 'POST'
|
|
19
27
|
});
|
|
@@ -59,7 +67,8 @@ export const R2ClientUploadHandler = createClientUploadHandler({
|
|
|
59
67
|
}
|
|
60
68
|
const key = await complete.text();
|
|
61
69
|
return {
|
|
62
|
-
key
|
|
70
|
+
key,
|
|
71
|
+
prefix: sanitizedDocPrefix
|
|
63
72
|
};
|
|
64
73
|
}
|
|
65
74
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/R2ClientUploadHandler.ts"],"sourcesContent":["'use client'\n\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { formatAdminURL } from 'payload/shared'\n\nimport type {\n R2MultipartUpload,\n R2StorageClientUploadContext,\n R2StorageClientUploadHandlerParams,\n R2StorageMultipartUploadHandlerParams,\n R2UploadedPart,\n} from '../types.js'\n\nexport const R2ClientUploadHandler = createClientUploadHandler<R2StorageClientUploadHandlerParams>({\n handler: async ({\n apiRoute,\n collectionSlug,\n extra: { chunkSize = 5 * 1024 * 1024
|
|
1
|
+
{"version":3,"sources":["../../src/client/R2ClientUploadHandler.ts"],"sourcesContent":["'use client'\n\nimport { createClientUploadHandler } from '@payloadcms/plugin-cloud-storage/client'\nimport { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\nimport { formatAdminURL } from 'payload/shared'\n\nimport type {\n R2MultipartUpload,\n R2StorageClientUploadContext,\n R2StorageClientUploadHandlerParams,\n R2StorageMultipartUploadHandlerParams,\n R2UploadedPart,\n} from '../types.js'\n\nexport const R2ClientUploadHandler = createClientUploadHandler<R2StorageClientUploadHandlerParams>({\n handler: async ({\n apiRoute,\n collectionSlug,\n docPrefix,\n extra: { chunkSize = 5 * 1024 * 1024 },\n file,\n prefix,\n serverHandlerPath,\n serverURL,\n }): Promise<R2StorageClientUploadContext | undefined> => {\n const { sanitizedDocPrefix } = getFileKey({\n collectionPrefix: prefix,\n docPrefix,\n filename: file.name,\n })\n\n const params: R2StorageMultipartUploadHandlerParams = {\n collection: collectionSlug,\n docPrefix: sanitizedDocPrefix,\n fileName: file.name,\n fileType: file.type,\n }\n const baseURL = formatAdminURL({\n apiRoute,\n path: serverHandlerPath,\n serverURL,\n })\n\n const endpoint = `${baseURL}?${String(new URLSearchParams(params))}`\n\n // upload the file directly to R2 using the signed URL\n const multipart = await fetch(endpoint, { method: 'POST' })\n if (!multipart.ok) {\n throw new Error('Failed to initialize multipart upload')\n }\n\n const multipartUpload = (await multipart.json()) as Pick<R2MultipartUpload, 'key' | 'uploadId'>\n const multipartUploadedParts: R2UploadedPart[] = []\n\n params.multipartId = multipartUpload.uploadId\n params.multipartKey = multipartUpload.key\n\n const partTotal = Math.ceil(file.size / chunkSize)\n\n for (let part = 1; part <= partTotal; part++) {\n const bytesEnd = Math.min(part * chunkSize, file.size)\n const bytesStart = (part - 1) * chunkSize\n\n params.multipartNumber = String(part)\n\n const body = file.slice(bytesStart, bytesEnd)\n const headers = {\n 'Content-Length': String(body.size),\n 'Content-Type': 'application/octet-stream',\n }\n const uploaded = await fetch(endpoint, { body, headers, method: 'POST' })\n if (!uploaded.ok) {\n throw new Error(`Failed to upload part ${part} / ${partTotal}`)\n }\n\n multipartUploadedParts.push((await uploaded.json()) as R2UploadedPart)\n\n if (part === partTotal) {\n delete params.multipartNumber\n\n const body = JSON.stringify(multipartUploadedParts)\n const headers = { 'Content-Type': 'application/json' }\n const complete = await fetch(endpoint, { body, headers, method: 'POST' })\n if (!complete.ok) {\n throw new Error(`Failed to complete multipart upload`)\n }\n\n const key = await complete.text()\n return {\n key,\n prefix: sanitizedDocPrefix,\n }\n }\n }\n },\n})\n"],"names":["createClientUploadHandler","getFileKey","formatAdminURL","R2ClientUploadHandler","handler","apiRoute","collectionSlug","docPrefix","extra","chunkSize","file","prefix","serverHandlerPath","serverURL","sanitizedDocPrefix","collectionPrefix","filename","name","params","collection","fileName","fileType","type","baseURL","path","endpoint","String","URLSearchParams","multipart","fetch","method","ok","Error","multipartUpload","json","multipartUploadedParts","multipartId","uploadId","multipartKey","key","partTotal","Math","ceil","size","part","bytesEnd","min","bytesStart","multipartNumber","body","slice","headers","uploaded","push","JSON","stringify","complete","text"],"mappings":"AAAA;AAEA,SAASA,yBAAyB,QAAQ,0CAAyC;AACnF,SAASC,UAAU,QAAQ,6CAA4C;AACvE,SAASC,cAAc,QAAQ,iBAAgB;AAU/C,OAAO,MAAMC,wBAAwBH,0BAA8D;IACjGI,SAAS,OAAO,EACdC,QAAQ,EACRC,cAAc,EACdC,SAAS,EACTC,OAAO,EAAEC,YAAY,IAAI,OAAO,IAAI,EAAE,EACtCC,IAAI,EACJC,MAAM,EACNC,iBAAiB,EACjBC,SAAS,EACV;QACC,MAAM,EAAEC,kBAAkB,EAAE,GAAGb,WAAW;YACxCc,kBAAkBJ;YAClBJ;YACAS,UAAUN,KAAKO,IAAI;QACrB;QAEA,MAAMC,SAAgD;YACpDC,YAAYb;YACZC,WAAWO;YACXM,UA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AMC,OAAOK,KAAKC,SAAS,CAACpB;gBAC5B,MAAMgB,UAAU;oBAAE,gBAAgB;gBAAmB;gBACrD,MAAMK,WAAW,MAAM3B,MAAMJ,UAAU;oBAAEwB;oBAAME;oBAASrB,QAAQ;gBAAO;gBACvE,IAAI,CAAC0B,SAASzB,EAAE,EAAE;oBAChB,MAAM,IAAIC,MAAM,CAAC,mCAAmC,CAAC;gBACvD;gBAEA,MAAMO,MAAM,MAAMiB,SAASC,IAAI;gBAC/B,OAAO;oBACLlB;oBACA5B,QAAQG;gBACV;YACF;QACF;IACF;AACF,GAAE"}
|
package/dist/deleteFile.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
2
|
export async function deleteFile({ bucket, collectionPrefix = '', docPrefix, filename, useCompositePrefixes = false }) {
|
|
3
|
-
const
|
|
3
|
+
const { fileKey } = getFileKey({
|
|
4
4
|
collectionPrefix,
|
|
5
5
|
docPrefix,
|
|
6
6
|
filename,
|
|
7
7
|
useCompositePrefixes
|
|
8
8
|
});
|
|
9
|
-
await bucket.delete(
|
|
9
|
+
await bucket.delete(fileKey);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
//# sourceMappingURL=deleteFile.js.map
|
package/dist/deleteFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/deleteFile.ts"],"sourcesContent":["import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket } from './types.js'\n\ninterface DeleteFileArgs {\n bucket: R2Bucket\n collectionPrefix?: string\n docPrefix: string\n filename: string\n useCompositePrefixes?: boolean\n}\n\nexport async function deleteFile({\n bucket,\n collectionPrefix = '',\n docPrefix,\n filename,\n useCompositePrefixes = false,\n}: DeleteFileArgs): Promise<void> {\n const
|
|
1
|
+
{"version":3,"sources":["../src/deleteFile.ts"],"sourcesContent":["import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket } from './types.js'\n\ninterface DeleteFileArgs {\n bucket: R2Bucket\n collectionPrefix?: string\n docPrefix: string\n filename: string\n useCompositePrefixes?: boolean\n}\n\nexport async function deleteFile({\n bucket,\n collectionPrefix = '',\n docPrefix,\n filename,\n useCompositePrefixes = false,\n}: DeleteFileArgs): Promise<void> {\n const { fileKey } = getFileKey({\n collectionPrefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n\n await bucket.delete(fileKey)\n}\n"],"names":["getFileKey","deleteFile","bucket","collectionPrefix","docPrefix","filename","useCompositePrefixes","fileKey","delete"],"mappings":"AAAA,SAASA,UAAU,QAAQ,6CAA4C;AAYvE,OAAO,eAAeC,WAAW,EAC/BC,MAAM,EACNC,mBAAmB,EAAE,EACrBC,SAAS,EACTC,QAAQ,EACRC,uBAAuB,KAAK,EACb;IACf,MAAM,EAAEC,OAAO,EAAE,GAAGP,WAAW;QAC7BG;QACAC;QACAC;QACAC;IACF;IAEA,MAAMJ,OAAOM,MAAM,CAACD;AACtB"}
|
package/dist/getFile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFile.d.ts","sourceRoot":"","sources":["../src/getFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"getFile.d.ts","sourceRoot":"","sources":["../src/getFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAQ/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAE1C,UAAU,WAAW;IACnB,MAAM,EAAE,QAAQ,CAAA;IAChB,mBAAmB,CAAC,EAAE,OAAO,CAAA;IAC7B,UAAU,EAAE,gBAAgB,CAAA;IAC5B,QAAQ,EAAE,MAAM,CAAA;IAChB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,GAAG,EAAE,cAAc,CAAA;IACnB,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAID,wBAAsB,OAAO,CAAC,EAC5B,MAAM,EACN,mBAAmB,EACnB,UAAU,EACV,QAAQ,EACR,eAAe,EACf,MAAW,EACX,gBAAgB,EAChB,GAAG,EACH,oBAA4B,GAC7B,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsHjC"}
|
package/dist/getFile.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { getFilePrefix as getDocPrefix, getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
2
|
import { getRangeRequestInfo } from 'payload/internal';
|
|
3
|
-
import { sanitizeFilename } from 'payload/shared';
|
|
4
3
|
const isMiniflare = process.env.NODE_ENV === 'development';
|
|
5
4
|
export async function getFile({ bucket, clientUploadContext, collection, filename, incomingHeaders, prefix = '', prefixQueryParam, req, useCompositePrefixes = false }) {
|
|
6
5
|
try {
|
|
@@ -11,14 +10,14 @@ export async function getFile({ bucket, clientUploadContext, collection, filenam
|
|
|
11
10
|
prefixQueryParam,
|
|
12
11
|
req
|
|
13
12
|
});
|
|
14
|
-
const
|
|
13
|
+
const { fileKey } = getFileKey({
|
|
15
14
|
collectionPrefix: prefix,
|
|
16
15
|
docPrefix,
|
|
17
|
-
filename
|
|
16
|
+
filename,
|
|
18
17
|
useCompositePrefixes
|
|
19
18
|
});
|
|
20
19
|
// Get file size for range validation
|
|
21
|
-
const headObj = await bucket?.head(
|
|
20
|
+
const headObj = await bucket?.head(fileKey);
|
|
22
21
|
if (!headObj) {
|
|
23
22
|
return new Response(null, {
|
|
24
23
|
status: 404,
|
|
@@ -47,12 +46,12 @@ export async function getFile({ bucket, clientUploadContext, collection, filenam
|
|
|
47
46
|
// Get object with range if needed
|
|
48
47
|
// Due to https://github.com/cloudflare/workers-sdk/issues/6047
|
|
49
48
|
// We cannot send a Headers instance to Miniflare
|
|
50
|
-
const obj = rangeResult.type === 'partial' && !isMiniflare ? await bucket?.get(
|
|
49
|
+
const obj = rangeResult.type === 'partial' && !isMiniflare ? await bucket?.get(fileKey, {
|
|
51
50
|
range: {
|
|
52
51
|
length: rangeResult.rangeEnd - rangeResult.rangeStart + 1,
|
|
53
52
|
offset: rangeResult.rangeStart
|
|
54
53
|
}
|
|
55
|
-
}) : await bucket?.get(
|
|
54
|
+
}) : await bucket?.get(fileKey);
|
|
56
55
|
if (!obj || obj.body == undefined) {
|
|
57
56
|
return new Response(null, {
|
|
58
57
|
status: 404,
|
|
@@ -107,7 +106,7 @@ export async function getFile({ bucket, clientUploadContext, collection, filenam
|
|
|
107
106
|
headers,
|
|
108
107
|
status: rangeResult.status
|
|
109
108
|
});
|
|
110
|
-
} catch (
|
|
109
|
+
} catch (_err) {
|
|
111
110
|
return new Response('Internal Server Error', {
|
|
112
111
|
status: 500
|
|
113
112
|
});
|
package/dist/getFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/getFile.ts"],"sourcesContent":["import type { CollectionConfig, PayloadRequest } from 'payload'\n\nimport {\n getFilePrefix as getDocPrefix,\n getFileKey,\n} from '@payloadcms/plugin-cloud-storage/utilities'\nimport { getRangeRequestInfo } from 'payload/internal'\
|
|
1
|
+
{"version":3,"sources":["../src/getFile.ts"],"sourcesContent":["import type { CollectionConfig, PayloadRequest } from 'payload'\n\nimport {\n getFilePrefix as getDocPrefix,\n getFileKey,\n} from '@payloadcms/plugin-cloud-storage/utilities'\nimport { getRangeRequestInfo } from 'payload/internal'\n\nimport type { R2Bucket } from './types.js'\n\ninterface GetFileArgs {\n bucket: R2Bucket\n clientUploadContext?: unknown\n collection: CollectionConfig\n filename: string\n incomingHeaders?: Headers\n prefix: string\n prefixQueryParam?: string\n req: PayloadRequest\n useCompositePrefixes?: boolean\n}\n\nconst isMiniflare = process.env.NODE_ENV === 'development'\n\nexport async function getFile({\n bucket,\n clientUploadContext,\n collection,\n filename,\n incomingHeaders,\n prefix = '',\n prefixQueryParam,\n req,\n useCompositePrefixes = false,\n}: GetFileArgs): Promise<Response> {\n try {\n const docPrefix = await getDocPrefix({\n clientUploadContext,\n collection,\n filename,\n prefixQueryParam,\n req,\n })\n\n const { fileKey } = getFileKey({\n collectionPrefix: prefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n\n // Get file size for range validation\n const headObj = await bucket?.head(fileKey)\n if (!headObj) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n const fileSize = headObj.size\n\n // Don't return large file uploads back to the client, or the Worker will run out of memory\n if (fileSize > 50 * 1024 * 1024 && clientUploadContext) {\n return new Response(null, { status: 200 })\n }\n\n // Handle range request\n const rangeHeader = req.headers.get('range')\n const rangeResult = getRangeRequestInfo({ fileSize, rangeHeader })\n\n if (rangeResult.type === 'invalid') {\n return new Response(null, {\n headers: new Headers(rangeResult.headers),\n status: rangeResult.status,\n })\n }\n\n // Get object with range if needed\n // Due to https://github.com/cloudflare/workers-sdk/issues/6047\n // We cannot send a Headers instance to Miniflare\n const obj =\n rangeResult.type === 'partial' && !isMiniflare\n ? await bucket?.get(fileKey, {\n range: {\n length: rangeResult.rangeEnd - rangeResult.rangeStart + 1,\n offset: rangeResult.rangeStart,\n },\n })\n : await bucket?.get(fileKey)\n\n if (!obj || obj.body == undefined) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n let headers = new Headers(incomingHeaders)\n\n // Add range-related headers from the result\n for (const [headerKey, value] of Object.entries(rangeResult.headers)) {\n headers.append(headerKey, value)\n }\n\n // Add R2-specific headers\n if (isMiniflare) {\n // In development with Miniflare, manually set headers from httpMetadata\n const metadata = obj.httpMetadata\n if (metadata?.cacheControl) {\n headers.set('Cache-Control', metadata.cacheControl)\n }\n if (metadata?.contentDisposition) {\n headers.set('Content-Disposition', metadata.contentDisposition)\n }\n if (metadata?.contentEncoding) {\n headers.set('Content-Encoding', metadata.contentEncoding)\n }\n if (metadata?.contentLanguage) {\n headers.set('Content-Language', metadata.contentLanguage)\n }\n if (metadata?.contentType) {\n headers.set('Content-Type', metadata.contentType)\n }\n } else {\n obj.writeHttpMetadata(headers)\n }\n\n // Add Content-Security-Policy header for SVG files to prevent executable code\n const contentType = headers.get('Content-Type')\n if (contentType === 'image/svg+xml') {\n headers.set('Content-Security-Policy', \"script-src 'none'\")\n }\n\n const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match')\n\n if (\n collection.upload &&\n typeof collection.upload === 'object' &&\n typeof collection.upload.modifyResponseHeaders === 'function'\n ) {\n headers = collection.upload.modifyResponseHeaders({ headers }) || headers\n }\n\n if (etagFromHeaders && etagFromHeaders === obj.etag) {\n return new Response(null, {\n headers,\n status: 304,\n })\n }\n\n return new Response(obj.body, {\n headers,\n status: rangeResult.status,\n })\n } catch (_err: unknown) {\n return new Response('Internal Server Error', { status: 500 })\n }\n}\n"],"names":["getFilePrefix","getDocPrefix","getFileKey","getRangeRequestInfo","isMiniflare","process","env","NODE_ENV","getFile","bucket","clientUploadContext","collection","filename","incomingHeaders","prefix","prefixQueryParam","req","useCompositePrefixes","docPrefix","fileKey","collectionPrefix","headObj","head","Response","status","statusText","fileSize","size","rangeHeader","headers","get","rangeResult","type","Headers","obj","range","length","rangeEnd","rangeStart","offset","body","undefined","headerKey","value","Object","entries","append","metadata","httpMetadata","cacheControl","set","contentDisposition","contentEncoding","contentLanguage","contentType","writeHttpMetadata","etagFromHeaders","upload","modifyResponseHeaders","etag","_err"],"mappings":"AAEA,SACEA,iBAAiBC,YAAY,EAC7BC,UAAU,QACL,6CAA4C;AACnD,SAASC,mBAAmB,QAAQ,mBAAkB;AAgBtD,MAAMC,cAAcC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAE7C,OAAO,eAAeC,QAAQ,EAC5BC,MAAM,EACNC,mBAAmB,EACnBC,UAAU,EACVC,QAAQ,EAC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OAAO,IAAIpC,SAAS,MAAM;gBACxBM;gBACAL,QAAQ;YACV;QACF;QAEA,OAAO,IAAID,SAASW,IAAIM,IAAI,EAAE;YAC5BX;YACAL,QAAQO,YAAYP,MAAM;QAC5B;IACF,EAAE,OAAOoC,MAAe;QACtB,OAAO,IAAIrC,SAAS,yBAAyB;YAAEC,QAAQ;QAAI;IAC7D;AACF"}
|
|
@@ -6,7 +6,8 @@ type Args = {
|
|
|
6
6
|
access?: ClientUploadsAccess;
|
|
7
7
|
bucket: R2Bucket;
|
|
8
8
|
collections: R2StorageOptions['collections'];
|
|
9
|
+
useCompositePrefixes?: boolean;
|
|
9
10
|
};
|
|
10
|
-
export declare const getHandleMultiPartUpload: ({ access, bucket, collections }: Args) => PayloadHandler;
|
|
11
|
+
export declare const getHandleMultiPartUpload: ({ access, bucket, collections, useCompositePrefixes }: Args) => PayloadHandler;
|
|
11
12
|
export {};
|
|
12
13
|
//# sourceMappingURL=handleMultiPartUpload.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleMultiPartUpload.d.ts","sourceRoot":"","sources":["../src/handleMultiPartUpload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"handleMultiPartUpload.d.ts","sourceRoot":"","sources":["../src/handleMultiPartUpload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAyC,MAAM,YAAY,CAAA;AAEjF,KAAK,IAAI,GAAG;IACV,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,MAAM,EAAE,QAAQ,CAAA;IAChB,WAAW,EAAE,gBAAgB,CAAC,aAAa,CAAC,CAAA;IAC5C,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,CAAA;AAGD,eAAO,MAAM,wBAAwB,0DAC6B,IAAI,KAAG,cA4EtE,CAAA"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
2
|
import { APIError, Forbidden } from 'payload';
|
|
3
|
-
import { sanitizeFilename } from 'payload/shared';
|
|
4
3
|
// Adapted from https://developers.cloudflare.com/r2/api/workers/workers-multipart-usage/
|
|
5
|
-
export const getHandleMultiPartUpload = ({ access, bucket, collections })=>async (req)=>{
|
|
4
|
+
export const getHandleMultiPartUpload = ({ access, bucket, collections, useCompositePrefixes = false })=>async (req)=>{
|
|
6
5
|
const params = Object.fromEntries(req.searchParams);
|
|
7
6
|
const collectionSlug = params.collection;
|
|
8
7
|
const filetype = params.fileType;
|
|
@@ -37,9 +36,13 @@ export const getHandleMultiPartUpload = ({ access, bucket, collections })=>async
|
|
|
37
36
|
throw new Forbidden(req.t);
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
39
|
+
const collectionPrefix = typeof collectionConfig === 'object' && collectionConfig.prefix || '';
|
|
40
|
+
const { fileKey } = getFileKey({
|
|
41
|
+
collectionPrefix,
|
|
42
|
+
docPrefix: params.docPrefix ?? undefined,
|
|
43
|
+
filename: params.fileName,
|
|
44
|
+
useCompositePrefixes
|
|
45
|
+
});
|
|
43
46
|
const multipartId = params.multipartId;
|
|
44
47
|
const multipartKey = params.multipartKey;
|
|
45
48
|
const multipartNumber = parseInt(params.multipartNumber || '');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/handleMultiPartUpload.ts"],"sourcesContent":["import type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport
|
|
1
|
+
{"version":3,"sources":["../src/handleMultiPartUpload.ts"],"sourcesContent":["import type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\nimport { APIError, Forbidden } from 'payload'\n\nimport type { R2StorageOptions } from './index.js'\nimport type { R2Bucket, R2StorageMultipartUploadHandlerParams } from './types.js'\n\ntype Args = {\n access?: ClientUploadsAccess\n bucket: R2Bucket\n collections: R2StorageOptions['collections']\n useCompositePrefixes?: boolean\n}\n\n// Adapted from https://developers.cloudflare.com/r2/api/workers/workers-multipart-usage/\nexport const getHandleMultiPartUpload =\n ({ access, bucket, collections, useCompositePrefixes = false }: Args): PayloadHandler =>\n async (req) => {\n const params = Object.fromEntries(req.searchParams) as R2StorageMultipartUploadHandlerParams\n const collectionSlug = params.collection\n const filetype = params.fileType\n\n const collectionConfig = collections[collectionSlug]\n if (!collectionConfig) {\n throw new APIError(`Collection ${collectionSlug} was not found in R2 Storage options`)\n }\n\n // Check custom access if provided, otherwise check collection's create access\n if (access) {\n if (!(await access({ collectionSlug, req }))) {\n throw new Forbidden(req.t)\n }\n } else {\n // Use the collection's create access control\n const collection = req.payload.collections[collectionSlug]\n if (!collection) {\n throw new APIError(`Collection ${collectionSlug} not found`)\n }\n\n const createAccess = collection.config.access?.create\n if (createAccess) {\n const hasAccess = await createAccess({ req })\n if (!hasAccess) {\n throw new Forbidden(req.t)\n }\n } else if (!req.user) {\n // No custom access and no user - deny by default\n throw new Forbidden(req.t)\n }\n }\n\n const collectionPrefix = (typeof collectionConfig === 'object' && collectionConfig.prefix) || ''\n const { fileKey } = getFileKey({\n collectionPrefix,\n docPrefix: params.docPrefix ?? undefined,\n filename: params.fileName,\n useCompositePrefixes,\n })\n\n const multipartId = params.multipartId\n const multipartKey = params.multipartKey\n const multipartNumber = parseInt(params.multipartNumber || '')\n\n if (multipartId && multipartKey) {\n const multipartUpload = bucket.resumeMultipartUpload(multipartKey, multipartId)\n const request = req as Request\n\n if (isNaN(multipartNumber)) {\n // Upload complete\n const object = await multipartUpload.complete((await request.json()) as any)\n return new Response(object.key, { status: 200 })\n } else {\n // Upload part\n const uploadedPart = await multipartUpload.uploadPart(\n multipartNumber,\n await request.arrayBuffer(),\n )\n return Response.json(uploadedPart)\n }\n } else {\n // Create multipart upload\n const multipartUpload = await bucket.createMultipartUpload(fileKey, {\n httpMetadata: {\n contentType: filetype,\n },\n })\n\n return Response.json({\n key: multipartUpload.key,\n uploadId: multipartUpload.uploadId,\n })\n }\n }\n"],"names":["getFileKey","APIError","Forbidden","getHandleMultiPartUpload","access","bucket","collections","useCompositePrefixes","req","params","Object","fromEntries","searchParams","collectionSlug","collection","filetype","fileType","collectionConfig","t","payload","createAccess","config","create","hasAccess","user","collectionPrefix","prefix","fileKey","docPrefix","undefined","filename","fileName","multipartId","multipartKey","multipartNumber","parseInt","multipartUpload","resumeMultipartUpload","request","isNaN","object","complete","json","Response","key","status","uploadedPart","uploadPart","arrayBuffer","createMultipartUpload","httpMetadata","contentType","uploadId"],"mappings":"AAGA,SAASA,UAAU,QAAQ,6CAA4C;AACvE,SAASC,QAAQ,EAAEC,SAAS,QAAQ,UAAS;AAY7C,yFAAyF;AACzF,OAAO,MAAMC,2BACX,CAAC,EAAEC,MAAM,EAAEC,MAAM,EAAEC,WAAW,EAAEC,uBAAuB,KAAK,EAAQ,GACpE,OAAOC;QACL,MAAMC,SAASC,OAAOC,WAAW,CAACH,IAAII,YAAY;QAClD,MAAMC,iBAAiBJ,OAAOK,UAAU;QACxC,MAAMC,WAAWN,OAAOO,QAAQ;QAEhC,MAAMC,mBAAmBX,WAAW,CAACO,eAAe;QACpD,IAAI,CAACI,kBAAkB;YACrB,MAAM,IAAIhB,SAAS,CAAC,WAAW,EAAEY,eAAe,oCAAoC,CAAC;QACvF;QAEA,8EAA8E;QAC9E,IAAIT,QAAQ;YACV,IA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"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,MAAM,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAKnE,OAAO,KAAK,EAAE,QAAQ,EAAsC,MAAM,YAAY,CAAA;AAK9E,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAE5B,MAAM,EAAE,QAAQ,CAAA;IAChB;;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,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;;;;;;;;OAYG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,KAAK,eAAe,GAAG,CAAC,aAAa,EAAE,gBAAgB,KAAK,MAAM,CAAA;AAElE,eAAO,MAAM,SAAS,EAAE,eAwErB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -14,13 +14,11 @@ export const r2Storage = (r2StorageOptions)=>(incomingConfig)=>{
|
|
|
14
14
|
collections: r2StorageOptions.collections,
|
|
15
15
|
config: incomingConfig,
|
|
16
16
|
enabled: !isPluginDisabled && Boolean(r2StorageOptions.clientUploads),
|
|
17
|
-
extraClientHandlerProps: (collection)=>({
|
|
18
|
-
prefix: typeof collection === 'object' && collection.prefix && `${collection.prefix}/` || ''
|
|
19
|
-
}),
|
|
20
17
|
serverHandler: getHandleMultiPartUpload({
|
|
21
18
|
access: typeof r2StorageOptions.clientUploads === 'object' ? r2StorageOptions.clientUploads.access : undefined,
|
|
22
19
|
bucket: r2StorageOptions.bucket,
|
|
23
|
-
collections: r2StorageOptions.collections
|
|
20
|
+
collections: r2StorageOptions.collections,
|
|
21
|
+
useCompositePrefixes: r2StorageOptions.useCompositePrefixes
|
|
24
22
|
}),
|
|
25
23
|
serverHandlerPath: '/storage-r2-multi-part-upload'
|
|
26
24
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, Plugin, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\nimport { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket, R2StorageClientUploadHandlerParams } from './types.js'\n\nimport { createR2Adapter } from './adapter.js'\nimport { getHandleMultiPartUpload } from './handleMultiPartUpload.js'\n\nexport interface R2StorageOptions {\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 bucket: R2Bucket\n /**\n * Do uploads directly on the client, to bypass limits on Cloudflare/Vercel.\n */\n clientUploads?: ClientUploadsConfig\n /**\n * Collection options to apply the R2 adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n enabled?: boolean\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 R2StoragePlugin = (r2StorageArgs: R2StorageOptions) => Plugin\n\nexport const r2Storage: R2StoragePlugin =\n (r2StorageOptions) =>\n (incomingConfig: Config): Config => {\n const adapter = createR2Adapter({\n bucket: r2StorageOptions.bucket,\n clientUploads: r2StorageOptions.clientUploads,\n useCompositePrefixes: r2StorageOptions.useCompositePrefixes,\n })\n\n const isPluginDisabled = r2StorageOptions.enabled === false\n\n initClientUploads<\n R2StorageClientUploadHandlerParams,\n R2StorageOptions['collections'][keyof R2StorageOptions['collections']]\n >({\n clientHandler: '@payloadcms/storage-r2/client#R2ClientUploadHandler',\n collections: r2StorageOptions.collections,\n config: incomingConfig,\n enabled: !isPluginDisabled && Boolean(r2StorageOptions.clientUploads),\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n} from '@payloadcms/plugin-cloud-storage/types'\nimport type { Config, Plugin, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\nimport { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket, R2StorageClientUploadHandlerParams } from './types.js'\n\nimport { createR2Adapter } from './adapter.js'\nimport { getHandleMultiPartUpload } from './handleMultiPartUpload.js'\n\nexport interface R2StorageOptions {\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 bucket: R2Bucket\n /**\n * Do uploads directly on the client, to bypass limits on Cloudflare/Vercel.\n */\n clientUploads?: ClientUploadsConfig\n /**\n * Collection options to apply the R2 adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n enabled?: boolean\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 R2StoragePlugin = (r2StorageArgs: R2StorageOptions) => Plugin\n\nexport const r2Storage: R2StoragePlugin =\n (r2StorageOptions) =>\n (incomingConfig: Config): Config => {\n const adapter = createR2Adapter({\n bucket: r2StorageOptions.bucket,\n clientUploads: r2StorageOptions.clientUploads,\n useCompositePrefixes: r2StorageOptions.useCompositePrefixes,\n })\n\n const isPluginDisabled = r2StorageOptions.enabled === false\n\n initClientUploads<\n R2StorageClientUploadHandlerParams,\n R2StorageOptions['collections'][keyof R2StorageOptions['collections']]\n >({\n clientHandler: '@payloadcms/storage-r2/client#R2ClientUploadHandler',\n collections: r2StorageOptions.collections,\n config: incomingConfig,\n enabled: !isPluginDisabled && Boolean(r2StorageOptions.clientUploads),\n serverHandler: getHandleMultiPartUpload({\n access:\n typeof r2StorageOptions.clientUploads === 'object'\n ? r2StorageOptions.clientUploads.access\n : undefined,\n bucket: r2StorageOptions.bucket,\n collections: r2StorageOptions.collections,\n useCompositePrefixes: r2StorageOptions.useCompositePrefixes,\n }),\n serverHandlerPath: '/storage-r2-multi-part-upload',\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 r2StorageOptions.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: r2StorageOptions.alwaysInsertFields,\n collections: collectionsWithAdapter,\n useCompositePrefixes: r2StorageOptions.useCompositePrefixes,\n })(config)\n }\n"],"names":["cloudStoragePlugin","initClientUploads","createR2Adapter","getHandleMultiPartUpload","r2Storage","r2StorageOptions","incomingConfig","adapter","bucket","clientUploads","useCompositePrefixes","isPluginDisabled","enabled","clientHandler","collections","config","Boolean","serverHandler","access","undefined","serverHandlerPath","collectionsWithAdapter","Object","entries","reduce","acc","slug","collOptions","map","collection","upload","disableLocalStorage","alwaysInsertFields"],"mappings":"AAOA,SAASA,kBAAkB,QAAQ,mCAAkC;AACrE,SAASC,iBAAiB,QAAQ,6CAA4C;AAI9E,SAASC,eAAe,QAAQ,eAAc;AAC9C,SAASC,wBAAwB,QAAQ,6BAA4B;AA0CrE,OAAO,MAAMC,YACX,CAACC,mBACD,CAACC;QACC,MAAMC,UAAUL,gBAAgB;YAC9BM,QAAQH,iBAAiBG,MAAM;YAC/BC,eAAeJ,iBAAiBI,aAAa;YAC7CC,sBAAsBL,iBAAiBK,oBAAoB;QAC7D;QAEA,MAAMC,mBAAmBN,iBAAiBO,OAAO,KAAK;QAEtDX,kBAGE;YACAY,eAAe;YACfC,aAAaT,iBAAiBS,WAAW;YACzCC,QAAQT;YACRM,SAAS,CAACD,oBAAoBK,QAAQX,iBAAiBI,aAAa;YACpEQ,eAAed,yBAAyB;gBACtCe,QACE,OAAOb,iBAAiBI,aAAa,KAAK,WACtCJ,iBAAiBI,aAAa,CAACS,MAAM,GACrCC;gBACNX,QAAQH,iBAAiBG,MAAM;gBAC/BM,aAAaT,iBAAiBS,WAAW;gBACzCJ,sBAAsBL,iBAAiBK,oBAAoB;YAC7D;YACAU,mBAAmB;QACrB;QAEA,IAAIT,kBAAkB;YACpB,OAAOL;QACT;QAEA,+CAA+C;QAC/C,MAAMe,yBAAmEC,OAAOC,OAAO,CACrFlB,iBAAiBS,WAAW,EAC5BU,MAAM,CACN,CAACC,KAAK,CAACC,MAAMC,YAAY,GAAM,CAAA;gBAC7B,GAAGF,GAAG;gBACN,CAACC,KAAK,EAAE;oBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;oBAC3CpB;gBACF;YACF,CAAA,GACA,CAAC;QAGH,gFAAgF;QAChF,MAAMQ,SAAS;YACb,GAAGT,cAAc;YACjBQ,aAAa,AAACR,CAAAA,eAAeQ,WAAW,IAAI,EAAE,AAAD,EAAGc,GAAG,CAAC,CAACC;gBACnD,IAAI,CAACR,sBAAsB,CAACQ,WAAWH,IAAI,CAAC,EAAE;oBAC5C,OAAOG;gBACT;gBAEA,OAAO;oBACL,GAAGA,UAAU;oBACbC,QAAQ;wBACN,GAAI,OAAOD,WAAWC,MAAM,KAAK,WAAWD,WAAWC,MAAM,GAAG,CAAC,CAAC;wBAClEC,qBAAqB;oBACvB;gBACF;YACF;QACF;QAEA,OAAO/B,mBAAmB;YACxBgC,oBAAoB3B,iBAAiB2B,kBAAkB;YACvDlB,aAAaO;YACbX,sBAAsBL,iBAAiBK,oBAAoB;QAC7D,GAAGK;IACL,EAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -50,13 +50,14 @@ export interface R2UploadedPart {
|
|
|
50
50
|
}
|
|
51
51
|
export interface R2StorageClientUploadContext {
|
|
52
52
|
key: string;
|
|
53
|
+
prefix: string;
|
|
53
54
|
}
|
|
54
55
|
export type R2StorageClientUploadHandlerParams = {
|
|
55
56
|
chunkSize?: number;
|
|
56
|
-
prefix: string;
|
|
57
57
|
};
|
|
58
58
|
export type R2StorageMultipartUploadHandlerParams = {
|
|
59
59
|
collection: string;
|
|
60
|
+
docPrefix?: string;
|
|
60
61
|
fileName: string;
|
|
61
62
|
fileType: string;
|
|
62
63
|
multipartId?: string;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types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B,CAAA"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * R2 API types compatible with both Node (Miniflare) and Cloudflare Workers.\n * R2Range is sourced from Cloudflare so it cannot drift; other types are our own\n * so Node's Blob/Buffer/Headers are accepted and we avoid strict Workers-only types.\n */\nimport type { R2Range } from '@cloudflare/workers-types/2023-07-01'\n\nexport type { R2Range }\n\nexport interface R2GetOptions {\n [key: string]: any\n onlyIf?: any | Headers\n range?: R2Range\n}\n\nexport interface R2Bucket {\n createMultipartUpload(key: string, options?: any): Promise<R2MultipartUpload>\n delete(keys: string | string[]): Promise<void>\n get(key: string, options?: R2GetOptions): Promise<null | R2ObjectBody>\n head(key: string): Promise<null | R2Object>\n list(options?: any): Promise<any>\n put(\n key: string,\n value: ArrayBuffer | ArrayBufferView | Blob | null | ReadableStream | string,\n options?: any,\n ): Promise<any>\n resumeMultipartUpload(key: string, uploadId: string): R2MultipartUpload\n}\n\ninterface R2HTTPMetadata {\n cacheControl?: string\n cacheExpiry?: Date\n contentDisposition?: string\n contentEncoding?: string\n contentLanguage?: string\n contentType?: string\n}\n\nexport interface R2Object {\n readonly etag: string\n readonly httpMetadata?: R2HTTPMetadata\n readonly key: string\n readonly size: number\n writeHttpMetadata(headers: Headers): void\n}\n\nexport interface R2ObjectBody extends R2Object {\n get body(): ReadableStream\n}\n\nexport interface R2MultipartUpload {\n abort(): Promise<void>\n complete(uploadedParts: R2UploadedPart[]): Promise<R2Object>\n readonly key: string\n readonly uploadId: string\n uploadPart(\n partNumber: number,\n value: (ArrayBuffer | ArrayBufferView) | Blob | ReadableStream | string,\n options?: any,\n ): Promise<R2UploadedPart>\n}\n\nexport interface R2UploadedPart {\n etag: string\n partNumber: number\n}\n\nexport interface R2StorageClientUploadContext {\n key: string\n}\nexport type R2StorageClientUploadHandlerParams = {\n chunkSize?: number\n
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * R2 API types compatible with both Node (Miniflare) and Cloudflare Workers.\n * R2Range is sourced from Cloudflare so it cannot drift; other types are our own\n * so Node's Blob/Buffer/Headers are accepted and we avoid strict Workers-only types.\n */\nimport type { R2Range } from '@cloudflare/workers-types/2023-07-01'\n\nexport type { R2Range }\n\nexport interface R2GetOptions {\n [key: string]: any\n onlyIf?: any | Headers\n range?: R2Range\n}\n\nexport interface R2Bucket {\n createMultipartUpload(key: string, options?: any): Promise<R2MultipartUpload>\n delete(keys: string | string[]): Promise<void>\n get(key: string, options?: R2GetOptions): Promise<null | R2ObjectBody>\n head(key: string): Promise<null | R2Object>\n list(options?: any): Promise<any>\n put(\n key: string,\n value: ArrayBuffer | ArrayBufferView | Blob | null | ReadableStream | string,\n options?: any,\n ): Promise<any>\n resumeMultipartUpload(key: string, uploadId: string): R2MultipartUpload\n}\n\ninterface R2HTTPMetadata {\n cacheControl?: string\n cacheExpiry?: Date\n contentDisposition?: string\n contentEncoding?: string\n contentLanguage?: string\n contentType?: string\n}\n\nexport interface R2Object {\n readonly etag: string\n readonly httpMetadata?: R2HTTPMetadata\n readonly key: string\n readonly size: number\n writeHttpMetadata(headers: Headers): void\n}\n\nexport interface R2ObjectBody extends R2Object {\n get body(): ReadableStream\n}\n\nexport interface R2MultipartUpload {\n abort(): Promise<void>\n complete(uploadedParts: R2UploadedPart[]): Promise<R2Object>\n readonly key: string\n readonly uploadId: string\n uploadPart(\n partNumber: number,\n value: (ArrayBuffer | ArrayBufferView) | Blob | ReadableStream | string,\n options?: any,\n ): Promise<R2UploadedPart>\n}\n\nexport interface R2UploadedPart {\n etag: string\n partNumber: number\n}\n\nexport interface R2StorageClientUploadContext {\n key: string\n prefix: string\n}\nexport type R2StorageClientUploadHandlerParams = {\n chunkSize?: number\n}\n\nexport type R2StorageMultipartUploadHandlerParams = {\n collection: string\n docPrefix?: string\n fileName: string\n fileType: string\n multipartId?: string\n multipartKey?: string\n multipartNumber?: string\n}\n"],"names":[],"mappings":"AAAA;;;;CAIC,GAuED,WAQC"}
|
package/dist/uploadFile.js
CHANGED
|
@@ -5,13 +5,13 @@ export async function uploadFile({ bucket, buffer, collectionPrefix = '', docPre
|
|
|
5
5
|
const body = isMiniflare ? new Blob([
|
|
6
6
|
buffer
|
|
7
7
|
]) : buffer;
|
|
8
|
-
const
|
|
8
|
+
const { fileKey } = getFileKey({
|
|
9
9
|
collectionPrefix,
|
|
10
|
-
docPrefix
|
|
10
|
+
docPrefix,
|
|
11
11
|
filename,
|
|
12
12
|
useCompositePrefixes
|
|
13
13
|
});
|
|
14
|
-
await bucket.put(
|
|
14
|
+
await bucket.put(fileKey, body, {
|
|
15
15
|
httpMetadata: {
|
|
16
16
|
contentType: mimeType
|
|
17
17
|
}
|
package/dist/uploadFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/uploadFile.ts"],"sourcesContent":["import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket } from './types.js'\n\nconst isMiniflare = process.env.NODE_ENV === 'development'\n\ninterface UploadFileArgs {\n bucket: R2Bucket\n buffer: Buffer\n collectionPrefix?: string\n docPrefix?: string\n filename: string\n mimeType: string\n useCompositePrefixes?: boolean\n}\n\nexport async function uploadFile({\n bucket,\n buffer,\n collectionPrefix = '',\n docPrefix,\n filename,\n mimeType,\n useCompositePrefixes = false,\n}: UploadFileArgs): Promise<void> {\n // Read more: https://github.com/cloudflare/workers-sdk/issues/6047#issuecomment-2691217843\n const body = isMiniflare ? new Blob([buffer]) : buffer\n\n const
|
|
1
|
+
{"version":3,"sources":["../src/uploadFile.ts"],"sourcesContent":["import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\nimport type { R2Bucket } from './types.js'\n\nconst isMiniflare = process.env.NODE_ENV === 'development'\n\ninterface UploadFileArgs {\n bucket: R2Bucket\n buffer: Buffer\n collectionPrefix?: string\n docPrefix?: string\n filename: string\n mimeType: string\n useCompositePrefixes?: boolean\n}\n\nexport async function uploadFile({\n bucket,\n buffer,\n collectionPrefix = '',\n docPrefix,\n filename,\n mimeType,\n useCompositePrefixes = false,\n}: UploadFileArgs): Promise<void> {\n // Read more: https://github.com/cloudflare/workers-sdk/issues/6047#issuecomment-2691217843\n const body = isMiniflare ? new Blob([buffer]) : buffer\n\n const { fileKey } = getFileKey({\n collectionPrefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n\n await bucket.put(fileKey, body, {\n httpMetadata: { contentType: mimeType },\n })\n}\n"],"names":["getFileKey","isMiniflare","process","env","NODE_ENV","uploadFile","bucket","buffer","collectionPrefix","docPrefix","filename","mimeType","useCompositePrefixes","body","Blob","fileKey","put","httpMetadata","contentType"],"mappings":"AAAA,SAASA,UAAU,QAAQ,6CAA4C;AAIvE,MAAMC,cAAcC,QAAQC,GAAG,CAACC,QAAQ,KAAK;AAY7C,OAAO,eAAeC,WAAW,EAC/BC,MAAM,EACNC,MAAM,EACNC,mBAAmB,EAAE,EACrBC,SAAS,EACTC,QAAQ,EACRC,QAAQ,EACRC,uBAAuB,KAAK,EACb;IACf,2FAA2F;IAC3F,MAAMC,OAAOZ,cAAc,IAAIa,KAAK;QAACP;KAAO,IAAIA;IAEhD,MAAM,EAAEQ,OAAO,EAAE,GAAGf,WAAW;QAC7BQ;QACAC;QACAC;QACAE;IACF;IAEA,MAAMN,OAAOU,GAAG,CAACD,SAASF,MAAM;QAC9BI,cAAc;YAAEC,aAAaP;QAAS;IACxC;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-r2",
|
|
3
|
-
"version": "3.84.0-
|
|
3
|
+
"version": "3.84.0-internal.d5d6e43",
|
|
4
4
|
"description": "Payload storage adapter for Cloudflare R2",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -37,14 +37,14 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@payloadcms/plugin-cloud-storage": "3.84.0-
|
|
40
|
+
"@payloadcms/plugin-cloud-storage": "3.84.0-internal.d5d6e43"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@cloudflare/workers-types": "4.20260218.0",
|
|
44
|
-
"payload": "3.84.0-
|
|
44
|
+
"payload": "3.84.0-internal.d5d6e43"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"payload": "3.84.0-
|
|
47
|
+
"payload": "3.84.0-internal.d5d6e43"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": "^18.20.2 || >=20.9.0"
|