@payloadcms/plugin-cloud-storage 3.0.0-beta.6 → 3.0.0-beta.6-86adc6f
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/adapters/vercelBlob/generateURL.d.ts +8 -0
- package/dist/adapters/vercelBlob/generateURL.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/generateURL.js +8 -0
- package/dist/adapters/vercelBlob/generateURL.js.map +1 -0
- package/dist/adapters/vercelBlob/getFilePrefix.d.ts +6 -0
- package/dist/adapters/vercelBlob/getFilePrefix.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/getFilePrefix.js +29 -0
- package/dist/adapters/vercelBlob/getFilePrefix.js.map +1 -0
- package/dist/adapters/vercelBlob/handleDelete.d.ts +9 -0
- package/dist/adapters/vercelBlob/handleDelete.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/handleDelete.js +13 -0
- package/dist/adapters/vercelBlob/handleDelete.js.map +1 -0
- package/dist/adapters/vercelBlob/handleUpload.d.ts +10 -0
- package/dist/adapters/vercelBlob/handleUpload.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/handleUpload.js +21 -0
- package/dist/adapters/vercelBlob/handleUpload.js.map +1 -0
- package/dist/adapters/vercelBlob/index.d.ts +32 -0
- package/dist/adapters/vercelBlob/index.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/index.js +49 -0
- package/dist/adapters/vercelBlob/index.js.map +1 -0
- package/dist/adapters/vercelBlob/staticHandler.d.ts +9 -0
- package/dist/adapters/vercelBlob/staticHandler.d.ts.map +1 -0
- package/dist/adapters/vercelBlob/staticHandler.js +77 -0
- package/dist/adapters/vercelBlob/staticHandler.js.map +1 -0
- package/dist/exports/vercelBlob.d.ts +2 -0
- package/dist/exports/vercelBlob.d.ts.map +1 -0
- package/dist/exports/vercelBlob.js +3 -0
- package/dist/exports/vercelBlob.js.map +1 -0
- package/package.json +5 -1
- package/vercelBlob.d.ts +2 -0
- package/vercelBlob.js +3 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
type GenerateUrlArgs = {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const getGenerateUrl: ({ baseUrl }: GenerateUrlArgs) => GenerateURL;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=generateURL.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateURL.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/generateURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wCAAwC,CAAA;AAIzE,KAAK,eAAe,GAAG;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,eAAO,MAAM,cAAc,gBAAiB,eAAe,KAAG,WAI7D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/generateURL.ts"],"sourcesContent":["import type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types'\n\nimport path from 'path'\n\ntype GenerateUrlArgs = {\n baseUrl: string\n prefix?: string\n}\n\nexport const getGenerateUrl = ({ baseUrl }: GenerateUrlArgs): GenerateURL => {\n return ({ filename, prefix = '' }) => {\n return `${baseUrl}/${path.posix.join(prefix, filename)}`\n }\n}\n"],"names":["path","getGenerateUrl","baseUrl","filename","prefix","posix","join"],"rangeMappings":";;;;;","mappings":"AAEA,OAAOA,UAAU,OAAM;AAOvB,OAAO,MAAMC,iBAAiB,CAAC,EAAEC,OAAO,EAAmB;IACzD,OAAO,CAAC,EAAEC,QAAQ,EAAEC,SAAS,EAAE,EAAE;QAC/B,OAAO,CAAC,EAAEF,QAAQ,CAAC,EAAEF,KAAKK,KAAK,CAACC,IAAI,CAACF,QAAQD,UAAU,CAAC;IAC1D;AACF,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFilePrefix.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/getFilePrefix.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAgB,MAAM,eAAe,CAAA;AAEnF,wBAAsB,aAAa,CAAC,EAClC,UAAU,EACV,GAAG,GACJ,EAAE;IACD,UAAU,EAAE,gBAAgB,CAAA;IAC5B,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBlB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export async function getFilePrefix({ collection, req }) {
|
|
2
|
+
const imageSizes = collection?.upload?.imageSizes || [];
|
|
3
|
+
const { routeParams } = req;
|
|
4
|
+
const filename = routeParams?.['filename'];
|
|
5
|
+
const files = await req.payload.find({
|
|
6
|
+
collection: collection.slug,
|
|
7
|
+
depth: 0,
|
|
8
|
+
limit: 1,
|
|
9
|
+
pagination: false,
|
|
10
|
+
where: {
|
|
11
|
+
or: [
|
|
12
|
+
{
|
|
13
|
+
filename: {
|
|
14
|
+
equals: filename
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
...imageSizes.map((imageSize)=>({
|
|
18
|
+
[`sizes.${imageSize.name}.filename`]: {
|
|
19
|
+
equals: filename
|
|
20
|
+
}
|
|
21
|
+
}))
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const prefix = files?.docs?.[0]?.prefix;
|
|
26
|
+
return prefix ? prefix : '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=getFilePrefix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/getFilePrefix.ts"],"sourcesContent":["import type { CollectionConfig, PayloadRequest, UploadConfig } from 'payload/types'\n\nexport async function getFilePrefix({\n collection,\n req,\n}: {\n collection: CollectionConfig\n req: PayloadRequest\n}): Promise<string> {\n const imageSizes = (collection?.upload as UploadConfig)?.imageSizes || []\n const { routeParams } = req\n const filename = routeParams?.['filename']\n\n const files = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n limit: 1,\n pagination: false,\n where: {\n or: [\n {\n filename: { equals: filename },\n },\n ...imageSizes.map((imageSize) => ({\n [`sizes.${imageSize.name}.filename`]: { equals: filename },\n })),\n ],\n },\n })\n const prefix = files?.docs?.[0]?.prefix\n return prefix ? (prefix as string) : ''\n}\n"],"names":["getFilePrefix","collection","req","imageSizes","upload","routeParams","filename","files","payload","find","slug","depth","limit","pagination","where","or","equals","map","imageSize","name","prefix","docs"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,OAAO,eAAeA,cAAc,EAClCC,UAAU,EACVC,GAAG,EAIJ;IACC,MAAMC,aAAa,AAACF,YAAYG,QAAyBD,cAAc,EAAE;IACzE,MAAM,EAAEE,WAAW,EAAE,GAAGH;IACxB,MAAMI,WAAWD,aAAa,CAAC,WAAW;IAE1C,MAAME,QAAQ,MAAML,IAAIM,OAAO,CAACC,IAAI,CAAC;QACnCR,YAAYA,WAAWS,IAAI;QAC3BC,OAAO;QACPC,OAAO;QACPC,YAAY;QACZC,OAAO;YACLC,IAAI;gBACF;oBACET,UAAU;wBAAEU,QAAQV;oBAAS;gBAC/B;mBACGH,WAAWc,GAAG,CAAC,CAACC,YAAe,CAAA;wBAChC,CAAC,CAAC,MAAM,EAAEA,UAAUC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE;4BAAEH,QAAQV;wBAAS;oBAC3D,CAAA;aACD;QACH;IACF;IACA,MAAMc,SAASb,OAAOc,MAAM,CAAC,EAAE,EAAED;IACjC,OAAOA,SAAUA,SAAoB;AACvC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
type HandleDeleteArgs = {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const getHandleDelete: ({ baseUrl, token }: HandleDeleteArgs) => HandleDelete;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=handleDelete.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleDelete.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/handleDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAK1E,KAAK,gBAAgB,GAAG;IACtB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,eAAO,MAAM,eAAe,uBAAwB,gBAAgB,KAAG,YAOtE,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { del } from '@vercel/blob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export const getHandleDelete = ({ baseUrl, token })=>{
|
|
4
|
+
return async ({ doc: { prefix = '' }, filename })=>{
|
|
5
|
+
const fileUrl = `${baseUrl}/${path.posix.join(prefix, filename)}`;
|
|
6
|
+
const deletedBlob = await del(fileUrl, {
|
|
7
|
+
token
|
|
8
|
+
});
|
|
9
|
+
return deletedBlob;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=handleDelete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/handleDelete.ts"],"sourcesContent":["import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types'\n\nimport { del } from '@vercel/blob'\nimport path from 'path'\n\ntype HandleDeleteArgs = {\n baseUrl: string\n prefix?: string\n token: string\n}\n\nexport const getHandleDelete = ({ baseUrl, token }: HandleDeleteArgs): HandleDelete => {\n return async ({ doc: { prefix = '' }, filename }) => {\n const fileUrl = `${baseUrl}/${path.posix.join(prefix, filename)}`\n const deletedBlob = await del(fileUrl, { token })\n\n return deletedBlob\n }\n}\n"],"names":["del","path","getHandleDelete","baseUrl","token","doc","prefix","filename","fileUrl","posix","join","deletedBlob"],"rangeMappings":";;;;;;;;;;","mappings":"AAEA,SAASA,GAAG,QAAQ,eAAc;AAClC,OAAOC,UAAU,OAAM;AAQvB,OAAO,MAAMC,kBAAkB,CAAC,EAAEC,OAAO,EAAEC,KAAK,EAAoB;IAClE,OAAO,OAAO,EAAEC,KAAK,EAAEC,SAAS,EAAE,EAAE,EAAEC,QAAQ,EAAE;QAC9C,MAAMC,UAAU,CAAC,EAAEL,QAAQ,CAAC,EAAEF,KAAKQ,KAAK,CAACC,IAAI,CAACJ,QAAQC,UAAU,CAAC;QACjE,MAAMI,cAAc,MAAMX,IAAIQ,SAAS;YAAEJ;QAAM;QAE/C,OAAOO;IACT;AACF,EAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
import type { VercelBlobAdapterUploadOptions } from './index.js';
|
|
3
|
+
type HandleUploadArgs = VercelBlobAdapterUploadOptions & {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
prefix?: string;
|
|
6
|
+
token: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const getHandleUpload: ({ access, addRandomSuffix, baseUrl, cacheControlMaxAge, prefix, token, }: HandleUploadArgs) => HandleUpload;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=handleUpload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleUpload.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/handleUpload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAK1E,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,YAAY,CAAA;AAEhE,KAAK,gBAAgB,GAAG,8BAA8B,GAAG;IACvD,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,eAAO,MAAM,eAAe,6EAOzB,gBAAgB,KAAG,YAmBrB,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { put } from '@vercel/blob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export const getHandleUpload = ({ access = 'public', addRandomSuffix, baseUrl, cacheControlMaxAge, prefix = '', token })=>{
|
|
4
|
+
return async ({ data, file: { buffer, filename, mimeType } })=>{
|
|
5
|
+
const fileKey = path.posix.join(data.prefix || prefix, filename);
|
|
6
|
+
const result = await put(fileKey, buffer, {
|
|
7
|
+
access,
|
|
8
|
+
addRandomSuffix,
|
|
9
|
+
cacheControlMaxAge,
|
|
10
|
+
contentType: mimeType,
|
|
11
|
+
token
|
|
12
|
+
});
|
|
13
|
+
// Get filename with suffix from returned url
|
|
14
|
+
if (addRandomSuffix) {
|
|
15
|
+
data.filename = result.url.replace(`${baseUrl}/`, '');
|
|
16
|
+
}
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=handleUpload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/handleUpload.ts"],"sourcesContent":["import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'\n\nimport { put } from '@vercel/blob'\nimport path from 'path'\n\nimport type { VercelBlobAdapterUploadOptions } from './index.js'\n\ntype HandleUploadArgs = VercelBlobAdapterUploadOptions & {\n baseUrl: string\n prefix?: string\n token: string\n}\n\nexport const getHandleUpload = ({\n access = 'public',\n addRandomSuffix,\n baseUrl,\n cacheControlMaxAge,\n prefix = '',\n token,\n}: HandleUploadArgs): HandleUpload => {\n return async ({ data, file: { buffer, filename, mimeType } }) => {\n const fileKey = path.posix.join(data.prefix || prefix, filename)\n\n const result = await put(fileKey, buffer, {\n access,\n addRandomSuffix,\n cacheControlMaxAge,\n contentType: mimeType,\n token,\n })\n\n // Get filename with suffix from returned url\n if (addRandomSuffix) {\n data.filename = result.url.replace(`${baseUrl}/`, '')\n }\n\n return data\n }\n}\n"],"names":["put","path","getHandleUpload","access","addRandomSuffix","baseUrl","cacheControlMaxAge","prefix","token","data","file","buffer","filename","mimeType","fileKey","posix","join","result","contentType","url","replace"],"rangeMappings":";;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,GAAG,QAAQ,eAAc;AAClC,OAAOC,UAAU,OAAM;AAUvB,OAAO,MAAMC,kBAAkB,CAAC,EAC9BC,SAAS,QAAQ,EACjBC,eAAe,EACfC,OAAO,EACPC,kBAAkB,EAClBC,SAAS,EAAE,EACXC,KAAK,EACY;IACjB,OAAO,OAAO,EAAEC,IAAI,EAAEC,MAAM,EAAEC,MAAM,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,EAAE;QAC1D,MAAMC,UAAUb,KAAKc,KAAK,CAACC,IAAI,CAACP,KAAKF,MAAM,IAAIA,QAAQK;QAEvD,MAAMK,SAAS,MAAMjB,IAAIc,SAASH,QAAQ;YACxCR;YACAC;YACAE;YACAY,aAAaL;YACbL;QACF;QAEA,6CAA6C;QAC7C,IAAIJ,iBAAiB;YACnBK,KAAKG,QAAQ,GAAGK,OAAOE,GAAG,CAACC,OAAO,CAAC,CAAC,EAAEf,QAAQ,CAAC,CAAC,EAAE;QACpD;QAEA,OAAOI;IACT;AACF,EAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Adapter } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
export interface VercelBlobAdapterArgs {
|
|
3
|
+
options?: VercelBlobAdapterUploadOptions;
|
|
4
|
+
/**
|
|
5
|
+
* Vercel Blob storage read/write token
|
|
6
|
+
*
|
|
7
|
+
* Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel
|
|
8
|
+
*/
|
|
9
|
+
token: string;
|
|
10
|
+
}
|
|
11
|
+
export interface VercelBlobAdapterUploadOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Access control level
|
|
14
|
+
*
|
|
15
|
+
* @default 'public'
|
|
16
|
+
*/
|
|
17
|
+
access?: 'public';
|
|
18
|
+
/**
|
|
19
|
+
* Add a random suffix to the uploaded file name
|
|
20
|
+
*
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
addRandomSuffix?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Cache-Control max-age in seconds
|
|
26
|
+
*
|
|
27
|
+
* @default 31536000 (1 year)
|
|
28
|
+
*/
|
|
29
|
+
cacheControlMaxAge?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare const vercelBlobAdapter: ({ options, token }: VercelBlobAdapterArgs) => Adapter;
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAoB,MAAM,wCAAwC,CAAA;AAOvF,MAAM,WAAW,qBAAqB;IACpC,OAAO,CAAC,EAAE,8BAA8B,CAAA;IAExC;;;;OAIG;IACH,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,8BAA8B;IAC7C;;;;OAIG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAA;IACjB;;;;OAIG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAQD,eAAO,MAAM,iBAAiB,uBACF,qBAAqB,KAAG,OAmCjD,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getGenerateUrl } from './generateURL.js';
|
|
2
|
+
import { getHandleDelete } from './handleDelete.js';
|
|
3
|
+
import { getHandleUpload } from './handleUpload.js';
|
|
4
|
+
import { getStaticHandler } from './staticHandler.js';
|
|
5
|
+
const defaultUploadOptions = {
|
|
6
|
+
access: 'public',
|
|
7
|
+
addRandomSuffix: false,
|
|
8
|
+
cacheControlMaxAge: 60 * 60 * 24 * 365
|
|
9
|
+
};
|
|
10
|
+
export const vercelBlobAdapter = ({ options = {}, token })=>({ collection, prefix })=>{
|
|
11
|
+
if (!token) {
|
|
12
|
+
throw new Error('The token argument is required for the Vercel Blob adapter.');
|
|
13
|
+
}
|
|
14
|
+
// Parse storeId from token
|
|
15
|
+
const storeId = token.match(/^vercel_blob_rw_([a-z\d]+)_[a-z\d]+$/i)?.[1].toLowerCase();
|
|
16
|
+
if (!storeId) {
|
|
17
|
+
throw new Error('Invalid token format for Vercel Blob adapter. Should be vercel_blob_rw_<store_id>_<random_string>.');
|
|
18
|
+
}
|
|
19
|
+
const { access, addRandomSuffix, cacheControlMaxAge } = {
|
|
20
|
+
...defaultUploadOptions,
|
|
21
|
+
...options
|
|
22
|
+
};
|
|
23
|
+
const baseUrl = `https://${storeId}.${access}.blob.vercel-storage.com`;
|
|
24
|
+
return {
|
|
25
|
+
generateURL: getGenerateUrl({
|
|
26
|
+
baseUrl,
|
|
27
|
+
prefix
|
|
28
|
+
}),
|
|
29
|
+
handleDelete: getHandleDelete({
|
|
30
|
+
baseUrl,
|
|
31
|
+
prefix,
|
|
32
|
+
token
|
|
33
|
+
}),
|
|
34
|
+
handleUpload: getHandleUpload({
|
|
35
|
+
access,
|
|
36
|
+
addRandomSuffix,
|
|
37
|
+
baseUrl,
|
|
38
|
+
cacheControlMaxAge,
|
|
39
|
+
prefix,
|
|
40
|
+
token
|
|
41
|
+
}),
|
|
42
|
+
staticHandler: getStaticHandler({
|
|
43
|
+
baseUrl,
|
|
44
|
+
token
|
|
45
|
+
}, collection)
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/index.ts"],"sourcesContent":["import type { Adapter, GeneratedAdapter } from '@payloadcms/plugin-cloud-storage/types'\n\nimport { getGenerateUrl } from './generateURL.js'\nimport { getHandleDelete } from './handleDelete.js'\nimport { getHandleUpload } from './handleUpload.js'\nimport { getStaticHandler } from './staticHandler.js'\n\nexport interface VercelBlobAdapterArgs {\n options?: VercelBlobAdapterUploadOptions\n\n /**\n * Vercel Blob storage read/write token\n *\n * Usually process.env.BLOB_READ_WRITE_TOKEN set by Vercel\n */\n token: string\n}\n\nexport interface VercelBlobAdapterUploadOptions {\n /**\n * Access control level\n *\n * @default 'public'\n */\n access?: 'public'\n /**\n * Add a random suffix to the uploaded file name\n *\n * @default false\n */\n addRandomSuffix?: boolean\n /**\n * Cache-Control max-age in seconds\n *\n * @default 31536000 (1 year)\n */\n cacheControlMaxAge?: number\n}\n\nconst defaultUploadOptions: VercelBlobAdapterUploadOptions = {\n access: 'public',\n addRandomSuffix: false,\n cacheControlMaxAge: 60 * 60 * 24 * 365, // 1 year\n}\n\nexport const vercelBlobAdapter =\n ({ options = {}, token }: VercelBlobAdapterArgs): Adapter =>\n ({ collection, prefix }): GeneratedAdapter => {\n if (!token) {\n throw new Error('The token argument is required for the Vercel Blob adapter.')\n }\n\n // Parse storeId from token\n const storeId = token.match(/^vercel_blob_rw_([a-z\\d]+)_[a-z\\d]+$/i)?.[1].toLowerCase()\n\n if (!storeId) {\n throw new Error(\n 'Invalid token format for Vercel Blob adapter. Should be vercel_blob_rw_<store_id>_<random_string>.',\n )\n }\n\n const { access, addRandomSuffix, cacheControlMaxAge } = {\n ...defaultUploadOptions,\n ...options,\n }\n\n const baseUrl = `https://${storeId}.${access}.blob.vercel-storage.com`\n\n return {\n generateURL: getGenerateUrl({ baseUrl, prefix }),\n handleDelete: getHandleDelete({ baseUrl, prefix, token }),\n handleUpload: getHandleUpload({\n access,\n addRandomSuffix,\n baseUrl,\n cacheControlMaxAge,\n prefix,\n token,\n }),\n staticHandler: getStaticHandler({ baseUrl, token }, collection),\n }\n }\n"],"names":["getGenerateUrl","getHandleDelete","getHandleUpload","getStaticHandler","defaultUploadOptions","access","addRandomSuffix","cacheControlMaxAge","vercelBlobAdapter","options","token","collection","prefix","Error","storeId","match","toLowerCase","baseUrl","generateURL","handleDelete","handleUpload","staticHandler"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,cAAc,QAAQ,mBAAkB;AACjD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,eAAe,QAAQ,oBAAmB;AACnD,SAASC,gBAAgB,QAAQ,qBAAoB;AAkCrD,MAAMC,uBAAuD;IAC3DC,QAAQ;IACRC,iBAAiB;IACjBC,oBAAoB,KAAK,KAAK,KAAK;AACrC;AAEA,OAAO,MAAMC,oBACX,CAAC,EAAEC,UAAU,CAAC,CAAC,EAAEC,KAAK,EAAyB,GAC/C,CAAC,EAAEC,UAAU,EAAEC,MAAM,EAAE;QACrB,IAAI,CAACF,OAAO;YACV,MAAM,IAAIG,MAAM;QAClB;QAEA,2BAA2B;QAC3B,MAAMC,UAAUJ,MAAMK,KAAK,CAAC,0CAA0C,CAAC,EAAE,CAACC;QAE1E,IAAI,CAACF,SAAS;YACZ,MAAM,IAAID,MACR;QAEJ;QAEA,MAAM,EAAER,MAAM,EAAEC,eAAe,EAAEC,kBAAkB,EAAE,GAAG;YACtD,GAAGH,oBAAoB;YACvB,GAAGK,OAAO;QACZ;QAEA,MAAMQ,UAAU,CAAC,QAAQ,EAAEH,QAAQ,CAAC,EAAET,OAAO,wBAAwB,CAAC;QAEtE,OAAO;YACLa,aAAalB,eAAe;gBAAEiB;gBAASL;YAAO;YAC9CO,cAAclB,gBAAgB;gBAAEgB;gBAASL;gBAAQF;YAAM;YACvDU,cAAclB,gBAAgB;gBAC5BG;gBACAC;gBACAW;gBACAV;gBACAK;gBACAF;YACF;YACAW,eAAelB,iBAAiB;gBAAEc;gBAASP;YAAM,GAAGC;QACtD;IACF,EAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
import type { CollectionConfig } from 'payload/types';
|
|
3
|
+
type StaticHandlerArgs = {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const getStaticHandler: ({ baseUrl, token }: StaticHandlerArgs, collection: CollectionConfig) => StaticHandler;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=staticHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"staticHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/vercelBlob/staticHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAA;AAC3E,OAAO,KAAK,EAAE,gBAAgB,EAAgC,MAAM,eAAe,CAAA;AAKnF,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,eAAO,MAAM,gBAAgB,uBACP,iBAAiB,cACzB,gBAAgB,KAC3B,aAmCF,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { head } from '@vercel/blob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export const getStaticHandler = ({ baseUrl, token }, collection)=>{
|
|
4
|
+
return async (req, { params: { filename } })=>{
|
|
5
|
+
try {
|
|
6
|
+
const prefix = await getFilePrefix({
|
|
7
|
+
collection,
|
|
8
|
+
req
|
|
9
|
+
});
|
|
10
|
+
const fileUrl = `${baseUrl}/${path.posix.join(prefix, filename)}`;
|
|
11
|
+
const blobMetadata = await head(fileUrl, {
|
|
12
|
+
token
|
|
13
|
+
});
|
|
14
|
+
if (!blobMetadata) {
|
|
15
|
+
return new Response(null, {
|
|
16
|
+
status: 404,
|
|
17
|
+
statusText: 'Not Found'
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const { contentDisposition, contentType, size } = blobMetadata;
|
|
21
|
+
const response = await fetch(fileUrl);
|
|
22
|
+
const blob = await response.blob();
|
|
23
|
+
if (!blob) {
|
|
24
|
+
return new Response(null, {
|
|
25
|
+
status: 204,
|
|
26
|
+
statusText: 'No Content'
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const bodyBuffer = await blob.arrayBuffer();
|
|
30
|
+
return new Response(bodyBuffer, {
|
|
31
|
+
headers: new Headers({
|
|
32
|
+
'Content-Disposition': contentDisposition,
|
|
33
|
+
'Content-Length': String(size),
|
|
34
|
+
'Content-Type': contentType
|
|
35
|
+
}),
|
|
36
|
+
status: 200
|
|
37
|
+
});
|
|
38
|
+
} catch (err) {
|
|
39
|
+
req.payload.logger.error({
|
|
40
|
+
err,
|
|
41
|
+
msg: 'Unexpected error in staticHandler'
|
|
42
|
+
});
|
|
43
|
+
return new Response('Internal Server Error', {
|
|
44
|
+
status: 500
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
async function getFilePrefix({ collection, req }) {
|
|
50
|
+
const imageSizes = collection?.upload?.imageSizes || [];
|
|
51
|
+
const { routeParams } = req;
|
|
52
|
+
const filename = routeParams?.['filename'];
|
|
53
|
+
const files = await req.payload.find({
|
|
54
|
+
collection: collection.slug,
|
|
55
|
+
depth: 0,
|
|
56
|
+
limit: 1,
|
|
57
|
+
pagination: false,
|
|
58
|
+
where: {
|
|
59
|
+
or: [
|
|
60
|
+
{
|
|
61
|
+
filename: {
|
|
62
|
+
equals: filename
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
...imageSizes.map((imageSize)=>({
|
|
66
|
+
[`sizes.${imageSize.name}.filename`]: {
|
|
67
|
+
equals: filename
|
|
68
|
+
}
|
|
69
|
+
}))
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
const prefix = files?.docs?.[0]?.prefix;
|
|
74
|
+
return prefix ? prefix : '';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=staticHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/vercelBlob/staticHandler.ts"],"sourcesContent":["import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig, PayloadRequest, UploadConfig } from 'payload/types'\n\nimport { head } from '@vercel/blob'\nimport path from 'path'\n\ntype StaticHandlerArgs = {\n baseUrl: string\n token: string\n}\n\nexport const getStaticHandler = (\n { baseUrl, token }: StaticHandlerArgs,\n collection: CollectionConfig,\n): StaticHandler => {\n return async (req, { params: { filename } }) => {\n try {\n const prefix = await getFilePrefix({ collection, req })\n\n const fileUrl = `${baseUrl}/${path.posix.join(prefix, filename)}`\n\n const blobMetadata = await head(fileUrl, { token })\n if (!blobMetadata) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n const { contentDisposition, contentType, size } = blobMetadata\n const response = await fetch(fileUrl)\n const blob = await response.blob()\n\n if (!blob) {\n return new Response(null, { status: 204, statusText: 'No Content' })\n }\n\n const bodyBuffer = await blob.arrayBuffer()\n\n return new Response(bodyBuffer, {\n headers: new Headers({\n 'Content-Disposition': contentDisposition,\n 'Content-Length': String(size),\n 'Content-Type': contentType,\n }),\n status: 200,\n })\n } catch (err: unknown) {\n req.payload.logger.error({ err, msg: 'Unexpected error in staticHandler' })\n return new Response('Internal Server Error', { status: 500 })\n }\n }\n}\n\nasync function getFilePrefix({\n collection,\n req,\n}: {\n collection: CollectionConfig\n req: PayloadRequest\n}): Promise<string> {\n const imageSizes = (collection?.upload as UploadConfig)?.imageSizes || []\n const { routeParams } = req\n const filename = routeParams?.['filename']\n\n const files = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n limit: 1,\n pagination: false,\n where: {\n or: [\n {\n filename: { equals: filename },\n },\n ...imageSizes.map((imageSize) => ({\n [`sizes.${imageSize.name}.filename`]: { equals: filename },\n })),\n ],\n },\n })\n const prefix = files?.docs?.[0]?.prefix\n return prefix ? (prefix as string) : ''\n}\n"],"names":["head","path","getStaticHandler","baseUrl","token","collection","req","params","filename","prefix","getFilePrefix","fileUrl","posix","join","blobMetadata","Response","status","statusText","contentDisposition","contentType","size","response","fetch","blob","bodyBuffer","arrayBuffer","headers","Headers","String","err","payload","logger","error","msg","imageSizes","upload","routeParams","files","find","slug","depth","limit","pagination","where","or","equals","map","imageSize","name","docs"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAGA,SAASA,IAAI,QAAQ,eAAc;AACnC,OAAOC,UAAU,OAAM;AAOvB,OAAO,MAAMC,mBAAmB,CAC9B,EAAEC,OAAO,EAAEC,KAAK,EAAqB,EACrCC;IAEA,OAAO,OAAOC,KAAK,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,EAAE;QACzC,IAAI;YACF,MAAMC,SAAS,MAAMC,cAAc;gBAAEL;gBAAYC;YAAI;YAErD,MAAMK,UAAU,CAAC,EAAER,QAAQ,CAAC,EAAEF,KAAKW,KAAK,CAACC,IAAI,CAACJ,QAAQD,UAAU,CAAC;YAEjE,MAAMM,eAAe,MAAMd,KAAKW,SAAS;gBAAEP;YAAM;YACjD,IAAI,CAACU,cAAc;gBACjB,OAAO,IAAIC,SAAS,MAAM;oBAAEC,QAAQ;oBAAKC,YAAY;gBAAY;YACnE;YAEA,MAAM,EAAEC,kBAAkB,EAAEC,WAAW,EAAEC,IAAI,EAAE,GAAGN;YAClD,MAAMO,WAAW,MAAMC,MAAMX;YAC7B,MAAMY,OAAO,MAAMF,SAASE,IAAI;YAEhC,IAAI,CAACA,MAAM;gBACT,OAAO,IAAIR,SAAS,MAAM;oBAAEC,QAAQ;oBAAKC,YAAY;gBAAa;YACpE;YAEA,MAAMO,aAAa,MAAMD,KAAKE,WAAW;YAEzC,OAAO,IAAIV,SAASS,YAAY;gBAC9BE,SAAS,IAAIC,QAAQ;oBACnB,uBAAuBT;oBACvB,kBAAkBU,OAAOR;oBACzB,gBAAgBD;gBAClB;gBACAH,QAAQ;YACV;QACF,EAAE,OAAOa,KAAc;YACrBvB,IAAIwB,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBAAEH;gBAAKI,KAAK;YAAoC;YACzE,OAAO,IAAIlB,SAAS,yBAAyB;gBAAEC,QAAQ;YAAI;QAC7D;IACF;AACF,EAAC;AAED,eAAeN,cAAc,EAC3BL,UAAU,EACVC,GAAG,EAIJ;IACC,MAAM4B,aAAa,AAAC7B,YAAY8B,QAAyBD,cAAc,EAAE;IACzE,MAAM,EAAEE,WAAW,EAAE,GAAG9B;IACxB,MAAME,WAAW4B,aAAa,CAAC,WAAW;IAE1C,MAAMC,QAAQ,MAAM/B,IAAIwB,OAAO,CAACQ,IAAI,CAAC;QACnCjC,YAAYA,WAAWkC,IAAI;QAC3BC,OAAO;QACPC,OAAO;QACPC,YAAY;QACZC,OAAO;YACLC,IAAI;gBACF;oBACEpC,UAAU;wBAAEqC,QAAQrC;oBAAS;gBAC/B;mBACG0B,WAAWY,GAAG,CAAC,CAACC,YAAe,CAAA;wBAChC,CAAC,CAAC,MAAM,EAAEA,UAAUC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE;4BAAEH,QAAQrC;wBAAS;oBAC3D,CAAA;aACD;QACH;IACF;IACA,MAAMC,SAAS4B,OAAOY,MAAM,CAAC,EAAE,EAAExC;IACjC,OAAOA,SAAUA,SAAoB;AACvC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercelBlob.d.ts","sourceRoot":"","sources":["../../src/exports/vercelBlob.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/vercelBlob.ts"],"sourcesContent":["export { vercelBlobAdapter } from '../adapters/vercelBlob/index.js'\n"],"names":["vercelBlobAdapter"],"rangeMappings":"","mappings":"AAAA,SAASA,iBAAiB,QAAQ,kCAAiC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-cloud-storage",
|
|
3
3
|
"description": "The official cloud storage plugin for Payload CMS",
|
|
4
|
-
"version": "3.0.0-beta.6",
|
|
4
|
+
"version": "3.0.0-beta.6-86adc6f",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -35,6 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"@google-cloud/storage": {
|
|
37
37
|
"optional": true
|
|
38
|
+
},
|
|
39
|
+
"@vercel/blob": {
|
|
40
|
+
"optional": true
|
|
38
41
|
}
|
|
39
42
|
},
|
|
40
43
|
"files": [
|
|
@@ -48,6 +51,7 @@
|
|
|
48
51
|
"@azure/storage-blob": "^12.11.0",
|
|
49
52
|
"@google-cloud/storage": "^7.7.0",
|
|
50
53
|
"@types/find-node-modules": "^2.1.2",
|
|
54
|
+
"@vercel/blob": "^0.22.3",
|
|
51
55
|
"payload": "3.0.0-beta.6"
|
|
52
56
|
},
|
|
53
57
|
"dependencies": {
|
package/vercelBlob.d.ts
ADDED
package/vercelBlob.js
ADDED