@payloadcms/storage-azure 4.0.0-internal.cea8867 → 4.0.0-internal.d21c039
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/README.md +13 -10
- package/dist/adapter.d.ts +1 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +24 -14
- package/dist/adapter.js.map +1 -1
- package/dist/client/AzureClientUploadHandler.d.ts +3 -4
- package/dist/client/AzureClientUploadHandler.d.ts.map +1 -1
- package/dist/client/AzureClientUploadHandler.js +2 -11
- package/dist/client/AzureClientUploadHandler.js.map +1 -1
- package/dist/client/handleAzureUpload.d.ts +2 -9
- package/dist/client/handleAzureUpload.d.ts.map +1 -1
- package/dist/client/handleAzureUpload.js +2 -27
- package/dist/client/handleAzureUpload.js.map +1 -1
- package/dist/deleteFile.d.ts +2 -5
- package/dist/deleteFile.d.ts.map +1 -1
- package/dist/deleteFile.js +2 -9
- package/dist/deleteFile.js.map +1 -1
- package/dist/generateURL.js +3 -3
- package/dist/generateURL.js.map +1 -1
- package/dist/generateUploadInstructions.d.ts +12 -0
- package/dist/generateUploadInstructions.d.ts.map +1 -0
- package/dist/generateUploadInstructions.js +57 -0
- package/dist/generateUploadInstructions.js.map +1 -0
- package/dist/getFile.d.ts +4 -4
- package/dist/getFile.d.ts.map +1 -1
- package/dist/getFile.js +13 -11
- package/dist/getFile.js.map +1 -1
- package/dist/index.d.ts +23 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -22
- package/dist/index.js.map +1 -1
- package/dist/isClientUploadAllowed.d.ts +6 -0
- package/dist/isClientUploadAllowed.d.ts.map +1 -0
- package/dist/isClientUploadAllowed.js +3 -0
- package/dist/isClientUploadAllowed.js.map +1 -0
- package/dist/uploadFile.d.ts +2 -5
- package/dist/uploadFile.d.ts.map +1 -1
- package/dist/uploadFile.js +2 -9
- package/dist/uploadFile.js.map +1 -1
- package/package.json +5 -5
- package/dist/client/handleAzureUpload.spec.d.ts +0 -2
- package/dist/client/handleAzureUpload.spec.d.ts.map +0 -1
- package/dist/client/handleAzureUpload.spec.js +0 -185
- package/dist/client/handleAzureUpload.spec.js.map +0 -1
- package/dist/generateSignedURL.d.ts +0 -14
- package/dist/generateSignedURL.d.ts.map +0 -1
- package/dist/generateSignedURL.js +0 -47
- package/dist/generateSignedURL.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,16 +8,6 @@ export type AzureStorageOptions = {
|
|
|
8
8
|
* @default false
|
|
9
9
|
*/
|
|
10
10
|
allowContainerCreate: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* When enabled, fields (like the prefix field) will always be inserted into
|
|
13
|
-
* the collection schema regardless of whether the plugin is enabled. This
|
|
14
|
-
* ensures a consistent schema across all environments.
|
|
15
|
-
*
|
|
16
|
-
* This will be enabled by default in Payload v4.
|
|
17
|
-
*
|
|
18
|
-
* @default false
|
|
19
|
-
*/
|
|
20
|
-
alwaysInsertFields?: boolean;
|
|
21
11
|
/**
|
|
22
12
|
* Base URL for the Azure Blob storage account
|
|
23
13
|
*/
|
|
@@ -47,6 +37,21 @@ export type AzureStorageOptions = {
|
|
|
47
37
|
* Azure Blob storage connection string
|
|
48
38
|
*/
|
|
49
39
|
connectionString: string;
|
|
40
|
+
/**
|
|
41
|
+
* Public access level applied to a container that the plugin creates via
|
|
42
|
+
* `allowContainerCreate`. Has no effect on containers that already exist.
|
|
43
|
+
*
|
|
44
|
+
* - `'private'` (default): no anonymous access. Blobs are reachable only
|
|
45
|
+
* through Payload's access-controlled file route.
|
|
46
|
+
* - `'blob'`: unauthenticated clients can read any blob directly from Azure.
|
|
47
|
+
* - `'container'`: unauthenticated clients can read and list blobs directly.
|
|
48
|
+
*
|
|
49
|
+
* Only choose `'blob'` or `'container'` if you deliberately want files served
|
|
50
|
+
* publicly from Azure, bypassing Payload read access control.
|
|
51
|
+
*
|
|
52
|
+
* @default 'private'
|
|
53
|
+
*/
|
|
54
|
+
containerAccess?: 'blob' | 'container' | 'private';
|
|
50
55
|
/**
|
|
51
56
|
* Azure Blob storage container name
|
|
52
57
|
*/
|
|
@@ -59,14 +64,15 @@ export type AzureStorageOptions = {
|
|
|
59
64
|
enabled?: boolean;
|
|
60
65
|
/**
|
|
61
66
|
* When true, the collection-level prefix and document-level prefix are combined
|
|
62
|
-
* (compositional). When false (default), document prefix
|
|
63
|
-
* prefix
|
|
67
|
+
* (compositional). When false (default), a document prefix already within the
|
|
68
|
+
* collection prefix is used as-is for new uploads; otherwise it is nested beneath it.
|
|
69
|
+
* Existing files retain their stored prefixes for reads, URLs, and cleanup.
|
|
64
70
|
*
|
|
65
|
-
* Example:
|
|
66
|
-
* - collection prefix: `
|
|
67
|
-
* - document prefix: `
|
|
68
|
-
* - resulting prefix with useCompositePrefixes=true: `
|
|
69
|
-
* - resulting prefix with useCompositePrefixes=false: `
|
|
71
|
+
* Example with a document prefix already contained by the collection prefix:
|
|
72
|
+
* - collection prefix: `uploads/`
|
|
73
|
+
* - document prefix: `uploads/documents/`
|
|
74
|
+
* - resulting prefix with useCompositePrefixes=true: `uploads/uploads/documents/`
|
|
75
|
+
* - resulting prefix with useCompositePrefixes=false: `uploads/documents/`
|
|
70
76
|
*
|
|
71
77
|
* @default false
|
|
72
78
|
*/
|
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,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"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,cAAc,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAK3E,OAAO,EAAE,gBAAgB,IAAI,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAEtF,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,oBAAoB,EAAE,OAAO,CAAA;IAE7B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAA;IAEnC;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IAE7F;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAA;IAExB;;;;;;;;;;;;;OAaG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAAA;IAElD;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,CAAA;AAED,KAAK,mBAAmB,GAAG,CAAC,gBAAgB,EAAE,mBAAmB,KAAK,cAAc,CAAA;AAEpF,eAAO,MAAM,YAAY,EAAE,mBAiGzB,CAAA;AAEF,OAAO,EAAE,oBAAoB,IAAI,gBAAgB,EAAE,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage';
|
|
2
|
-
import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
3
2
|
import { createAzureAdapter } from './adapter.js';
|
|
4
|
-
import { getGenerateSignedURLHandler } from './generateSignedURL.js';
|
|
5
3
|
import { getStorageClient as getStorageClientFunc } from './utils/getStorageClient.js';
|
|
6
4
|
export const azureStorage = (azureStorageOptions)=>({
|
|
7
5
|
name: 'azure',
|
|
@@ -12,30 +10,36 @@ export const azureStorage = (azureStorageOptions)=>({
|
|
|
12
10
|
containerName: azureStorageOptions.containerName
|
|
13
11
|
});
|
|
14
12
|
const isPluginDisabled = azureStorageOptions.enabled === false;
|
|
15
|
-
initClientUploads({
|
|
16
|
-
clientHandler: '@payloadcms/storage-azure/client#AzureClientUploadHandler',
|
|
17
|
-
collections: azureStorageOptions.collections,
|
|
18
|
-
config: incomingConfig,
|
|
19
|
-
enabled: !isPluginDisabled && Boolean(azureStorageOptions.clientUploads),
|
|
20
|
-
serverHandler: getGenerateSignedURLHandler({
|
|
21
|
-
access: typeof azureStorageOptions.clientUploads === 'object' ? azureStorageOptions.clientUploads.access : undefined,
|
|
22
|
-
collections: azureStorageOptions.collections,
|
|
23
|
-
containerName: azureStorageOptions.containerName,
|
|
24
|
-
getStorageClient,
|
|
25
|
-
useCompositePrefixes: azureStorageOptions.useCompositePrefixes
|
|
26
|
-
}),
|
|
27
|
-
serverHandlerPath: '/storage-azure-generate-signed-url'
|
|
28
|
-
});
|
|
29
13
|
if (isPluginDisabled) {
|
|
30
|
-
|
|
14
|
+
// Still call cloudStoragePlugin with adapter: null so fields (like prefix) are
|
|
15
|
+
// inserted into the schema, keeping it consistent across environments.
|
|
16
|
+
const collectionsWithoutAdapter = Object.entries(azureStorageOptions.collections).reduce((acc, [slug, collOptions])=>({
|
|
17
|
+
...acc,
|
|
18
|
+
[slug]: {
|
|
19
|
+
...collOptions === true ? {} : collOptions,
|
|
20
|
+
adapter: null
|
|
21
|
+
}
|
|
22
|
+
}), {});
|
|
23
|
+
return cloudStoragePlugin({
|
|
24
|
+
collections: collectionsWithoutAdapter,
|
|
25
|
+
enabled: false,
|
|
26
|
+
useCompositePrefixes: azureStorageOptions.useCompositePrefixes
|
|
27
|
+
})(incomingConfig);
|
|
31
28
|
}
|
|
32
|
-
const createContainerIfNotExists = ()=>{
|
|
33
|
-
|
|
29
|
+
const createContainerIfNotExists = async ()=>{
|
|
30
|
+
const containerClient = getStorageClientFunc({
|
|
34
31
|
connectionString: azureStorageOptions.connectionString,
|
|
35
32
|
containerName: azureStorageOptions.containerName
|
|
36
|
-
}).createIfNotExists({
|
|
37
|
-
access: 'blob'
|
|
38
33
|
});
|
|
34
|
+
// Private by default; public access is opt-in via `containerAccess`.
|
|
35
|
+
const containerAccess = azureStorageOptions.containerAccess ?? 'private';
|
|
36
|
+
if (containerAccess === 'private') {
|
|
37
|
+
await containerClient.createIfNotExists();
|
|
38
|
+
} else {
|
|
39
|
+
await containerClient.createIfNotExists({
|
|
40
|
+
access: containerAccess
|
|
41
|
+
});
|
|
42
|
+
}
|
|
39
43
|
};
|
|
40
44
|
const adapter = createAzureAdapter({
|
|
41
45
|
allowContainerCreate: azureStorageOptions.allowContainerCreate,
|
|
@@ -71,7 +75,6 @@ export const azureStorage = (azureStorageOptions)=>({
|
|
|
71
75
|
})
|
|
72
76
|
};
|
|
73
77
|
return cloudStoragePlugin({
|
|
74
|
-
alwaysInsertFields: azureStorageOptions.alwaysInsertFields,
|
|
75
78
|
collections: collectionsWithAdapter,
|
|
76
79
|
useCompositePrefixes: azureStorageOptions.useCompositePrefixes
|
|
77
80
|
})(config);
|
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, StorageAdapter, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\
|
|
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, StorageAdapter, UploadCollectionSlug } from 'payload'\n\nimport { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'\n\nimport { createAzureAdapter } from './adapter.js'\nimport { getStorageClient as getStorageClientFunc } from './utils/getStorageClient.js'\n\nexport type AzureStorageOptions = {\n /**\n * Whether or not to allow the container to be created if it does not exist\n *\n * @default false\n */\n allowContainerCreate: boolean\n\n /**\n * Base URL for the Azure Blob storage account\n */\n baseURL: string\n\n /**\n * Optional cache key to identify the Azure Blob storage client instance.\n * If not provided, a default key will be used.\n *\n * @default `azure:containerName`\n */\n clientCacheKey?: string\n\n /**\n * Do uploads directly on the client to bypass limits on Vercel.\n *\n * Client uploads use the Azure Blob SDK, which splits large files into blocks\n * (avoiding the ~5GB limit of a single upload request). The SDK sends `x-ms-*`\n * headers, so the browser issues a CORS preflight: your storage account's CORS\n * rules must allow the `OPTIONS` and `PUT` methods and the required headers\n * (allowed headers `*`, or at minimum `x-ms-*,content-type,content-length`).\n */\n clientUploads?: ClientUploadsConfig\n\n /**\n * Collection options to apply the Azure Blob adapter to.\n */\n collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>\n\n /**\n * Azure Blob storage connection string\n */\n connectionString: string\n\n /**\n * Public access level applied to a container that the plugin creates via\n * `allowContainerCreate`. Has no effect on containers that already exist.\n *\n * - `'private'` (default): no anonymous access. Blobs are reachable only\n * through Payload's access-controlled file route.\n * - `'blob'`: unauthenticated clients can read any blob directly from Azure.\n * - `'container'`: unauthenticated clients can read and list blobs directly.\n *\n * Only choose `'blob'` or `'container'` if you deliberately want files served\n * publicly from Azure, bypassing Payload read access control.\n *\n * @default 'private'\n */\n containerAccess?: 'blob' | 'container' | 'private'\n\n /**\n * Azure Blob storage container name\n */\n containerName: string\n\n /**\n * Whether or not to enable the plugin\n *\n * Default: true\n */\n enabled?: boolean\n /**\n * When true, the collection-level prefix and document-level prefix are combined\n * (compositional). When false (default), a document prefix already within the\n * collection prefix is used as-is for new uploads; otherwise it is nested beneath it.\n * Existing files retain their stored prefixes for reads, URLs, and cleanup.\n *\n * Example with a document prefix already contained by the collection prefix:\n * - collection prefix: `uploads/`\n * - document prefix: `uploads/documents/`\n * - resulting prefix with useCompositePrefixes=true: `uploads/uploads/documents/`\n * - resulting prefix with useCompositePrefixes=false: `uploads/documents/`\n *\n * @default false\n */\n useCompositePrefixes?: boolean\n}\n\ntype AzureStorageFactory = (azureStorageArgs: AzureStorageOptions) => StorageAdapter\n\nexport const azureStorage: AzureStorageFactory = (\n azureStorageOptions: AzureStorageOptions,\n): StorageAdapter => ({\n name: 'azure',\n collections: Object.keys(azureStorageOptions.collections),\n init: (incomingConfig: Config): Config => {\n const getStorageClient = () =>\n getStorageClientFunc({\n connectionString: azureStorageOptions.connectionString,\n containerName: azureStorageOptions.containerName,\n })\n\n const isPluginDisabled = azureStorageOptions.enabled === false\n\n if (isPluginDisabled) {\n // Still call cloudStoragePlugin with adapter: null so fields (like prefix) are\n // inserted into the schema, keeping it consistent across environments.\n const collectionsWithoutAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n azureStorageOptions.collections,\n ).reduce(\n (acc, [slug, collOptions]) => ({\n ...acc,\n [slug]: { ...(collOptions === true ? {} : collOptions), adapter: null },\n }),\n {} as Record<string, CollectionOptions>,\n )\n\n return cloudStoragePlugin({\n collections: collectionsWithoutAdapter,\n enabled: false,\n useCompositePrefixes: azureStorageOptions.useCompositePrefixes,\n })(incomingConfig)\n }\n\n const createContainerIfNotExists = async (): Promise<void> => {\n const containerClient = getStorageClientFunc({\n connectionString: azureStorageOptions.connectionString,\n containerName: azureStorageOptions.containerName,\n })\n\n // Private by default; public access is opt-in via `containerAccess`.\n const containerAccess = azureStorageOptions.containerAccess ?? 'private'\n\n if (containerAccess === 'private') {\n await containerClient.createIfNotExists()\n } else {\n await containerClient.createIfNotExists({ access: containerAccess })\n }\n }\n\n const adapter = createAzureAdapter({\n allowContainerCreate: azureStorageOptions.allowContainerCreate,\n baseURL: azureStorageOptions.baseURL,\n clientUploads: azureStorageOptions.clientUploads,\n containerName: azureStorageOptions.containerName,\n createContainerIfNotExists,\n getStorageClient,\n useCompositePrefixes: azureStorageOptions.useCompositePrefixes,\n })\n\n // Add adapter to each collection option object\n const collectionsWithAdapter: CloudStoragePluginOptions['collections'] = Object.entries(\n azureStorageOptions.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 collections: collectionsWithAdapter,\n useCompositePrefixes: azureStorageOptions.useCompositePrefixes,\n })(config)\n },\n})\n\nexport { getStorageClientFunc as getStorageClient }\n"],"names":["cloudStoragePlugin","createAzureAdapter","getStorageClient","getStorageClientFunc","azureStorage","azureStorageOptions","name","collections","Object","keys","init","incomingConfig","connectionString","containerName","isPluginDisabled","enabled","collectionsWithoutAdapter","entries","reduce","acc","slug","collOptions","adapter","useCompositePrefixes","createContainerIfNotExists","containerClient","containerAccess","createIfNotExists","access","allowContainerCreate","baseURL","clientUploads","collectionsWithAdapter","config","map","collection","upload","disableLocalStorage"],"mappings":"AAOA,SAASA,kBAAkB,QAAQ,mCAAkC;AAErE,SAASC,kBAAkB,QAAQ,eAAc;AACjD,SAASC,oBAAoBC,oBAAoB,QAAQ,8BAA6B;AA0FtF,OAAO,MAAMC,eAAoC,CAC/CC,sBACoB,CAAA;QACpBC,MAAM;QACNC,aAAaC,OAAOC,IAAI,CAACJ,oBAAoBE,WAAW;QACxDG,MAAM,CAACC;YACL,MAAMT,mBAAmB,IACvBC,qBAAqB;oBACnBS,kBAAkBP,oBAAoBO,gBAAgB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C7B,GAAGF,GAAG;oBACN,CAACC,KAAK,EAAE;wBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;wBAC3CC;oBACF;gBACF,CAAA,GACA,CAAC;YAGH,gFAAgF;YAChF,MAAMW,SAAS;gBACb,GAAGtB,cAAc;gBACjBJ,aAAa,AAACI,CAAAA,eAAeJ,WAAW,IAAI,EAAE,AAAD,EAAG2B,GAAG,CAAC,CAACC;oBACnD,IAAI,CAACH,sBAAsB,CAACG,WAAWf,IAAI,CAAC,EAAE;wBAC5C,OAAOe;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,OAAOrC,mBAAmB;gBACxBO,aAAayB;gBACbT,sBAAsBlB,oBAAoBkB,oBAAoB;YAChE,GAAGU;QACL;IACF,CAAA,EAAE;AAEF,SAAS9B,wBAAwBD,gBAAgB,GAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isClientUploadAllowed.d.ts","sourceRoot":"","sources":["../src/isClientUploadAllowed.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,gBAAiB;IACjD,MAAM,CAAC,EAAE;QAAE,wBAAwB,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAA;CAC1D,KAAG,OAKD,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/isClientUploadAllowed.ts"],"sourcesContent":["export const isClientUploadAllowed = (collection?: {\n upload?: { allowRestrictedFileTypes?: boolean } | boolean\n}): boolean =>\n Boolean(\n collection &&\n typeof collection.upload === 'object' &&\n collection.upload.allowRestrictedFileTypes,\n )\n"],"names":["isClientUploadAllowed","collection","Boolean","upload","allowRestrictedFileTypes"],"mappings":"AAAA,OAAO,MAAMA,wBAAwB,CAACC,aAGpCC,QACED,cACE,OAAOA,WAAWE,MAAM,KAAK,YAC7BF,WAAWE,MAAM,CAACC,wBAAwB,EAC7C"}
|
package/dist/uploadFile.d.ts
CHANGED
|
@@ -2,13 +2,10 @@ import type { ContainerClient } from '@azure/storage-blob';
|
|
|
2
2
|
interface UploadArgs {
|
|
3
3
|
buffer: Buffer;
|
|
4
4
|
client: ContainerClient;
|
|
5
|
-
collectionPrefix?: string;
|
|
6
|
-
docPrefix?: string;
|
|
7
|
-
filename: string;
|
|
8
5
|
mimeType: string;
|
|
6
|
+
storageFilePath: string;
|
|
9
7
|
tempFilePath?: string;
|
|
10
|
-
useCompositePrefixes?: boolean;
|
|
11
8
|
}
|
|
12
|
-
export declare function uploadFile({ buffer, client,
|
|
9
|
+
export declare function uploadFile({ buffer, client, mimeType, storageFilePath, tempFilePath, }: UploadArgs): Promise<void>;
|
|
13
10
|
export {};
|
|
14
11
|
//# sourceMappingURL=uploadFile.d.ts.map
|
package/dist/uploadFile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../src/uploadFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../src/uploadFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAM1D,UAAU,UAAU;IAClB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,eAAe,CAAA;IACvB,QAAQ,EAAE,MAAM,CAAA;IAChB,eAAe,EAAE,MAAM,CAAA;IACvB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAID,wBAAsB,UAAU,CAAC,EAC/B,MAAM,EACN,MAAM,EACN,QAAQ,EACR,eAAe,EACf,YAAY,GACb,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAmB5B"}
|
package/dist/uploadFile.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { AbortController } from '@azure/abort-controller';
|
|
2
|
-
import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import { Readable } from 'stream';
|
|
5
4
|
const multipartThreshold = 1024 * 1024 * 50 // 50MB
|
|
6
5
|
;
|
|
7
|
-
export async function uploadFile({ buffer, client,
|
|
8
|
-
const
|
|
9
|
-
collectionPrefix,
|
|
10
|
-
docPrefix,
|
|
11
|
-
filename,
|
|
12
|
-
useCompositePrefixes
|
|
13
|
-
});
|
|
14
|
-
const blockBlobClient = client.getBlockBlobClient(fileKey);
|
|
6
|
+
export async function uploadFile({ buffer, client, mimeType, storageFilePath, tempFilePath }) {
|
|
7
|
+
const blockBlobClient = client.getBlockBlobClient(storageFilePath);
|
|
15
8
|
// when there are no temp files, or the upload is less than the threshold size, do not stream files
|
|
16
9
|
if (!tempFilePath && buffer.length > 0 && buffer.length < multipartThreshold) {
|
|
17
10
|
await blockBlobClient.upload(buffer, buffer.byteLength, {
|
package/dist/uploadFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/uploadFile.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\n\nimport { AbortController } from '@azure/abort-controller'\nimport
|
|
1
|
+
{"version":3,"sources":["../src/uploadFile.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\n\nimport { AbortController } from '@azure/abort-controller'\nimport fs from 'fs'\nimport { Readable } from 'stream'\n\ninterface UploadArgs {\n buffer: Buffer\n client: ContainerClient\n mimeType: string\n storageFilePath: string\n tempFilePath?: string\n}\n\nconst multipartThreshold = 1024 * 1024 * 50 // 50MB\n\nexport async function uploadFile({\n buffer,\n client,\n mimeType,\n storageFilePath,\n tempFilePath,\n}: UploadArgs): Promise<void> {\n const blockBlobClient = client.getBlockBlobClient(storageFilePath)\n\n // when there are no temp files, or the upload is less than the threshold size, do not stream files\n if (!tempFilePath && buffer.length > 0 && buffer.length < multipartThreshold) {\n await blockBlobClient.upload(buffer, buffer.byteLength, {\n blobHTTPHeaders: { blobContentType: mimeType },\n })\n return\n }\n\n const fileBufferOrStream: Readable = tempFilePath\n ? fs.createReadStream(tempFilePath)\n : Readable.from(buffer)\n\n await blockBlobClient.uploadStream(fileBufferOrStream, 4 * 1024 * 1024, 4, {\n abortSignal: AbortController.timeout(30 * 60 * 1000),\n blobHTTPHeaders: { blobContentType: mimeType },\n })\n}\n"],"names":["AbortController","fs","Readable","multipartThreshold","uploadFile","buffer","client","mimeType","storageFilePath","tempFilePath","blockBlobClient","getBlockBlobClient","length","upload","byteLength","blobHTTPHeaders","blobContentType","fileBufferOrStream","createReadStream","from","uploadStream","abortSignal","timeout"],"mappings":"AAEA,SAASA,eAAe,QAAQ,0BAAyB;AACzD,OAAOC,QAAQ,KAAI;AACnB,SAASC,QAAQ,QAAQ,SAAQ;AAUjC,MAAMC,qBAAqB,OAAO,OAAO,GAAG,OAAO;;AAEnD,OAAO,eAAeC,WAAW,EAC/BC,MAAM,EACNC,MAAM,EACNC,QAAQ,EACRC,eAAe,EACfC,YAAY,EACD;IACX,MAAMC,kBAAkBJ,OAAOK,kBAAkB,CAACH;IAElD,mGAAmG;IACnG,IAAI,CAACC,gBAAgBJ,OAAOO,MAAM,GAAG,KAAKP,OAAOO,MAAM,GAAGT,oBAAoB;QAC5E,MAAMO,gBAAgBG,MAAM,CAACR,QAAQA,OAAOS,UAAU,EAAE;YACtDC,iBAAiB;gBAAEC,iBAAiBT;YAAS;QAC/C;QACA;IACF;IAEA,MAAMU,qBAA+BR,eACjCR,GAAGiB,gBAAgB,CAACT,gBACpBP,SAASiB,IAAI,CAACd;IAElB,MAAMK,gBAAgBU,YAAY,CAACH,oBAAoB,IAAI,OAAO,MAAM,GAAG;QACzEI,aAAarB,gBAAgBsB,OAAO,CAAC,KAAK,KAAK;QAC/CP,iBAAiB;YAAEC,iBAAiBT;QAAS;IAC/C;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-azure",
|
|
3
|
-
"version": "4.0.0-internal.
|
|
3
|
+
"version": "4.0.0-internal.d21c039",
|
|
4
4
|
"description": "Payload storage adapter for Azure Blob Storage",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -38,14 +38,14 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@azure/abort-controller": "^1.1.0",
|
|
41
|
-
"@azure/storage-blob": "^12.
|
|
42
|
-
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.
|
|
41
|
+
"@azure/storage-blob": "^12.33.0",
|
|
42
|
+
"@payloadcms/plugin-cloud-storage": "4.0.0-internal.d21c039"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"payload": "4.0.0-internal.
|
|
45
|
+
"payload": "4.0.0-internal.d21c039"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"payload": "4.0.0-internal.
|
|
48
|
+
"payload": "4.0.0-internal.d21c039"
|
|
49
49
|
},
|
|
50
50
|
"engines": {
|
|
51
51
|
"node": ">=24.15.0"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"handleAzureUpload.spec.d.ts","sourceRoot":"","sources":["../../src/client/handleAzureUpload.spec.ts"],"names":[],"mappings":""}
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
const uploadDataMock = vi.fn();
|
|
3
|
-
const blockBlobClientMock = vi.fn(function() {
|
|
4
|
-
return {
|
|
5
|
-
uploadData: uploadDataMock
|
|
6
|
-
};
|
|
7
|
-
});
|
|
8
|
-
vi.mock('@azure/storage-blob', ()=>({
|
|
9
|
-
BlockBlobClient: blockBlobClientMock
|
|
10
|
-
}));
|
|
11
|
-
import { handleAzureUpload } from './handleAzureUpload.js';
|
|
12
|
-
const serverURL = 'https://example.com';
|
|
13
|
-
const apiRoute = '/api';
|
|
14
|
-
const serverHandlerPath = '/storage-azure-generate-signed-url';
|
|
15
|
-
const signedURL = 'https://account.blob.core.windows.net/container/file.png?sig=abc';
|
|
16
|
-
const createFile = ()=>new File([
|
|
17
|
-
new Uint8Array([
|
|
18
|
-
1,
|
|
19
|
-
2,
|
|
20
|
-
3
|
|
21
|
-
])
|
|
22
|
-
], 'file.png', {
|
|
23
|
-
type: 'image/png'
|
|
24
|
-
});
|
|
25
|
-
const mockSignedURLResponse = (body, ok = true)=>{
|
|
26
|
-
const fetchMock = vi.fn().mockResolvedValue({
|
|
27
|
-
json: ()=>Promise.resolve(body),
|
|
28
|
-
ok
|
|
29
|
-
});
|
|
30
|
-
vi.stubGlobal('fetch', fetchMock);
|
|
31
|
-
return fetchMock;
|
|
32
|
-
};
|
|
33
|
-
describe('handleAzureUpload', ()=>{
|
|
34
|
-
beforeEach(()=>{
|
|
35
|
-
vi.clearAllMocks();
|
|
36
|
-
vi.unstubAllGlobals();
|
|
37
|
-
uploadDataMock.mockResolvedValue(undefined);
|
|
38
|
-
});
|
|
39
|
-
it('should request a signed URL from the server handler', async ()=>{
|
|
40
|
-
const fetchMock = mockSignedURLResponse({
|
|
41
|
-
docPrefix: 'docs',
|
|
42
|
-
url: signedURL
|
|
43
|
-
});
|
|
44
|
-
await handleAzureUpload({
|
|
45
|
-
apiRoute,
|
|
46
|
-
collectionSlug: 'media',
|
|
47
|
-
docPrefix: 'docs',
|
|
48
|
-
file: createFile(),
|
|
49
|
-
serverHandlerPath,
|
|
50
|
-
serverURL,
|
|
51
|
-
updateFilename: vi.fn()
|
|
52
|
-
});
|
|
53
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
54
|
-
const [calledURL, init] = fetchMock.mock.calls[0];
|
|
55
|
-
expect(calledURL).toBe(`${serverURL}${apiRoute}${serverHandlerPath}`);
|
|
56
|
-
expect(init.method).toBe('POST');
|
|
57
|
-
expect(JSON.parse(init.body)).toEqual({
|
|
58
|
-
collectionSlug: 'media',
|
|
59
|
-
docPrefix: 'docs',
|
|
60
|
-
filename: 'file.png',
|
|
61
|
-
mimeType: 'image/png'
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
it('should upload via BlockBlobClient.uploadData rather than a raw PUT', async ()=>{
|
|
65
|
-
const fetchMock = mockSignedURLResponse({
|
|
66
|
-
docPrefix: 'docs',
|
|
67
|
-
url: signedURL
|
|
68
|
-
});
|
|
69
|
-
const file = createFile();
|
|
70
|
-
await handleAzureUpload({
|
|
71
|
-
apiRoute,
|
|
72
|
-
collectionSlug: 'media',
|
|
73
|
-
file,
|
|
74
|
-
serverHandlerPath,
|
|
75
|
-
serverURL,
|
|
76
|
-
updateFilename: vi.fn()
|
|
77
|
-
});
|
|
78
|
-
expect(blockBlobClientMock).toHaveBeenCalledWith(signedURL);
|
|
79
|
-
expect(uploadDataMock).toHaveBeenCalledTimes(1);
|
|
80
|
-
const [uploadedFile, options] = uploadDataMock.mock.calls[0];
|
|
81
|
-
expect(uploadedFile).toBe(file);
|
|
82
|
-
expect(options.blockSize).toBeGreaterThan(0);
|
|
83
|
-
expect(options.concurrency).toBeGreaterThan(0);
|
|
84
|
-
// Only the signed-URL request should hit fetch; no raw PUT to the blob URL.
|
|
85
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
86
|
-
});
|
|
87
|
-
it('should pass the file content type to the blob headers', async ()=>{
|
|
88
|
-
mockSignedURLResponse({
|
|
89
|
-
docPrefix: 'docs',
|
|
90
|
-
url: signedURL
|
|
91
|
-
});
|
|
92
|
-
await handleAzureUpload({
|
|
93
|
-
apiRoute,
|
|
94
|
-
collectionSlug: 'media',
|
|
95
|
-
file: createFile(),
|
|
96
|
-
serverHandlerPath,
|
|
97
|
-
serverURL,
|
|
98
|
-
updateFilename: vi.fn()
|
|
99
|
-
});
|
|
100
|
-
const [, options] = uploadDataMock.mock.calls[0];
|
|
101
|
-
expect(options.blobHTTPHeaders).toEqual({
|
|
102
|
-
blobContentType: 'image/png'
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
it('should call updateFilename when the server returns a sanitized filename', async ()=>{
|
|
106
|
-
mockSignedURLResponse({
|
|
107
|
-
docPrefix: 'docs',
|
|
108
|
-
filename: 'file-1.png',
|
|
109
|
-
url: signedURL
|
|
110
|
-
});
|
|
111
|
-
const updateFilename = vi.fn();
|
|
112
|
-
await handleAzureUpload({
|
|
113
|
-
apiRoute,
|
|
114
|
-
collectionSlug: 'media',
|
|
115
|
-
file: createFile(),
|
|
116
|
-
serverHandlerPath,
|
|
117
|
-
serverURL,
|
|
118
|
-
updateFilename
|
|
119
|
-
});
|
|
120
|
-
expect(updateFilename).toHaveBeenCalledWith('file-1.png');
|
|
121
|
-
});
|
|
122
|
-
it('should not call updateFilename when the filename is unchanged', async ()=>{
|
|
123
|
-
mockSignedURLResponse({
|
|
124
|
-
docPrefix: 'docs',
|
|
125
|
-
filename: 'file.png',
|
|
126
|
-
url: signedURL
|
|
127
|
-
});
|
|
128
|
-
const updateFilename = vi.fn();
|
|
129
|
-
await handleAzureUpload({
|
|
130
|
-
apiRoute,
|
|
131
|
-
collectionSlug: 'media',
|
|
132
|
-
file: createFile(),
|
|
133
|
-
serverHandlerPath,
|
|
134
|
-
serverURL,
|
|
135
|
-
updateFilename
|
|
136
|
-
});
|
|
137
|
-
expect(updateFilename).not.toHaveBeenCalled();
|
|
138
|
-
});
|
|
139
|
-
it('should return the sanitized doc prefix', async ()=>{
|
|
140
|
-
mockSignedURLResponse({
|
|
141
|
-
docPrefix: 'sanitized-docs',
|
|
142
|
-
url: signedURL
|
|
143
|
-
});
|
|
144
|
-
const result = await handleAzureUpload({
|
|
145
|
-
apiRoute,
|
|
146
|
-
collectionSlug: 'media',
|
|
147
|
-
file: createFile(),
|
|
148
|
-
serverHandlerPath,
|
|
149
|
-
serverURL,
|
|
150
|
-
updateFilename: vi.fn()
|
|
151
|
-
});
|
|
152
|
-
expect(result).toEqual({
|
|
153
|
-
prefix: 'sanitized-docs'
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
it('should throw when the signed URL request fails', async ()=>{
|
|
157
|
-
mockSignedURLResponse({}, false);
|
|
158
|
-
await expect(handleAzureUpload({
|
|
159
|
-
apiRoute,
|
|
160
|
-
collectionSlug: 'media',
|
|
161
|
-
file: createFile(),
|
|
162
|
-
serverHandlerPath,
|
|
163
|
-
serverURL,
|
|
164
|
-
updateFilename: vi.fn()
|
|
165
|
-
})).rejects.toThrow();
|
|
166
|
-
expect(uploadDataMock).not.toHaveBeenCalled();
|
|
167
|
-
});
|
|
168
|
-
it('should propagate errors from uploadData', async ()=>{
|
|
169
|
-
mockSignedURLResponse({
|
|
170
|
-
docPrefix: 'docs',
|
|
171
|
-
url: signedURL
|
|
172
|
-
});
|
|
173
|
-
uploadDataMock.mockRejectedValue(new Error('block upload failed'));
|
|
174
|
-
await expect(handleAzureUpload({
|
|
175
|
-
apiRoute,
|
|
176
|
-
collectionSlug: 'media',
|
|
177
|
-
file: createFile(),
|
|
178
|
-
serverHandlerPath,
|
|
179
|
-
serverURL,
|
|
180
|
-
updateFilename: vi.fn()
|
|
181
|
-
})).rejects.toThrow('block upload failed');
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
//# sourceMappingURL=handleAzureUpload.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/handleAzureUpload.spec.ts"],"sourcesContent":["import { beforeEach, describe, expect, it, vi } from 'vitest'\n\nconst uploadDataMock = vi.fn()\nconst blockBlobClientMock = vi.fn(function () {\n return { uploadData: uploadDataMock }\n})\n\nvi.mock('@azure/storage-blob', () => ({\n BlockBlobClient: blockBlobClientMock,\n}))\n\nimport { handleAzureUpload } from './handleAzureUpload.js'\n\nconst serverURL = 'https://example.com'\nconst apiRoute = '/api'\nconst serverHandlerPath = '/storage-azure-generate-signed-url' as const\nconst signedURL = 'https://account.blob.core.windows.net/container/file.png?sig=abc'\n\nconst createFile = () => new File([new Uint8Array([1, 2, 3])], 'file.png', { type: 'image/png' })\n\nconst mockSignedURLResponse = (body: Record<string, unknown>, ok = true) => {\n const fetchMock = vi.fn().mockResolvedValue({\n json: () => Promise.resolve(body),\n ok,\n })\n vi.stubGlobal('fetch', fetchMock)\n return fetchMock\n}\n\ndescribe('handleAzureUpload', () => {\n beforeEach(() => {\n vi.clearAllMocks()\n vi.unstubAllGlobals()\n uploadDataMock.mockResolvedValue(undefined)\n })\n\n it('should request a signed URL from the server handler', async () => {\n const fetchMock = mockSignedURLResponse({ docPrefix: 'docs', url: signedURL })\n\n await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n docPrefix: 'docs',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n })\n\n expect(fetchMock).toHaveBeenCalledTimes(1)\n const [calledURL, init] = fetchMock.mock.calls[0]!\n\n expect(calledURL).toBe(`${serverURL}${apiRoute}${serverHandlerPath}`)\n expect(init.method).toBe('POST')\n expect(JSON.parse(init.body)).toEqual({\n collectionSlug: 'media',\n docPrefix: 'docs',\n filename: 'file.png',\n mimeType: 'image/png',\n })\n })\n\n it('should upload via BlockBlobClient.uploadData rather than a raw PUT', async () => {\n const fetchMock = mockSignedURLResponse({ docPrefix: 'docs', url: signedURL })\n const file = createFile()\n\n await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file,\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n })\n\n expect(blockBlobClientMock).toHaveBeenCalledWith(signedURL)\n expect(uploadDataMock).toHaveBeenCalledTimes(1)\n\n const [uploadedFile, options] = uploadDataMock.mock.calls[0]!\n expect(uploadedFile).toBe(file)\n expect(options.blockSize).toBeGreaterThan(0)\n expect(options.concurrency).toBeGreaterThan(0)\n\n // Only the signed-URL request should hit fetch; no raw PUT to the blob URL.\n expect(fetchMock).toHaveBeenCalledTimes(1)\n })\n\n it('should pass the file content type to the blob headers', async () => {\n mockSignedURLResponse({ docPrefix: 'docs', url: signedURL })\n\n await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n })\n\n const [, options] = uploadDataMock.mock.calls[0]!\n expect(options.blobHTTPHeaders).toEqual({ blobContentType: 'image/png' })\n })\n\n it('should call updateFilename when the server returns a sanitized filename', async () => {\n mockSignedURLResponse({ docPrefix: 'docs', filename: 'file-1.png', url: signedURL })\n const updateFilename = vi.fn()\n\n await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename,\n })\n\n expect(updateFilename).toHaveBeenCalledWith('file-1.png')\n })\n\n it('should not call updateFilename when the filename is unchanged', async () => {\n mockSignedURLResponse({ docPrefix: 'docs', filename: 'file.png', url: signedURL })\n const updateFilename = vi.fn()\n\n await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename,\n })\n\n expect(updateFilename).not.toHaveBeenCalled()\n })\n\n it('should return the sanitized doc prefix', async () => {\n mockSignedURLResponse({ docPrefix: 'sanitized-docs', url: signedURL })\n\n const result = await handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n })\n\n expect(result).toEqual({ prefix: 'sanitized-docs' })\n })\n\n it('should throw when the signed URL request fails', async () => {\n mockSignedURLResponse({}, false)\n\n await expect(\n handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n }),\n ).rejects.toThrow()\n\n expect(uploadDataMock).not.toHaveBeenCalled()\n })\n\n it('should propagate errors from uploadData', async () => {\n mockSignedURLResponse({ docPrefix: 'docs', url: signedURL })\n uploadDataMock.mockRejectedValue(new Error('block upload failed'))\n\n await expect(\n handleAzureUpload({\n apiRoute,\n collectionSlug: 'media',\n file: createFile(),\n serverHandlerPath,\n serverURL,\n updateFilename: vi.fn(),\n }),\n ).rejects.toThrow('block upload failed')\n })\n})\n"],"names":["beforeEach","describe","expect","it","vi","uploadDataMock","fn","blockBlobClientMock","uploadData","mock","BlockBlobClient","handleAzureUpload","serverURL","apiRoute","serverHandlerPath","signedURL","createFile","File","Uint8Array","type","mockSignedURLResponse","body","ok","fetchMock","mockResolvedValue","json","Promise","resolve","stubGlobal","clearAllMocks","unstubAllGlobals","undefined","docPrefix","url","collectionSlug","file","updateFilename","toHaveBeenCalledTimes","calledURL","init","calls","toBe","method","JSON","parse","toEqual","filename","mimeType","toHaveBeenCalledWith","uploadedFile","options","blockSize","toBeGreaterThan","concurrency","blobHTTPHeaders","blobContentType","not","toHaveBeenCalled","result","prefix","rejects","toThrow","mockRejectedValue","Error"],"mappings":"AAAA,SAASA,UAAU,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,SAAQ;AAE7D,MAAMC,iBAAiBD,GAAGE,EAAE;AAC5B,MAAMC,sBAAsBH,GAAGE,EAAE,CAAC;IAChC,OAAO;QAAEE,YAAYH;IAAe;AACtC;AAEAD,GAAGK,IAAI,CAAC,uBAAuB,IAAO,CAAA;QACpCC,iBAAiBH;IACnB,CAAA;AAEA,SAASI,iBAAiB,QAAQ,yBAAwB;AAE1D,MAAMC,YAAY;AAClB,MAAMC,WAAW;AACjB,MAAMC,oBAAoB;AAC1B,MAAMC,YAAY;AAElB,MAAMC,aAAa,IAAM,IAAIC,KAAK;QAAC,IAAIC,WAAW;YAAC;YAAG;YAAG;SAAE;KAAE,EAAE,YAAY;QAAEC,MAAM;IAAY;AAE/F,MAAMC,wBAAwB,CAACC,MAA+BC,KAAK,IAAI;IACrE,MAAMC,YAAYnB,GAAGE,E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,EAAE;QACvB;QAEAJ,OAAOwD,QAAQb,OAAO,CAAC;YAAEc,QAAQ;QAAiB;IACpD;IAEAxD,GAAG,kDAAkD;QACnDiB,sBAAsB,CAAC,GAAG;QAE1B,MAAMlB,OACJS,kBAAkB;YAChBE;YACAqB,gBAAgB;YAChBC,MAAMnB;YACNF;YACAF;YACAwB,gBAAgBhC,GAAGE,EAAE;QACvB,IACAsD,OAAO,CAACC,OAAO;QAEjB3D,OAAOG,gBAAgBmD,GAAG,CAACC,gBAAgB;IAC7C;IAEAtD,GAAG,2CAA2C;QAC5CiB,sBAAsB;YAAEY,WAAW;YAAQC,KAAKlB;QAAU;QAC1DV,eAAeyD,iBAAiB,CAAC,IAAIC,MAAM;QAE3C,MAAM7D,OACJS,kBAAkB;YAChBE;YACAqB,gBAAgB;YAChBC,MAAMnB;YACNF;YACAF;YACAwB,gBAAgBhC,GAAGE,EAAE;QACvB,IACAsD,OAAO,CAACC,OAAO,CAAC;IACpB;AACF"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { ContainerClient } from '@azure/storage-blob';
|
|
2
|
-
import type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types';
|
|
3
|
-
import type { PayloadHandler } from 'payload';
|
|
4
|
-
import type { AzureStorageOptions } from './index.js';
|
|
5
|
-
interface Args {
|
|
6
|
-
access?: ClientUploadsAccess;
|
|
7
|
-
collections: AzureStorageOptions['collections'];
|
|
8
|
-
containerName: string;
|
|
9
|
-
getStorageClient: () => ContainerClient;
|
|
10
|
-
useCompositePrefixes?: boolean;
|
|
11
|
-
}
|
|
12
|
-
export declare const getGenerateSignedURLHandler: ({ access, collections, containerName, getStorageClient, useCompositePrefixes, }: Args) => PayloadHandler;
|
|
13
|
-
export {};
|
|
14
|
-
//# sourceMappingURL=generateSignedURL.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generateSignedURL.d.ts","sourceRoot":"","sources":["../src/generateSignedURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA8B,MAAM,qBAAqB,CAAA;AACtF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAM7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErD,UAAU,IAAI;IACZ,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,WAAW,EAAE,mBAAmB,CAAC,aAAa,CAAC,CAAA;IAC/C,aAAa,EAAE,MAAM,CAAA;IACrB,gBAAgB,EAAE,MAAM,eAAe,CAAA;IACvC,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAID,eAAO,MAAM,2BAA2B,oFAMrC,IAAI,KAAG,cAsDT,CAAA"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { BlobSASPermissions, generateBlobSASQueryParameters } from '@azure/storage-blob';
|
|
2
|
-
import { resolveSignedURLKey } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
3
|
-
import { APIError, Forbidden } from 'payload';
|
|
4
|
-
const defaultAccess = ({ req })=>!!req.user;
|
|
5
|
-
export const getGenerateSignedURLHandler = ({ access = defaultAccess, collections, containerName, getStorageClient, useCompositePrefixes = false })=>{
|
|
6
|
-
return async (req)=>{
|
|
7
|
-
if (!req.json) {
|
|
8
|
-
throw new APIError('Unreachable');
|
|
9
|
-
}
|
|
10
|
-
const { collectionSlug, docPrefix, filename, mimeType } = await req.json();
|
|
11
|
-
const collectionStorageConfig = collections[collectionSlug];
|
|
12
|
-
if (!collectionStorageConfig) {
|
|
13
|
-
throw new APIError(`Collection ${collectionSlug} was not found in Azure storage options`);
|
|
14
|
-
}
|
|
15
|
-
const collectionPrefix = typeof collectionStorageConfig === 'object' && collectionStorageConfig.prefix || '';
|
|
16
|
-
if (!await access({
|
|
17
|
-
collectionSlug,
|
|
18
|
-
req
|
|
19
|
-
})) {
|
|
20
|
-
throw new Forbidden();
|
|
21
|
-
}
|
|
22
|
-
const { fileKey, sanitizedDocPrefix, sanitizedFilename } = await resolveSignedURLKey({
|
|
23
|
-
collectionPrefix,
|
|
24
|
-
collectionSlug,
|
|
25
|
-
docPrefix,
|
|
26
|
-
filename,
|
|
27
|
-
req,
|
|
28
|
-
useCompositePrefixes
|
|
29
|
-
});
|
|
30
|
-
const blobClient = getStorageClient().getBlobClient(fileKey);
|
|
31
|
-
const sasToken = generateBlobSASQueryParameters({
|
|
32
|
-
blobName: fileKey,
|
|
33
|
-
containerName,
|
|
34
|
-
contentType: mimeType,
|
|
35
|
-
expiresOn: new Date(Date.now() + 3 * 60 * 60 * 1000),
|
|
36
|
-
permissions: BlobSASPermissions.parse('w'),
|
|
37
|
-
startsOn: new Date()
|
|
38
|
-
}, getStorageClient().credential);
|
|
39
|
-
return Response.json({
|
|
40
|
-
docPrefix: sanitizedDocPrefix,
|
|
41
|
-
filename: sanitizedFilename,
|
|
42
|
-
url: `${blobClient.url}?${sasToken.toString()}`
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
//# sourceMappingURL=generateSignedURL.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generateSignedURL.ts"],"sourcesContent":["import type { ContainerClient, StorageSharedKeyCredential } from '@azure/storage-blob'\nimport type { ClientUploadsAccess } from '@payloadcms/plugin-cloud-storage/types'\nimport type { PayloadHandler } from 'payload'\n\nimport { BlobSASPermissions, generateBlobSASQueryParameters } from '@azure/storage-blob'\nimport { resolveSignedURLKey } from '@payloadcms/plugin-cloud-storage/utilities'\nimport { APIError, Forbidden } from 'payload'\n\nimport type { AzureStorageOptions } from './index.js'\n\ninterface Args {\n access?: ClientUploadsAccess\n collections: AzureStorageOptions['collections']\n containerName: string\n getStorageClient: () => ContainerClient\n useCompositePrefixes?: boolean\n}\n\nconst defaultAccess: Args['access'] = ({ req }) => !!req.user\n\nexport const getGenerateSignedURLHandler = ({\n access = defaultAccess,\n collections,\n containerName,\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 Azure 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 blobClient = getStorageClient().getBlobClient(fileKey)\n\n const sasToken = generateBlobSASQueryParameters(\n {\n blobName: fileKey,\n containerName,\n contentType: mimeType,\n expiresOn: new Date(Date.now() + 3 * 60 * 60 * 1000),\n permissions: BlobSASPermissions.parse('w'),\n startsOn: new Date(),\n },\n getStorageClient().credential as StorageSharedKeyCredential,\n )\n\n return Response.json({\n docPrefix: sanitizedDocPrefix,\n filename: sanitizedFilename,\n url: `${blobClient.url}?${sasToken.toString()}`,\n })\n }\n}\n"],"names":["BlobSASPermissions","generateBlobSASQueryParameters","resolveSignedURLKey","APIError","Forbidden","defaultAccess","req","user","getGenerateSignedURLHandler","access","collections","containerName","getStorageClient","useCompositePrefixes","json","collectionSlug","docPrefix","filename","mimeType","collectionStorageConfig","collectionPrefix","prefix","fileKey","sanitizedDocPrefix","sanitizedFilename","blobClient","getBlobClient","sasToken","blobName","contentType","expiresOn","Date","now","permissions","parse","startsOn","credential","Response","url","toString"],"mappings":"AAIA,SAASA,kBAAkB,EAAEC,8BAA8B,QAAQ,sBAAqB;AACxF,SAASC,mBAAmB,QAAQ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