@payloadcms/storage-azure 3.83.0-internal.791f423 → 3.83.0-internal.86b7bfb

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.
@@ -0,0 +1,14 @@
1
+ import type { ContainerClient } from '@azure/storage-blob';
2
+ import type { Adapter, ClientUploadsConfig } from '@payloadcms/plugin-cloud-storage/types';
3
+ interface CreateAzureAdapterArgs {
4
+ allowContainerCreate: boolean;
5
+ baseURL: string;
6
+ clientUploads?: ClientUploadsConfig;
7
+ containerName: string;
8
+ createContainerIfNotExists: () => void;
9
+ getStorageClient: () => ContainerClient;
10
+ useCompositePrefixes?: boolean;
11
+ }
12
+ export declare function createAzureAdapter({ allowContainerCreate, baseURL, clientUploads, containerName, createContainerIfNotExists, getStorageClient, useCompositePrefixes, }: CreateAzureAdapterArgs): Adapter;
13
+ export {};
14
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EACV,OAAO,EACP,mBAAmB,EAEpB,MAAM,wCAAwC,CAAA;AAO/C,UAAU,sBAAsB;IAC9B,oBAAoB,EAAE,OAAO,CAAA;IAC7B,OAAO,EAAE,MAAM,CAAA;IACf,aAAa,CAAC,EAAE,mBAAmB,CAAA;IACnC,aAAa,EAAE,MAAM,CAAA;IACrB,0BAA0B,EAAE,MAAM,IAAI,CAAA;IACtC,gBAAgB,EAAE,MAAM,eAAe,CAAA;IACvC,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,wBAAgB,kBAAkB,CAAC,EACjC,oBAAoB,EACpB,OAAO,EACP,aAAa,EACb,aAAa,EACb,0BAA0B,EAC1B,gBAAgB,EAChB,oBAA4B,GAC7B,EAAE,sBAAsB,GAAG,OAAO,CAyDlC"}
@@ -0,0 +1,54 @@
1
+ import { deleteFile } from './deleteFile.js';
2
+ import { generateURL } from './generateURL.js';
3
+ import { getFile } from './getFile.js';
4
+ import { uploadFile } from './uploadFile.js';
5
+ export function createAzureAdapter({ allowContainerCreate, baseURL, clientUploads, containerName, createContainerIfNotExists, getStorageClient, useCompositePrefixes = false }) {
6
+ return ({ collection, prefix = '' })=>({
7
+ name: 'azure',
8
+ clientUploads,
9
+ generateURL: ({ filename, prefix: urlPrefix = '' })=>generateURL({
10
+ baseURL,
11
+ collectionPrefix: prefix,
12
+ containerName,
13
+ filename,
14
+ prefix: urlPrefix,
15
+ useCompositePrefixes
16
+ }),
17
+ handleDelete: ({ doc: { prefix: docPrefix = '' }, filename })=>deleteFile({
18
+ client: getStorageClient(),
19
+ collectionPrefix: prefix,
20
+ docPrefix,
21
+ filename,
22
+ useCompositePrefixes
23
+ }),
24
+ handleUpload: async ({ data, file })=>{
25
+ await uploadFile({
26
+ buffer: file.buffer,
27
+ client: getStorageClient(),
28
+ collectionPrefix: prefix,
29
+ docPrefix: data.prefix,
30
+ filename: file.filename,
31
+ mimeType: file.mimeType,
32
+ tempFilePath: file.tempFilePath,
33
+ useCompositePrefixes
34
+ });
35
+ return data;
36
+ },
37
+ staticHandler: (req, { headers, params: { clientUploadContext, filename, prefix: prefixQueryParam } })=>getFile({
38
+ client: getStorageClient(),
39
+ clientUploadContext,
40
+ collection,
41
+ collectionPrefix: prefix,
42
+ filename,
43
+ incomingHeaders: headers,
44
+ prefixQueryParam,
45
+ req,
46
+ useCompositePrefixes
47
+ }),
48
+ ...allowContainerCreate && {
49
+ onInit: createContainerIfNotExists
50
+ }
51
+ });
52
+ }
53
+
54
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/adapter.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\nimport type {\n Adapter,\n ClientUploadsConfig,\n GeneratedAdapter,\n} from '@payloadcms/plugin-cloud-storage/types'\n\nimport { deleteFile } from './deleteFile.js'\nimport { generateURL } from './generateURL.js'\nimport { getFile } from './getFile.js'\nimport { uploadFile } from './uploadFile.js'\n\ninterface CreateAzureAdapterArgs {\n allowContainerCreate: boolean\n baseURL: string\n clientUploads?: ClientUploadsConfig\n containerName: string\n createContainerIfNotExists: () => void\n getStorageClient: () => ContainerClient\n useCompositePrefixes?: boolean\n}\n\nexport function createAzureAdapter({\n allowContainerCreate,\n baseURL,\n clientUploads,\n containerName,\n createContainerIfNotExists,\n getStorageClient,\n useCompositePrefixes = false,\n}: CreateAzureAdapterArgs): Adapter {\n return ({ collection, prefix = '' }): GeneratedAdapter => ({\n name: 'azure',\n clientUploads,\n\n generateURL: ({ filename, prefix: urlPrefix = '' }) =>\n generateURL({\n baseURL,\n collectionPrefix: prefix,\n containerName,\n filename,\n prefix: urlPrefix,\n useCompositePrefixes,\n }),\n\n handleDelete: ({ doc: { prefix: docPrefix = '' }, filename }) =>\n deleteFile({\n client: getStorageClient(),\n collectionPrefix: prefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n }),\n\n handleUpload: async ({ data, file }) => {\n await uploadFile({\n buffer: file.buffer,\n client: getStorageClient(),\n collectionPrefix: prefix,\n docPrefix: data.prefix,\n filename: file.filename,\n mimeType: file.mimeType,\n tempFilePath: file.tempFilePath,\n useCompositePrefixes,\n })\n\n return data\n },\n\n staticHandler: (\n req,\n { headers, params: { clientUploadContext, filename, prefix: prefixQueryParam } },\n ) =>\n getFile({\n client: getStorageClient(),\n clientUploadContext,\n collection,\n collectionPrefix: prefix,\n filename,\n incomingHeaders: headers,\n prefixQueryParam,\n req,\n useCompositePrefixes,\n }),\n\n ...(allowContainerCreate && { onInit: createContainerIfNotExists }),\n })\n}\n"],"names":["deleteFile","generateURL","getFile","uploadFile","createAzureAdapter","allowContainerCreate","baseURL","clientUploads","containerName","createContainerIfNotExists","getStorageClient","useCompositePrefixes","collection","prefix","name","filename","urlPrefix","collectionPrefix","handleDelete","doc","docPrefix","client","handleUpload","data","file","buffer","mimeType","tempFilePath","staticHandler","req","headers","params","clientUploadContext","prefixQueryParam","incomingHeaders","onInit"],"mappings":"AAOA,SAASA,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,OAAO,QAAQ,eAAc;AACtC,SAASC,UAAU,QAAQ,kBAAiB;AAY5C,OAAO,SAASC,mBAAmB,EACjCC,oBAAoB,EACpBC,OAAO,EACPC,aAAa,EACbC,aAAa,EACbC,0BAA0B,EAC1BC,gBAAgB,EAChBC,uBAAuB,KAAK,EACL;IACvB,OAAO,CAAC,EAAEC,UAAU,EAAEC,SAAS,EAAE,EAAE,GAAwB,CAAA;YACzDC,MAAM;YACNP;YAEAN,aAAa,CAAC,EAAEc,QAAQ,EAAEF,QAAQG,YAAY,EAAE,EAAE,GAChDf,YAAY;oBACVK;oBACAW,kBAAkBJ;oBAClBL;oBACAO;oBACAF,QAAQG;oBACRL;gBACF;YAEFO,cAAc,CAAC,EAAEC,KAAK,EAAEN,QAAQO,YAAY,EAAE,EAAE,EAAEL,QAAQ,EAAE,GAC1Df,WAAW;oBACTqB,QAAQX;oBACRO,kBAAkBJ;oBAClBO;oBACAL;oBACAJ;gBACF;YAEFW,cAAc,OAAO,EAAEC,IAAI,EAAEC,IAAI,EAAE;gBACjC,MAAMrB,WAAW;oBACfsB,QAAQD,KAAKC,MAAM;oBACnBJ,QAAQX;oBACRO,kBAAkBJ;oBAClBO,WAAWG,KAAKV,MAAM;oBACtBE,UAAUS,KAAKT,QAAQ;oBACvBW,UAAUF,KAAKE,QAAQ;oBACvBC,cAAcH,KAAKG,YAAY;oBAC/BhB;gBACF;gBAEA,OAAOY;YACT;YAEAK,eAAe,CACbC,KACA,EAAEC,OAAO,EAAEC,QAAQ,EAAEC,mBAAmB,EAAEjB,QAAQ,EAAEF,QAAQoB,gBAAgB,EAAE,EAAE,GAEhF/B,QAAQ;oBACNmB,QAAQX;oBACRsB;oBACApB;oBACAK,kBAAkBJ;oBAClBE;oBACAmB,iBAAiBJ;oBACjBG;oBACAJ;oBACAlB;gBACF;YAEF,GAAIN,wBAAwB;gBAAE8B,QAAQ1B;YAA2B,CAAC;QACpE,CAAA;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"AzureClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/AzureClientUploadHandler.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,wBAAwB;;;;;;;aAyBxB,OAAO,aASlB,CAAA"}
1
+ {"version":3,"file":"AzureClientUploadHandler.d.ts","sourceRoot":"","sources":["../../src/client/AzureClientUploadHandler.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,wBAAwB;;;;;;;aA2B7B,OADK,aAQX,CAAA"}
@@ -0,0 +1,11 @@
1
+ import type { ContainerClient } from '@azure/storage-blob';
2
+ interface DeleteArgs {
3
+ client: ContainerClient;
4
+ collectionPrefix?: string;
5
+ docPrefix: string;
6
+ filename: string;
7
+ useCompositePrefixes?: boolean;
8
+ }
9
+ export declare function deleteFile({ client, collectionPrefix, docPrefix, filename, useCompositePrefixes, }: DeleteArgs): Promise<void>;
10
+ export {};
11
+ //# sourceMappingURL=deleteFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteFile.d.ts","sourceRoot":"","sources":["../src/deleteFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAI1D,UAAU,UAAU;IAClB,MAAM,EAAE,eAAe,CAAA;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IAChB,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,wBAAsB,UAAU,CAAC,EAC/B,MAAM,EACN,gBAAqB,EACrB,SAAS,EACT,QAAQ,EACR,oBAA4B,GAC7B,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAW5B"}
@@ -0,0 +1,13 @@
1
+ import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
2
+ export async function deleteFile({ client, collectionPrefix = '', docPrefix, filename, useCompositePrefixes = false }) {
3
+ const fileKey = getFileKey({
4
+ collectionPrefix,
5
+ docPrefix,
6
+ filename,
7
+ useCompositePrefixes
8
+ });
9
+ const blockBlobClient = client.getBlockBlobClient(fileKey);
10
+ await blockBlobClient.deleteIfExists();
11
+ }
12
+
13
+ //# sourceMappingURL=deleteFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/deleteFile.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\n\nimport { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\ninterface DeleteArgs {\n client: ContainerClient\n collectionPrefix?: string\n docPrefix: string\n filename: string\n useCompositePrefixes?: boolean\n}\n\nexport async function deleteFile({\n client,\n collectionPrefix = '',\n docPrefix,\n filename,\n useCompositePrefixes = false,\n}: DeleteArgs): Promise<void> {\n const fileKey = getFileKey({\n collectionPrefix,\n docPrefix,\n filename,\n useCompositePrefixes,\n })\n\n const blockBlobClient = client.getBlockBlobClient(fileKey)\n\n await blockBlobClient.deleteIfExists()\n}\n"],"names":["getFileKey","deleteFile","client","collectionPrefix","docPrefix","filename","useCompositePrefixes","fileKey","blockBlobClient","getBlockBlobClient","deleteIfExists"],"mappings":"AAEA,SAASA,UAAU,QAAQ,6CAA4C;AAUvE,OAAO,eAAeC,WAAW,EAC/BC,MAAM,EACNC,mBAAmB,EAAE,EACrBC,SAAS,EACTC,QAAQ,EACRC,uBAAuB,KAAK,EACjB;IACX,MAAMC,UAAUP,WAAW;QACzBG;QACAC;QACAC;QACAC;IACF;IAEA,MAAME,kBAAkBN,OAAOO,kBAAkB,CAACF;IAElD,MAAMC,gBAAgBE,cAAc;AACtC"}
@@ -1,8 +1,11 @@
1
- import type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types';
2
- interface Args {
1
+ interface GenerateURLArgs {
3
2
  baseURL: string;
3
+ collectionPrefix?: string;
4
4
  containerName: string;
5
+ filename: string;
6
+ prefix: string;
7
+ useCompositePrefixes?: boolean;
5
8
  }
6
- export declare const getGenerateURL: ({ baseURL, containerName }: Args) => GenerateURL;
9
+ export declare function generateURL({ baseURL, collectionPrefix, containerName, filename, prefix, useCompositePrefixes, }: GenerateURLArgs): string;
7
10
  export {};
8
11
  //# sourceMappingURL=generateURL.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"generateURL.d.ts","sourceRoot":"","sources":["../src/generateURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wCAAwC,CAAA;AAIzE,UAAU,IAAI;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,aAAa,EAAE,MAAM,CAAA;CACtB;AAED,eAAO,MAAM,cAAc,+BACI,IAAI,KAAG,WAGnC,CAAA"}
1
+ {"version":3,"file":"generateURL.d.ts","sourceRoot":"","sources":["../src/generateURL.ts"],"names":[],"mappings":"AAEA,UAAU,eAAe;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,wBAAgB,WAAW,CAAC,EAC1B,OAAO,EACP,gBAAqB,EACrB,aAAa,EACb,QAAQ,EACR,MAAM,EACN,oBAA4B,GAC7B,EAAE,eAAe,GAAG,MAAM,CAS1B"}
@@ -1,6 +1,12 @@
1
- import path from 'path';
2
- export const getGenerateURL = ({ baseURL, containerName })=>({ filename, prefix = '' })=>{
3
- return `${baseURL}/${containerName}/${path.posix.join(prefix, filename)}`;
4
- };
1
+ import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
2
+ export function generateURL({ baseURL, collectionPrefix = '', containerName, filename, prefix, useCompositePrefixes = false }) {
3
+ const fileKey = getFileKey({
4
+ collectionPrefix,
5
+ docPrefix: prefix,
6
+ filename,
7
+ useCompositePrefixes
8
+ });
9
+ return `${baseURL}/${containerName}/${fileKey}`;
10
+ }
5
11
 
6
12
  //# sourceMappingURL=generateURL.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/generateURL.ts"],"sourcesContent":["import type { GenerateURL } from '@payloadcms/plugin-cloud-storage/types'\n\nimport path from 'path'\n\ninterface Args {\n baseURL: string\n containerName: string\n}\n\nexport const getGenerateURL =\n ({ baseURL, containerName }: Args): GenerateURL =>\n ({ filename, prefix = '' }) => {\n return `${baseURL}/${containerName}/${path.posix.join(prefix, filename)}`\n }\n"],"names":["path","getGenerateURL","baseURL","containerName","filename","prefix","posix","join"],"mappings":"AAEA,OAAOA,UAAU,OAAM;AAOvB,OAAO,MAAMC,iBACX,CAAC,EAAEC,OAAO,EAAEC,aAAa,EAAQ,GACjC,CAAC,EAAEC,QAAQ,EAAEC,SAAS,EAAE,EAAE;QACxB,OAAO,GAAGH,QAAQ,CAAC,EAAEC,cAAc,CAAC,EAAEH,KAAKM,KAAK,CAACC,IAAI,CAACF,QAAQD,WAAW;IAC3E,EAAC"}
1
+ {"version":3,"sources":["../src/generateURL.ts"],"sourcesContent":["import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\n\ninterface GenerateURLArgs {\n baseURL: string\n collectionPrefix?: string\n containerName: string\n filename: string\n prefix: string\n useCompositePrefixes?: boolean\n}\n\nexport function generateURL({\n baseURL,\n collectionPrefix = '',\n containerName,\n filename,\n prefix,\n useCompositePrefixes = false,\n}: GenerateURLArgs): string {\n const fileKey = getFileKey({\n collectionPrefix,\n docPrefix: prefix,\n filename,\n useCompositePrefixes,\n })\n\n return `${baseURL}/${containerName}/${fileKey}`\n}\n"],"names":["getFileKey","generateURL","baseURL","collectionPrefix","containerName","filename","prefix","useCompositePrefixes","fileKey","docPrefix"],"mappings":"AAAA,SAASA,UAAU,QAAQ,6CAA4C;AAWvE,OAAO,SAASC,YAAY,EAC1BC,OAAO,EACPC,mBAAmB,EAAE,EACrBC,aAAa,EACbC,QAAQ,EACRC,MAAM,EACNC,uBAAuB,KAAK,EACZ;IAChB,MAAMC,UAAUR,WAAW;QACzBG;QACAM,WAAWH;QACXD;QACAE;IACF;IAEA,OAAO,GAAGL,QAAQ,CAAC,EAAEE,cAAc,CAAC,EAAEI,SAAS;AACjD"}
@@ -0,0 +1,16 @@
1
+ import type { ContainerClient } from '@azure/storage-blob';
2
+ import type { CollectionConfig, PayloadRequest } from 'payload';
3
+ interface GetFileArgs {
4
+ client: ContainerClient;
5
+ clientUploadContext?: unknown;
6
+ collection: CollectionConfig;
7
+ collectionPrefix?: string;
8
+ filename: string;
9
+ incomingHeaders?: Headers;
10
+ prefixQueryParam?: string;
11
+ req: PayloadRequest;
12
+ useCompositePrefixes?: boolean;
13
+ }
14
+ export declare function getFile({ client, clientUploadContext, collection, collectionPrefix, filename, incomingHeaders, prefixQueryParam, req, useCompositePrefixes, }: GetFileArgs): Promise<Response>;
15
+ export {};
16
+ //# sourceMappingURL=getFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFile.d.ts","sourceRoot":"","sources":["../src/getFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA8B,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACtF,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAW/D,UAAU,WAAW;IACnB,MAAM,EAAE,eAAe,CAAA;IACvB,mBAAmB,CAAC,EAAE,OAAO,CAAA;IAC7B,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,GAAG,EAAE,cAAc,CAAA;IACnB,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAgCD,wBAAsB,OAAO,CAAC,EAC5B,MAAM,EACN,mBAAmB,EACnB,UAAU,EACV,gBAAqB,EACrB,QAAQ,EACR,eAAe,EACf,gBAAgB,EAChB,GAAG,EACH,oBAA4B,GAC7B,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAuHjC"}
@@ -0,0 +1,138 @@
1
+ import { RestError } from '@azure/storage-blob';
2
+ import { getFilePrefix as getDocPrefix, getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
3
+ import { getRangeRequestInfo } from 'payload/internal';
4
+ import { sanitizeFilename } from 'payload/shared';
5
+ const isNodeReadableStream = (body)=>{
6
+ return typeof body === 'object' && body !== null && 'pipe' in body && typeof body.pipe === 'function' && 'destroy' in body && typeof body.destroy === 'function';
7
+ };
8
+ const abortRequestAndDestroyStream = ({ abortController, blob })=>{
9
+ try {
10
+ abortController.abort();
11
+ } catch {
12
+ /* noop */ }
13
+ if (blob?.readableStreamBody && isNodeReadableStream(blob.readableStreamBody)) {
14
+ blob.readableStreamBody.destroy();
15
+ }
16
+ };
17
+ export async function getFile({ client, clientUploadContext, collection, collectionPrefix = '', filename, incomingHeaders, prefixQueryParam, req, useCompositePrefixes = false }) {
18
+ let blob = undefined;
19
+ let streamed = false;
20
+ const abortController = new AbortController();
21
+ if (req.signal) {
22
+ req.signal.addEventListener('abort', ()=>{
23
+ abortRequestAndDestroyStream({
24
+ abortController,
25
+ blob
26
+ });
27
+ });
28
+ }
29
+ try {
30
+ const docPrefix = await getDocPrefix({
31
+ clientUploadContext,
32
+ collection,
33
+ filename,
34
+ prefixQueryParam,
35
+ req
36
+ });
37
+ const key = getFileKey({
38
+ collectionPrefix,
39
+ docPrefix,
40
+ filename: sanitizeFilename(filename),
41
+ useCompositePrefixes
42
+ });
43
+ const blockBlobClient = client.getBlockBlobClient(key);
44
+ // Get file size for range validation
45
+ const properties = await blockBlobClient.getProperties();
46
+ const fileSize = properties.contentLength;
47
+ if (!fileSize) {
48
+ return new Response('Internal Server Error', {
49
+ status: 500
50
+ });
51
+ }
52
+ // Handle range request
53
+ const rangeHeader = req.headers.get('range');
54
+ const rangeResult = getRangeRequestInfo({
55
+ fileSize,
56
+ rangeHeader
57
+ });
58
+ if (rangeResult.type === 'invalid') {
59
+ return new Response(null, {
60
+ headers: new Headers(rangeResult.headers),
61
+ status: rangeResult.status
62
+ });
63
+ }
64
+ // Download with range if partial
65
+ blob = rangeResult.type === 'partial' ? await blockBlobClient.download(rangeResult.rangeStart, rangeResult.rangeEnd - rangeResult.rangeStart + 1, {
66
+ abortSignal: abortController.signal
67
+ }) : await blockBlobClient.download(0, undefined, {
68
+ abortSignal: abortController.signal
69
+ });
70
+ let headers = new Headers(incomingHeaders);
71
+ // Add range-related headers from the result
72
+ for (const [key, value] of Object.entries(rangeResult.headers)){
73
+ headers.append(key, value);
74
+ }
75
+ // Add Azure-specific headers
76
+ headers.append('Content-Type', String(properties.contentType));
77
+ if (properties.etag) {
78
+ headers.append('ETag', String(properties.etag));
79
+ }
80
+ // Add Content-Security-Policy header for SVG files to prevent executable code
81
+ if (properties.contentType === 'image/svg+xml') {
82
+ headers.append('Content-Security-Policy', "script-src 'none'");
83
+ }
84
+ const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match');
85
+ if (collection.upload && typeof collection.upload === 'object' && typeof collection.upload.modifyResponseHeaders === 'function') {
86
+ headers = collection.upload.modifyResponseHeaders({
87
+ headers
88
+ }) || headers;
89
+ }
90
+ if (etagFromHeaders && etagFromHeaders === properties.etag) {
91
+ return new Response(null, {
92
+ headers,
93
+ status: 304
94
+ });
95
+ }
96
+ if (!blob.readableStreamBody || !isNodeReadableStream(blob.readableStreamBody)) {
97
+ return new Response('Internal Server Error', {
98
+ status: 500
99
+ });
100
+ }
101
+ const stream = blob.readableStreamBody;
102
+ stream.on('error', (err)=>{
103
+ req.payload.logger.error({
104
+ err,
105
+ msg: 'Error while streaming Azure blob (aborting)'
106
+ });
107
+ abortRequestAndDestroyStream({
108
+ abortController,
109
+ blob
110
+ });
111
+ });
112
+ streamed = true;
113
+ return new Response(stream, {
114
+ headers,
115
+ status: rangeResult.status
116
+ });
117
+ } catch (err) {
118
+ if (err instanceof RestError && err.statusCode === 404) {
119
+ return new Response(null, {
120
+ status: 404,
121
+ statusText: 'Not Found'
122
+ });
123
+ }
124
+ req.payload.logger.error(err);
125
+ return new Response('Internal Server Error', {
126
+ status: 500
127
+ });
128
+ } finally{
129
+ if (!streamed) {
130
+ abortRequestAndDestroyStream({
131
+ abortController,
132
+ blob
133
+ });
134
+ }
135
+ }
136
+ }
137
+
138
+ //# sourceMappingURL=getFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/getFile.ts"],"sourcesContent":["import type { BlobDownloadResponseParsed, ContainerClient } from '@azure/storage-blob'\nimport type { CollectionConfig, PayloadRequest } from 'payload'\nimport type { Readable } from 'stream'\n\nimport { RestError } from '@azure/storage-blob'\nimport {\n getFilePrefix as getDocPrefix,\n getFileKey,\n} from '@payloadcms/plugin-cloud-storage/utilities'\nimport { getRangeRequestInfo } from 'payload/internal'\nimport { sanitizeFilename } from 'payload/shared'\n\ninterface GetFileArgs {\n client: ContainerClient\n clientUploadContext?: unknown\n collection: CollectionConfig\n collectionPrefix?: string\n filename: string\n incomingHeaders?: Headers\n prefixQueryParam?: string\n req: PayloadRequest\n useCompositePrefixes?: boolean\n}\n\nconst isNodeReadableStream = (\n body: BlobDownloadResponseParsed['readableStreamBody'],\n): body is Readable => {\n return (\n typeof body === 'object' &&\n body !== null &&\n 'pipe' in body &&\n typeof body.pipe === 'function' &&\n 'destroy' in body &&\n typeof body.destroy === 'function'\n )\n}\n\nconst abortRequestAndDestroyStream = ({\n abortController,\n blob,\n}: {\n abortController: AbortController\n blob?: BlobDownloadResponseParsed\n}) => {\n try {\n abortController.abort()\n } catch {\n /* noop */\n }\n if (blob?.readableStreamBody && isNodeReadableStream(blob.readableStreamBody)) {\n blob.readableStreamBody.destroy()\n }\n}\n\nexport async function getFile({\n client,\n clientUploadContext,\n collection,\n collectionPrefix = '',\n filename,\n incomingHeaders,\n prefixQueryParam,\n req,\n useCompositePrefixes = false,\n}: GetFileArgs): Promise<Response> {\n let blob: BlobDownloadResponseParsed | undefined = undefined\n let streamed = false\n\n const abortController = new AbortController()\n if (req.signal) {\n req.signal.addEventListener('abort', () => {\n abortRequestAndDestroyStream({ abortController, blob })\n })\n }\n\n try {\n const docPrefix = await getDocPrefix({\n clientUploadContext,\n collection,\n filename,\n prefixQueryParam,\n req,\n })\n\n const key = getFileKey({\n collectionPrefix,\n docPrefix,\n filename: sanitizeFilename(filename),\n useCompositePrefixes,\n })\n\n const blockBlobClient = client.getBlockBlobClient(key)\n\n // Get file size for range validation\n const properties = await blockBlobClient.getProperties()\n const fileSize = properties.contentLength\n\n if (!fileSize) {\n return new Response('Internal Server Error', { status: 500 })\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 // Download with range if partial\n blob =\n rangeResult.type === 'partial'\n ? await blockBlobClient.download(\n rangeResult.rangeStart,\n rangeResult.rangeEnd - rangeResult.rangeStart + 1,\n { abortSignal: abortController.signal },\n )\n : await blockBlobClient.download(0, undefined, { abortSignal: abortController.signal })\n\n let headers = new Headers(incomingHeaders)\n\n // Add range-related headers from the result\n for (const [key, value] of Object.entries(rangeResult.headers)) {\n headers.append(key, value)\n }\n\n // Add Azure-specific headers\n headers.append('Content-Type', String(properties.contentType))\n if (properties.etag) {\n headers.append('ETag', String(properties.etag))\n }\n\n // Add Content-Security-Policy header for SVG files to prevent executable code\n if (properties.contentType === 'image/svg+xml') {\n headers.append('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 === properties.etag) {\n return new Response(null, {\n headers,\n status: 304,\n })\n }\n\n if (!blob.readableStreamBody || !isNodeReadableStream(blob.readableStreamBody)) {\n return new Response('Internal Server Error', { status: 500 })\n }\n\n const stream = blob.readableStreamBody\n stream.on('error', (err: Error) => {\n req.payload.logger.error({\n err,\n msg: 'Error while streaming Azure blob (aborting)',\n })\n abortRequestAndDestroyStream({ abortController, blob })\n })\n\n streamed = true\n return new Response(stream, { headers, status: rangeResult.status })\n } catch (err: unknown) {\n if (err instanceof RestError && err.statusCode === 404) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n req.payload.logger.error(err)\n return new Response('Internal Server Error', { status: 500 })\n } finally {\n if (!streamed) {\n abortRequestAndDestroyStream({ abortController, blob })\n }\n }\n}\n"],"names":["RestError","getFilePrefix","getDocPrefix","getFileKey","getRangeRequestInfo","sanitizeFilename","isNodeReadableStream","body","pipe","destroy","abortRequestAndDestroyStream","abortController","blob","abort","readableStreamBody","getFile","client","clientUploadContext","collection","collectionPrefix","filename","incomingHeaders","prefixQueryParam","req","useCompositePrefixes","undefined","streamed","AbortController","signal","addEventListener","docPrefix","key","blockBlobClient","getBlockBlobClient","properties","getProperties","fileSize","contentLength","Response","status","rangeHeader","headers","get","rangeResult","type","Headers","download","rangeStart","rangeEnd","abortSignal","value","Object","entries","append","String","contentType","etag","etagFromHeaders","upload","modifyResponseHeaders","stream","on","err","payload","logger","error","msg","statusCode","statusText"],"mappings":"AAIA,SAASA,SAAS,QAAQ,sBAAqB;AAC/C,SACEC,iBAAiBC,YAAY,EAC7BC,UAAU,QACL,6CAA4C;AACnD,SAASC,mBAAmB,QAAQ,mBAAkB;AACtD,SAASC,gBAAgB,QAAQ,iBAAgB;AAcjD,MAAMC,uBAAuB,CAC3BC;IAEA,OACE,OAA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IAAI,EAAE;YAC1D,OAAO,IAAIlB,SAAS,MAAM;gBACxBG;gBACAF,QAAQ;YACV;QACF;QAEA,IAAI,CAAC3B,KAAKE,kBAAkB,IAAI,CAACR,qBAAqBM,KAAKE,kBAAkB,GAAG;YAC9E,OAAO,IAAIwB,SAAS,yBAAyB;gBAAEC,QAAQ;YAAI;QAC7D;QAEA,MAAMqB,SAAShD,KAAKE,kBAAkB;QACtC8C,OAAOC,EAAE,CAAC,SAAS,CAACC;YAClBvC,IAAIwC,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBACvBH;gBACAI,KAAK;YACP;YACAxD,6BAA6B;gBAAEC;gBAAiBC;YAAK;QACvD;QAEAc,WAAW;QACX,OAAO,IAAIY,SAASsB,QAAQ;YAAEnB;YAASF,QAAQI,YAAYJ,MAAM;QAAC;IACpE,EAAE,OAAOuB,KAAc;QACrB,IAAIA,eAAe9D,aAAa8D,IAAIK,UAAU,KAAK,KAAK;YACtD,OAAO,IAAI7B,SAAS,MAAM;gBAAEC,QAAQ;gBAAK6B,YAAY;YAAY;QACnE;QACA7C,IAAIwC,OAAO,CAACC,MAAM,CAACC,KAAK,CAACH;QACzB,OAAO,IAAIxB,SAAS,yBAAyB;YAAEC,QAAQ;QAAI;IAC7D,SAAU;QACR,IAAI,CAACb,UAAU;YACbhB,6BAA6B;gBAAEC;gBAAiBC;YAAK;QACvD;IACF;AACF"}
package/dist/index.d.ts CHANGED
@@ -51,6 +51,20 @@ export type AzureStorageOptions = {
51
51
  * Default: true
52
52
  */
53
53
  enabled?: boolean;
54
+ /**
55
+ * When true, the collection-level prefix and document-level prefix are combined
56
+ * (compositional). When false (default), document prefix overrides collection
57
+ * prefix entirely.
58
+ *
59
+ * Example:
60
+ * - collection prefix: `collection-prefix/`
61
+ * - document prefix: `document-prefix/`
62
+ * - resulting prefix with useCompositePrefixes=true: `collection-prefix/document-prefix/`
63
+ * - resulting prefix with useCompositePrefixes=false: `document-prefix/`
64
+ *
65
+ * @default false
66
+ */
67
+ useCompositePrefixes?: boolean;
54
68
  };
55
69
  type AzureStoragePlugin = (azureStorageArgs: AzureStorageOptions) => Plugin;
56
70
  export declare const azureStorage: AzureStoragePlugin;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,mBAAmB,EAEnB,iBAAiB,EAElB,MAAM,wCAAwC,CAAA;AAC/C,OAAO,KAAK,EAAU,MAAM,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAUnE,OAAO,EAAE,gBAAgB,IAAI,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAEtF,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,oBAAoB,EAAE,OAAO,CAAA;IAE7B;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAE5B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;OAEG;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;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,kBAAkB,GAAG,CAAC,gBAAgB,EAAE,mBAAmB,KAAK,MAAM,CAAA;AAE3E,eAAO,MAAM,YAAY,EAAE,kBAsExB,CAAA;AAmCH,OAAO,EAAE,oBAAoB,IAAI,gBAAgB,EAAE,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,MAAM,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAOnE,OAAO,EAAE,gBAAgB,IAAI,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAEtF,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,oBAAoB,EAAE,OAAO,CAAA;IAE7B;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAE5B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;OAEG;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;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;;;;;;;;OAYG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B,CAAA;AAED,KAAK,kBAAkB,GAAG,CAAC,gBAAgB,EAAE,mBAAmB,KAAK,MAAM,CAAA;AAE3E,eAAO,MAAM,YAAY,EAAE,kBAwFxB,CAAA;AAEH,OAAO,EAAE,oBAAoB,IAAI,gBAAgB,EAAE,CAAA"}
package/dist/index.js CHANGED
@@ -1,10 +1,7 @@
1
1
  import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage';
2
2
  import { initClientUploads } from '@payloadcms/plugin-cloud-storage/utilities';
3
+ import { createAzureAdapter } from './adapter.js';
3
4
  import { getGenerateSignedURLHandler } from './generateSignedURL.js';
4
- import { getGenerateURL } from './generateURL.js';
5
- import { getHandleDelete } from './handleDelete.js';
6
- import { getHandleUpload } from './handleUpload.js';
7
- import { getHandler } from './staticHandler.js';
8
5
  import { getStorageClient as getStorageClientFunc } from './utils/getStorageClient.js';
9
6
  export const azureStorage = (azureStorageOptions)=>(incomingConfig)=>{
10
7
  const getStorageClient = ()=>getStorageClientFunc({
@@ -28,7 +25,23 @@ export const azureStorage = (azureStorageOptions)=>(incomingConfig)=>{
28
25
  if (isPluginDisabled) {
29
26
  return incomingConfig;
30
27
  }
31
- const adapter = azureStorageInternal(getStorageClient, azureStorageOptions);
28
+ const createContainerIfNotExists = ()=>{
29
+ void getStorageClientFunc({
30
+ connectionString: azureStorageOptions.connectionString,
31
+ containerName: azureStorageOptions.containerName
32
+ }).createIfNotExists({
33
+ access: 'blob'
34
+ });
35
+ };
36
+ const adapter = createAzureAdapter({
37
+ allowContainerCreate: azureStorageOptions.allowContainerCreate,
38
+ baseURL: azureStorageOptions.baseURL,
39
+ clientUploads: azureStorageOptions.clientUploads,
40
+ containerName: azureStorageOptions.containerName,
41
+ createContainerIfNotExists,
42
+ getStorageClient,
43
+ useCompositePrefixes: azureStorageOptions.useCompositePrefixes
44
+ });
32
45
  // Add adapter to each collection option object
33
46
  const collectionsWithAdapter = Object.entries(azureStorageOptions.collections).reduce((acc, [slug, collOptions])=>({
34
47
  ...acc,
@@ -55,45 +68,10 @@ export const azureStorage = (azureStorageOptions)=>(incomingConfig)=>{
55
68
  };
56
69
  return cloudStoragePlugin({
57
70
  alwaysInsertFields: azureStorageOptions.alwaysInsertFields,
58
- collections: collectionsWithAdapter
71
+ collections: collectionsWithAdapter,
72
+ useCompositePrefixes: azureStorageOptions.useCompositePrefixes
59
73
  })(config);
60
74
  };
61
- function azureStorageInternal(getStorageClient, { allowContainerCreate, baseURL, clientUploads, connectionString, containerName }) {
62
- const createContainerIfNotExists = ()=>{
63
- void getStorageClientFunc({
64
- connectionString,
65
- containerName
66
- }).createIfNotExists({
67
- access: 'blob'
68
- });
69
- };
70
- return ({ collection, prefix })=>{
71
- return {
72
- name: 'azure',
73
- clientUploads,
74
- generateURL: getGenerateURL({
75
- baseURL,
76
- containerName
77
- }),
78
- handleDelete: getHandleDelete({
79
- collection,
80
- getStorageClient
81
- }),
82
- handleUpload: getHandleUpload({
83
- collection,
84
- getStorageClient,
85
- prefix
86
- }),
87
- staticHandler: getHandler({
88
- collection,
89
- getStorageClient
90
- }),
91
- ...allowContainerCreate && {
92
- onInit: createContainerIfNotExists
93
- }
94
- };
95
- };
96
- }
97
75
  export { getStorageClientFunc as getStorageClient };
98
76
 
99
77
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\nimport type {\n Adapter,\n ClientUploadsConfig,\n PluginOptions as CloudStoragePluginOptions,\n CollectionOptions,\n GeneratedAdapter,\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 { getGenerateSignedURLHandler } from './generateSignedURL.js'\nimport { getGenerateURL } from './generateURL.js'\nimport { getHandleDelete } from './handleDelete.js'\nimport { getHandleUpload } from './handleUpload.js'\nimport { getHandler } from './staticHandler.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 * When enabled, fields (like the prefix field) will always be inserted into\n * the collection schema regardless of whether the plugin is enabled. This\n * ensures a consistent schema across all environments.\n *\n * This will be enabled by default in Payload v4.\n *\n * @default false\n */\n alwaysInsertFields?: boolean\n\n /**\n * 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. You must allow CORS PUT method to your website.\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 * 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\ntype AzureStoragePlugin = (azureStorageArgs: AzureStorageOptions) => Plugin\n\nexport const azureStorage: AzureStoragePlugin =\n (azureStorageOptions: AzureStorageOptions) =>\n (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 initClientUploads({\n clientHandler: '@payloadcms/storage-azure/client#AzureClientUploadHandler',\n collections: azureStorageOptions.collections,\n config: incomingConfig,\n enabled: !isPluginDisabled && Boolean(azureStorageOptions.clientUploads),\n serverHandler: getGenerateSignedURLHandler({\n access:\n typeof azureStorageOptions.clientUploads === 'object'\n ? azureStorageOptions.clientUploads.access\n : undefined,\n collections: azureStorageOptions.collections,\n containerName: azureStorageOptions.containerName,\n getStorageClient,\n }),\n serverHandlerPath: '/storage-azure-generate-signed-url',\n })\n\n if (isPluginDisabled) {\n return incomingConfig\n }\n\n const adapter = azureStorageInternal(getStorageClient, azureStorageOptions)\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 alwaysInsertFields: azureStorageOptions.alwaysInsertFields,\n collections: collectionsWithAdapter,\n })(config)\n }\n\nfunction azureStorageInternal(\n getStorageClient: () => ContainerClient,\n {\n allowContainerCreate,\n baseURL,\n clientUploads,\n connectionString,\n containerName,\n }: AzureStorageOptions,\n): Adapter {\n const createContainerIfNotExists = () => {\n void getStorageClientFunc({ connectionString, containerName }).createIfNotExists({\n access: 'blob',\n })\n }\n\n return ({ collection, prefix }): GeneratedAdapter => {\n return {\n name: 'azure',\n clientUploads,\n generateURL: getGenerateURL({ baseURL, containerName }),\n handleDelete: getHandleDelete({ collection, getStorageClient }),\n handleUpload: getHandleUpload({\n collection,\n getStorageClient,\n prefix,\n }),\n staticHandler: getHandler({ collection, getStorageClient }),\n ...(allowContainerCreate && { onInit: createContainerIfNotExists }),\n }\n }\n}\n\nexport { getStorageClientFunc as getStorageClient }\n"],"names":["cloudStoragePlugin","initClientUploads","getGenerateSignedURLHandler","getGenerateURL","getHandleDelete","getHandleUpload","getHandler","getStorageClient","getStorageClientFunc","azureStorage","azureStorageOptions","incomingConfig","connectionString","containerName","isPluginDisabled","enabled","clientHandler","collections","config","Boolean","clientUploads","serverHandler","access","undefined","serverHandlerPath","adapter","azureStorageInternal","collectionsWithAdapter","Object","entries","reduce","acc","slug","collOptions","map","collection","upload","disableLocalStorage","alwaysInsertFields","allowContainerCreate","baseURL","createContainerIfNotExists","createIfNotExists","prefix","name","generateURL","handleDelete","handleUpload","staticHandler","onInit"],"mappings":"AAUA,SAASA,kBAAkB,QAAQ,mCAAkC;AACrE,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SAASC,2BAA2B,QAAQ,yBAA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sB,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,OAAOrC,mBAAmB;YACxBsC,oBAAoB5B,oBAAoB4B,kBAAkB;YAC1DrB,aAAaU;QACf,GAAGT;IACL,EAAC;AAEH,SAASQ,qBACPnB,gBAAuC,EACvC,EACEgC,oBAAoB,EACpBC,OAAO,EACPpB,aAAa,EACbR,gBAAgB,EAChBC,aAAa,EACO;IAEtB,MAAM4B,6BAA6B;QACjC,KAAKjC,qBAAqB;YAAEI;YAAkBC;QAAc,GAAG6B,iBAAiB,CAAC;YAC/EpB,QAAQ;QACV;IACF;IAEA,OAAO,CAAC,EAAEa,UAAU,EAAEQ,MAAM,EAAE;QAC5B,OAAO;YACLC,MAAM;YACNxB;YACAyB,aAAa1C,eAAe;gBAAEqC;gBAAS3B;YAAc;YACrDiC,cAAc1C,gBAAgB;gBAAE+B;gBAAY5B;YAAiB;YAC7DwC,cAAc1C,gBAAgB;gBAC5B8B;gBACA5B;gBACAoC;YACF;YACAK,eAAe1C,WAAW;gBAAE6B;gBAAY5B;YAAiB;YACzD,GAAIgC,wBAAwB;gBAAEU,QAAQR;YAA2B,CAAC;QACpE;IACF;AACF;AAEA,SAASjC,wBAAwBD,gBAAgB,GAAE"}
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 { createAzureAdapter } from './adapter.js'\nimport { getGenerateSignedURLHandler } from './generateSignedURL.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 * When enabled, fields (like the prefix field) will always be inserted into\n * the collection schema regardless of whether the plugin is enabled. This\n * ensures a consistent schema across all environments.\n *\n * This will be enabled by default in Payload v4.\n *\n * @default false\n */\n alwaysInsertFields?: boolean\n\n /**\n * 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. You must allow CORS PUT method to your website.\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 * 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), 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 AzureStoragePlugin = (azureStorageArgs: AzureStorageOptions) => Plugin\n\nexport const azureStorage: AzureStoragePlugin =\n (azureStorageOptions: AzureStorageOptions) =>\n (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 initClientUploads({\n clientHandler: '@payloadcms/storage-azure/client#AzureClientUploadHandler',\n collections: azureStorageOptions.collections,\n config: incomingConfig,\n enabled: !isPluginDisabled && Boolean(azureStorageOptions.clientUploads),\n serverHandler: getGenerateSignedURLHandler({\n access:\n typeof azureStorageOptions.clientUploads === 'object'\n ? azureStorageOptions.clientUploads.access\n : undefined,\n collections: azureStorageOptions.collections,\n containerName: azureStorageOptions.containerName,\n getStorageClient,\n }),\n serverHandlerPath: '/storage-azure-generate-signed-url',\n })\n\n if (isPluginDisabled) {\n return incomingConfig\n }\n\n const createContainerIfNotExists = () => {\n void getStorageClientFunc({\n connectionString: azureStorageOptions.connectionString,\n containerName: azureStorageOptions.containerName,\n }).createIfNotExists({\n access: 'blob',\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 alwaysInsertFields: azureStorageOptions.alwaysInsertFields,\n collections: collectionsWithAdapter,\n useCompositePrefixes: azureStorageOptions.useCompositePrefixes,\n })(config)\n }\n\nexport { getStorageClientFunc as getStorageClient }\n"],"names":["cloudStoragePlugin","initClientUploads","createAzureAdapter","getGenerateSignedURLHandler","getStorageClient","getStorageClientFunc","azureStorage","azureStorageOptions","incomingConfig","connectionString","containerName","isPluginDisabled","enabled","clientHandler","collections","config","Boolean","clientUploads","serverHandler","access","undefined","serverHandlerPath","createContainerIfNotExists","createIfNotExists","adapter","allowContainerCreate","baseURL","useCompositePrefixes","collectionsWithAdapter","Object","entries","reduce","acc","slug","collOptions","map","collection","upload","disableLocalStorage","alwaysInsertFields"],"mappings":"AAOA,SAASA,kBAAkB,QAAQ,mCAAkC;AACrE,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SAASC,kBAAkB,QAAQ,eAAc;AACjD,SAASC,2BAA2B,QAAQ,yBAAwB;AACpE,SAASC,oBAAoBC,oBAAoB,QAAQ,8BAA6B;AA8EtF,OAAO,MAAMC,eACX,CAACC,sBACD,CAACC;QACC,MAAMJ,mBAAmB,IACvBC,qBAAqB;gBACnBI,kBAAkBF,oBAAoBE,gBAAgB;gBACtDC,eAAeH,oBAAoBG,aAAa;YAClD;QAEF,MAAMC,mBAAmBJ,oBAAoBK,OAAO,KAAK;QAEzDX,kBAAkB;YAChBY,eAAe;YACfC,aAAaP,oBAAoBO,WAAW;YAC5CC,QAAQP;YACRI,SAAS,CAACD,oBAAoBK,QAAQT,oBAAoBU,aAAa;YACvEC,eAAef,4BAA4B;gBACzCgB,QACE,OAAOZ,oBAAoBU,aAAa,KAAK,WACzCV,oBAAoBU,aAAa,CAACE,MAAM,GACxCC;gBACNN,aAAaP,oBAAoBO,WAAW;gBAC5CJ,eAAeH,oBAAoBG,aAAa;gBAChDN;YACF;YACAiB,mBAAmB;QACrB;QAEA,IAAIV,kBAAkB;YACpB,OAAOH;QACT;QAEA,MAAMc,6BAA6B;YACjC,KAAKjB,qBAAqB;gBACxBI,kBAAkBF,oBAAoBE,gBAAgB;gBACtDC,eAAeH,oBAAoBG,aAAa;YAClD,GAAGa,iBAAiB,CAAC;gBACnBJ,QAAQ;YACV;QACF;QAEA,MAAMK,UAAUtB,mBAAmB;YACjCuB,sBAAsBlB,oBAAoBkB,oBAAoB;YAC9DC,SAASnB,oBAAoBmB,OAAO;YACpCT,eAAeV,oBAAoBU,aAAa;YAChDP,eAAeH,oBAAoBG,aAAa;YAChDY;YACAlB;YACAuB,sBAAsBpB,oBAAoBoB,oBAAoB;QAChE;QAEA,+CAA+C;QAC/C,MAAMC,yBAAmEC,OAAOC,OAAO,CACrFvB,oBAAoBO,WAAW,EAC/BiB,MAAM,CACN,CAACC,KAAK,CAACC,MAAMC,YAAY,GAAM,CAAA;gBAC7B,GAAGF,GAAG;gBACN,CAACC,KAAK,EAAE;oBACN,GAAIC,gBAAgB,OAAO,CAAC,IAAIA,WAAW;oBAC3CV;gBACF;YACF,CAAA,GACA,CAAC;QAGH,gFAAgF;QAChF,MAAMT,SAAS;YACb,GAAGP,cAAc;YACjBM,aAAa,AAACN,CAAAA,eAAeM,WAAW,IAAI,EAAE,AAAD,EAAGqB,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,OAAOtC,mBAAmB;YACxBuC,oBAAoBhC,oBAAoBgC,kBAAkB;YAC1DzB,aAAac;YACbD,sBAAsBpB,oBAAoBoB,oBAAoB;QAChE,GAAGZ;IACL,EAAC;AAEH,SAASV,wBAAwBD,gBAAgB,GAAE"}
@@ -0,0 +1,14 @@
1
+ import type { ContainerClient } from '@azure/storage-blob';
2
+ interface UploadArgs {
3
+ buffer: Buffer;
4
+ client: ContainerClient;
5
+ collectionPrefix?: string;
6
+ docPrefix?: string;
7
+ filename: string;
8
+ mimeType: string;
9
+ tempFilePath?: string;
10
+ useCompositePrefixes?: boolean;
11
+ }
12
+ export declare function uploadFile({ buffer, client, collectionPrefix, docPrefix, filename, mimeType, tempFilePath, useCompositePrefixes, }: UploadArgs): Promise<void>;
13
+ export {};
14
+ //# sourceMappingURL=uploadFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../src/uploadFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAO1D,UAAU,UAAU;IAClB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,eAAe,CAAA;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAID,wBAAsB,UAAU,CAAC,EAC/B,MAAM,EACN,MAAM,EACN,gBAAqB,EACrB,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,oBAA4B,GAC7B,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CA0B5B"}
@@ -0,0 +1,33 @@
1
+ import { AbortController } from '@azure/abort-controller';
2
+ import { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities';
3
+ import fs from 'fs';
4
+ import { Readable } from 'stream';
5
+ const multipartThreshold = 1024 * 1024 * 50 // 50MB
6
+ ;
7
+ export async function uploadFile({ buffer, client, collectionPrefix = '', docPrefix, filename, mimeType, tempFilePath, useCompositePrefixes = false }) {
8
+ const fileKey = getFileKey({
9
+ collectionPrefix,
10
+ docPrefix: docPrefix || '',
11
+ filename,
12
+ useCompositePrefixes
13
+ });
14
+ const blockBlobClient = client.getBlockBlobClient(fileKey);
15
+ // when there are no temp files, or the upload is less than the threshold size, do not stream files
16
+ if (!tempFilePath && buffer.length > 0 && buffer.length < multipartThreshold) {
17
+ await blockBlobClient.upload(buffer, buffer.byteLength, {
18
+ blobHTTPHeaders: {
19
+ blobContentType: mimeType
20
+ }
21
+ });
22
+ return;
23
+ }
24
+ const fileBufferOrStream = tempFilePath ? fs.createReadStream(tempFilePath) : Readable.from(buffer);
25
+ await blockBlobClient.uploadStream(fileBufferOrStream, 4 * 1024 * 1024, 4, {
26
+ abortSignal: AbortController.timeout(30 * 60 * 1000),
27
+ blobHTTPHeaders: {
28
+ blobContentType: mimeType
29
+ }
30
+ });
31
+ }
32
+
33
+ //# sourceMappingURL=uploadFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/uploadFile.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\n\nimport { AbortController } from '@azure/abort-controller'\nimport { getFileKey } from '@payloadcms/plugin-cloud-storage/utilities'\nimport fs from 'fs'\nimport { Readable } from 'stream'\n\ninterface UploadArgs {\n buffer: Buffer\n client: ContainerClient\n collectionPrefix?: string\n docPrefix?: string\n filename: string\n mimeType: string\n tempFilePath?: string\n useCompositePrefixes?: boolean\n}\n\nconst multipartThreshold = 1024 * 1024 * 50 // 50MB\n\nexport async function uploadFile({\n buffer,\n client,\n collectionPrefix = '',\n docPrefix,\n filename,\n mimeType,\n tempFilePath,\n useCompositePrefixes = false,\n}: UploadArgs): Promise<void> {\n const fileKey = getFileKey({\n collectionPrefix,\n docPrefix: docPrefix || '',\n filename,\n useCompositePrefixes,\n })\n\n const blockBlobClient = client.getBlockBlobClient(fileKey)\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","getFileKey","fs","Readable","multipartThreshold","uploadFile","buffer","client","collectionPrefix","docPrefix","filename","mimeType","tempFilePath","useCompositePrefixes","fileKey","blockBlobClient","getBlockBlobClient","length","upload","byteLength","blobHTTPHeaders","blobContentType","fileBufferOrStream","createReadStream","from","uploadStream","abortSignal","timeout"],"mappings":"AAEA,SAASA,eAAe,QAAQ,0BAAyB;AACzD,SAASC,UAAU,QAAQ,6CAA4C;AACvE,OAAOC,QAAQ,KAAI;AACnB,SAASC,QAAQ,QAAQ,SAAQ;AAajC,MAAMC,qBAAqB,OAAO,OAAO,GAAG,OAAO;;AAEnD,OAAO,eAAeC,WAAW,EAC/BC,MAAM,EACNC,MAAM,EACNC,mBAAmB,EAAE,EACrBC,SAAS,EACTC,QAAQ,EACRC,QAAQ,EACRC,YAAY,EACZC,uBAAuB,KAAK,EACjB;IACX,MAAMC,UAAUb,WAAW;QACzBO;QACAC,WAAWA,aAAa;QACxBC;QACAG;IACF;IAEA,MAAME,kBAAkBR,OAAOS,kBAAkB,CAACF;IAElD,mGAAmG;IACnG,IAAI,CAACF,gBAAgBN,OAAOW,MAAM,GAAG,KAAKX,OAAOW,MAAM,GAAGb,oBAAoB;QAC5E,MAAMW,gBAAgBG,MAAM,CAACZ,QAAQA,OAAOa,UAAU,EAAE;YACtDC,iBAAiB;gBAAEC,iBAAiBV;YAAS;QAC/C;QACA;IACF;IAEA,MAAMW,qBAA+BV,eACjCV,GAAGqB,gBAAgB,CAACX,gBACpBT,SAASqB,IAAI,CAAClB;IAElB,MAAMS,gBAAgBU,YAAY,CAACH,oBAAoB,IAAI,OAAO,MAAM,GAAG;QACzEI,aAAa1B,gBAAgB2B,OAAO,CAAC,KAAK,KAAK;QAC/CP,iBAAiB;YAAEC,iBAAiBV;QAAS;IAC/C;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/storage-azure",
3
- "version": "3.83.0-internal.791f423",
3
+ "version": "3.83.0-internal.86b7bfb",
4
4
  "description": "Payload storage adapter for Azure Blob Storage",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -39,13 +39,13 @@
39
39
  "dependencies": {
40
40
  "@azure/abort-controller": "^1.1.0",
41
41
  "@azure/storage-blob": "^12.11.0",
42
- "@payloadcms/plugin-cloud-storage": "3.83.0-internal.791f423"
42
+ "@payloadcms/plugin-cloud-storage": "3.83.0-internal.86b7bfb"
43
43
  },
44
44
  "devDependencies": {
45
- "payload": "3.83.0-internal.791f423"
45
+ "payload": "3.83.0-internal.86b7bfb"
46
46
  },
47
47
  "peerDependencies": {
48
- "payload": "3.83.0-internal.791f423"
48
+ "payload": "3.83.0-internal.86b7bfb"
49
49
  },
50
50
  "engines": {
51
51
  "node": "^18.20.2 || >=20.9.0"
@@ -1,10 +0,0 @@
1
- import type { ContainerClient } from '@azure/storage-blob';
2
- import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types';
3
- import type { CollectionConfig } from 'payload';
4
- interface Args {
5
- collection: CollectionConfig;
6
- getStorageClient: () => ContainerClient;
7
- }
8
- export declare const getHandleDelete: ({ getStorageClient }: Args) => HandleDelete;
9
- export {};
10
- //# sourceMappingURL=handleDelete.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleDelete.d.ts","sourceRoot":"","sources":["../src/handleDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAI/C,UAAU,IAAI;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,eAAe,CAAA;CACxC;AAED,eAAO,MAAM,eAAe,yBAA0B,IAAI,KAAG,YAK5D,CAAA"}
@@ -1,9 +0,0 @@
1
- import path from 'path';
2
- export const getHandleDelete = ({ getStorageClient })=>{
3
- return async ({ doc: { prefix = '' }, filename })=>{
4
- const blockBlobClient = getStorageClient().getBlockBlobClient(path.posix.join(prefix, filename));
5
- await blockBlobClient.deleteIfExists();
6
- };
7
- };
8
-
9
- //# sourceMappingURL=handleDelete.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/handleDelete.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\nimport type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload'\n\nimport path from 'path'\n\ninterface Args {\n collection: CollectionConfig\n getStorageClient: () => ContainerClient\n}\n\nexport const getHandleDelete = ({ getStorageClient }: Args): HandleDelete => {\n return async ({ doc: { prefix = '' }, filename }) => {\n const blockBlobClient = getStorageClient().getBlockBlobClient(path.posix.join(prefix, filename))\n await blockBlobClient.deleteIfExists()\n }\n}\n"],"names":["path","getHandleDelete","getStorageClient","doc","prefix","filename","blockBlobClient","getBlockBlobClient","posix","join","deleteIfExists"],"mappings":"AAIA,OAAOA,UAAU,OAAM;AAOvB,OAAO,MAAMC,kBAAkB,CAAC,EAAEC,gBAAgB,EAAQ;IACxD,OAAO,OAAO,EAAEC,KAAK,EAAEC,SAAS,EAAE,EAAE,EAAEC,QAAQ,EAAE;QAC9C,MAAMC,kBAAkBJ,mBAAmBK,kBAAkB,CAACP,KAAKQ,KAAK,CAACC,IAAI,CAACL,QAAQC;QACtF,MAAMC,gBAAgBI,cAAc;IACtC;AACF,EAAC"}
@@ -1,11 +0,0 @@
1
- import type { ContainerClient } from '@azure/storage-blob';
2
- import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types';
3
- import type { CollectionConfig } from 'payload';
4
- interface Args {
5
- collection: CollectionConfig;
6
- getStorageClient: () => ContainerClient;
7
- prefix?: string;
8
- }
9
- export declare const getHandleUpload: ({ getStorageClient, prefix }: Args) => HandleUpload;
10
- export {};
11
- //# sourceMappingURL=handleUpload.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handleUpload.d.ts","sourceRoot":"","sources":["../src/handleUpload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAO/C,UAAU,IAAI;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,eAAe,CAAA;IACvC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAGD,eAAO,MAAM,eAAe,iCAAuC,IAAI,KAAG,YA0BzE,CAAA"}
@@ -1,31 +0,0 @@
1
- import { AbortController } from '@azure/abort-controller';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { Readable } from 'stream';
5
- const multipartThreshold = 1024 * 1024 * 50 // 50MB
6
- ;
7
- export const getHandleUpload = ({ getStorageClient, prefix = '' })=>{
8
- return async ({ data, file })=>{
9
- const fileKey = path.posix.join(data.prefix || prefix, file.filename);
10
- const blockBlobClient = getStorageClient().getBlockBlobClient(fileKey);
11
- // when there are no temp files, or the upload is less than the threshold size, do not stream files
12
- if (!file.tempFilePath && file.buffer.length > 0 && file.buffer.length < multipartThreshold) {
13
- await blockBlobClient.upload(file.buffer, file.buffer.byteLength, {
14
- blobHTTPHeaders: {
15
- blobContentType: file.mimeType
16
- }
17
- });
18
- return data;
19
- }
20
- const fileBufferOrStream = file.tempFilePath ? fs.createReadStream(file.tempFilePath) : Readable.from(file.buffer);
21
- await blockBlobClient.uploadStream(fileBufferOrStream, 4 * 1024 * 1024, 4, {
22
- abortSignal: AbortController.timeout(30 * 60 * 1000),
23
- blobHTTPHeaders: {
24
- blobContentType: file.mimeType
25
- }
26
- });
27
- return data;
28
- };
29
- };
30
-
31
- //# sourceMappingURL=handleUpload.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/handleUpload.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\nimport type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload'\n\nimport { AbortController } from '@azure/abort-controller'\nimport fs from 'fs'\nimport path from 'path'\nimport { Readable } from 'stream'\n\ninterface Args {\n collection: CollectionConfig\n getStorageClient: () => ContainerClient\n prefix?: string\n}\n\nconst multipartThreshold = 1024 * 1024 * 50 // 50MB\nexport const getHandleUpload = ({ getStorageClient, prefix = '' }: Args): HandleUpload => {\n return async ({ data, file }) => {\n const fileKey = path.posix.join(data.prefix || prefix, file.filename)\n\n const blockBlobClient = getStorageClient().getBlockBlobClient(fileKey)\n\n // when there are no temp files, or the upload is less than the threshold size, do not stream files\n if (!file.tempFilePath && file.buffer.length > 0 && file.buffer.length < multipartThreshold) {\n await blockBlobClient.upload(file.buffer, file.buffer.byteLength, {\n blobHTTPHeaders: { blobContentType: file.mimeType },\n })\n\n return data\n }\n\n const fileBufferOrStream: Readable = file.tempFilePath\n ? fs.createReadStream(file.tempFilePath)\n : Readable.from(file.buffer)\n\n await blockBlobClient.uploadStream(fileBufferOrStream, 4 * 1024 * 1024, 4, {\n abortSignal: AbortController.timeout(30 * 60 * 1000),\n blobHTTPHeaders: { blobContentType: file.mimeType },\n })\n\n return data\n }\n}\n"],"names":["AbortController","fs","path","Readable","multipartThreshold","getHandleUpload","getStorageClient","prefix","data","file","fileKey","posix","join","filename","blockBlobClient","getBlockBlobClient","tempFilePath","buffer","length","upload","byteLength","blobHTTPHeaders","blobContentType","mimeType","fileBufferOrStream","createReadStream","from","uploadStream","abortSignal","timeout"],"mappings":"AAIA,SAASA,eAAe,QAAQ,0BAAyB;AACzD,OAAOC,QAAQ,KAAI;AACnB,OAAOC,UAAU,OAAM;AACvB,SAASC,QAAQ,QAAQ,SAAQ;AAQjC,MAAMC,qBAAqB,OAAO,OAAO,GAAG,OAAO;;AACnD,OAAO,MAAMC,kBAAkB,CAAC,EAAEC,gBAAgB,EAAEC,SAAS,EAAE,EAAQ;IACrE,OAAO,OAAO,EAAEC,IAAI,EAAEC,IAAI,EAAE;QAC1B,MAAMC,UAAUR,KAAKS,KAAK,CAACC,IAAI,CAACJ,KAAKD,MAAM,IAAIA,QAAQE,KAAKI,QAAQ;QAEpE,MAAMC,kBAAkBR,mBAAmBS,kBAAkB,CAACL;QAE9D,mGAAmG;QACnG,IAAI,CAACD,KAAKO,YAAY,IAAIP,KAAKQ,MAAM,CAACC,MAAM,GAAG,KAAKT,KAAKQ,MAAM,CAACC,MAAM,GAAGd,oBAAoB;YAC3F,MAAMU,gBAAgBK,MAAM,CAACV,KAAKQ,MAAM,EAAER,KAAKQ,MAAM,CAACG,UAAU,EAAE;gBAChEC,iBAAiB;oBAAEC,iBAAiBb,KAAKc,QAAQ;gBAAC;YACpD;YAEA,OAAOf;QACT;QAEA,MAAMgB,qBAA+Bf,KAAKO,YAAY,GAClDf,GAAGwB,gBAAgB,CAAChB,KAAKO,YAAY,IACrCb,SAASuB,IAAI,CAACjB,KAAKQ,MAAM;QAE7B,MAAMH,gBAAgBa,YAAY,CAACH,oBAAoB,IAAI,OAAO,MAAM,GAAG;YACzEI,aAAa5B,gBAAgB6B,OAAO,CAAC,KAAK,KAAK;YAC/CR,iBAAiB;gBAAEC,iBAAiBb,KAAKc,QAAQ;YAAC;QACpD;QAEA,OAAOf;IACT;AACF,EAAC"}
@@ -1,10 +0,0 @@
1
- import type { ContainerClient } from '@azure/storage-blob';
2
- import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types';
3
- import type { CollectionConfig } from 'payload';
4
- interface Args {
5
- collection: CollectionConfig;
6
- getStorageClient: () => ContainerClient;
7
- }
8
- export declare const getHandler: ({ collection, getStorageClient }: Args) => StaticHandler;
9
- export {};
10
- //# sourceMappingURL=staticHandler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"staticHandler.d.ts","sourceRoot":"","sources":["../src/staticHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA8B,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAA;AAC3E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAuC/C,UAAU,IAAI;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,eAAe,CAAA;CACxC;AAED,eAAO,MAAM,UAAU,qCAAsC,IAAI,KAAG,aAyHnE,CAAA"}
@@ -1,135 +0,0 @@
1
- import { RestError } from '@azure/storage-blob';
2
- import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
3
- import path from 'path';
4
- import { getRangeRequestInfo } from 'payload/internal';
5
- import { sanitizeFilename } from 'payload/shared';
6
- const isNodeReadableStream = (body)=>{
7
- return typeof body === 'object' && body !== null && 'pipe' in body && typeof body.pipe === 'function' && 'destroy' in body && typeof body.destroy === 'function';
8
- };
9
- const abortRequestAndDestroyStream = ({ abortController, blob })=>{
10
- try {
11
- abortController.abort();
12
- } catch {
13
- /* noop */ }
14
- if (blob?.readableStreamBody && isNodeReadableStream(blob.readableStreamBody)) {
15
- blob.readableStreamBody.destroy();
16
- }
17
- };
18
- export const getHandler = ({ collection, getStorageClient })=>{
19
- return async (req, { headers: incomingHeaders, params: { clientUploadContext, filename, prefix: prefixQueryParam } })=>{
20
- let blob = undefined;
21
- let streamed = false;
22
- const abortController = new AbortController();
23
- if (req.signal) {
24
- req.signal.addEventListener('abort', ()=>{
25
- abortRequestAndDestroyStream({
26
- abortController,
27
- blob
28
- });
29
- });
30
- }
31
- try {
32
- const prefix = await getFilePrefix({
33
- clientUploadContext,
34
- collection,
35
- filename,
36
- prefixQueryParam,
37
- req
38
- });
39
- const blockBlobClient = getStorageClient().getBlockBlobClient(path.posix.join(prefix, sanitizeFilename(filename)));
40
- // Get file size for range validation
41
- const properties = await blockBlobClient.getProperties();
42
- const fileSize = properties.contentLength;
43
- if (!fileSize) {
44
- return new Response('Internal Server Error', {
45
- status: 500
46
- });
47
- }
48
- // Handle range request
49
- const rangeHeader = req.headers.get('range');
50
- const rangeResult = getRangeRequestInfo({
51
- fileSize,
52
- rangeHeader
53
- });
54
- if (rangeResult.type === 'invalid') {
55
- return new Response(null, {
56
- headers: new Headers(rangeResult.headers),
57
- status: rangeResult.status
58
- });
59
- }
60
- // Download with range if partial
61
- blob = rangeResult.type === 'partial' ? await blockBlobClient.download(rangeResult.rangeStart, rangeResult.rangeEnd - rangeResult.rangeStart + 1, {
62
- abortSignal: abortController.signal
63
- }) : await blockBlobClient.download(0, undefined, {
64
- abortSignal: abortController.signal
65
- });
66
- let headers = new Headers(incomingHeaders);
67
- // Add range-related headers from the result
68
- for (const [key, value] of Object.entries(rangeResult.headers)){
69
- headers.append(key, value);
70
- }
71
- // Add Azure-specific headers
72
- headers.append('Content-Type', String(properties.contentType));
73
- if (properties.etag) {
74
- headers.append('ETag', String(properties.etag));
75
- }
76
- // Add Content-Security-Policy header for SVG files to prevent executable code
77
- if (properties.contentType === 'image/svg+xml') {
78
- headers.append('Content-Security-Policy', "script-src 'none'");
79
- }
80
- const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match');
81
- if (collection.upload && typeof collection.upload === 'object' && typeof collection.upload.modifyResponseHeaders === 'function') {
82
- headers = collection.upload.modifyResponseHeaders({
83
- headers
84
- }) || headers;
85
- }
86
- if (etagFromHeaders && etagFromHeaders === properties.etag) {
87
- return new Response(null, {
88
- headers,
89
- status: 304
90
- });
91
- }
92
- if (!blob.readableStreamBody || !isNodeReadableStream(blob.readableStreamBody)) {
93
- return new Response('Internal Server Error', {
94
- status: 500
95
- });
96
- }
97
- const stream = blob.readableStreamBody;
98
- stream.on('error', (err)=>{
99
- req.payload.logger.error({
100
- err,
101
- msg: 'Error while streaming Azure blob (aborting)'
102
- });
103
- abortRequestAndDestroyStream({
104
- abortController,
105
- blob
106
- });
107
- });
108
- streamed = true;
109
- return new Response(stream, {
110
- headers,
111
- status: rangeResult.status
112
- });
113
- } catch (err) {
114
- if (err instanceof RestError && err.statusCode === 404) {
115
- return new Response(null, {
116
- status: 404,
117
- statusText: 'Not Found'
118
- });
119
- }
120
- req.payload.logger.error(err);
121
- return new Response('Internal Server Error', {
122
- status: 500
123
- });
124
- } finally{
125
- if (!streamed) {
126
- abortRequestAndDestroyStream({
127
- abortController,
128
- blob
129
- });
130
- }
131
- }
132
- };
133
- };
134
-
135
- //# sourceMappingURL=staticHandler.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/staticHandler.ts"],"sourcesContent":["import type { BlobDownloadResponseParsed, ContainerClient } from '@azure/storage-blob'\nimport type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload'\nimport type { Readable } from 'stream'\n\nimport { RestError } from '@azure/storage-blob'\nimport { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities'\nimport path from 'path'\nimport { getRangeRequestInfo } from 'payload/internal'\nimport { sanitizeFilename } from 'payload/shared'\n\nconst isNodeReadableStream = (\n body: BlobDownloadResponseParsed['readableStreamBody'],\n): body is Readable => {\n return (\n typeof body === 'object' &&\n body !== null &&\n 'pipe' in body &&\n typeof body.pipe === 'function' &&\n 'destroy' in body &&\n typeof body.destroy === 'function'\n )\n}\n\nconst abortRequestAndDestroyStream = ({\n abortController,\n blob,\n}: {\n abortController: AbortController\n blob?: BlobDownloadResponseParsed\n}) => {\n try {\n abortController.abort()\n } catch {\n /* noop */\n }\n if (blob?.readableStreamBody && isNodeReadableStream(blob.readableStreamBody)) {\n blob.readableStreamBody.destroy()\n }\n}\n\ninterface Args {\n collection: CollectionConfig\n getStorageClient: () => ContainerClient\n}\n\nexport const getHandler = ({ collection, getStorageClient }: Args): StaticHandler => {\n return async (\n req,\n {\n headers: incomingHeaders,\n params: { clientUploadContext, filename, prefix: prefixQueryParam },\n },\n ) => {\n let blob: BlobDownloadResponseParsed | undefined = undefined\n let streamed = false\n\n const abortController = new AbortController()\n if (req.signal) {\n req.signal.addEventListener('abort', () => {\n abortRequestAndDestroyStream({ abortController, blob })\n })\n }\n\n try {\n const prefix = await getFilePrefix({\n clientUploadContext,\n collection,\n filename,\n prefixQueryParam,\n req,\n })\n const blockBlobClient = getStorageClient().getBlockBlobClient(\n path.posix.join(prefix, sanitizeFilename(filename)),\n )\n\n // Get file size for range validation\n const properties = await blockBlobClient.getProperties()\n const fileSize = properties.contentLength\n\n if (!fileSize) {\n return new Response('Internal Server Error', { status: 500 })\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 // Download with range if partial\n blob =\n rangeResult.type === 'partial'\n ? await blockBlobClient.download(\n rangeResult.rangeStart,\n rangeResult.rangeEnd - rangeResult.rangeStart + 1,\n { abortSignal: abortController.signal },\n )\n : await blockBlobClient.download(0, undefined, { abortSignal: abortController.signal })\n\n let headers = new Headers(incomingHeaders)\n\n // Add range-related headers from the result\n for (const [key, value] of Object.entries(rangeResult.headers)) {\n headers.append(key, value)\n }\n\n // Add Azure-specific headers\n headers.append('Content-Type', String(properties.contentType))\n if (properties.etag) {\n headers.append('ETag', String(properties.etag))\n }\n\n // Add Content-Security-Policy header for SVG files to prevent executable code\n if (properties.contentType === 'image/svg+xml') {\n headers.append('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 === properties.etag) {\n return new Response(null, {\n headers,\n status: 304,\n })\n }\n\n if (!blob.readableStreamBody || !isNodeReadableStream(blob.readableStreamBody)) {\n return new Response('Internal Server Error', { status: 500 })\n }\n\n const stream = blob.readableStreamBody\n stream.on('error', (err: Error) => {\n req.payload.logger.error({\n err,\n msg: 'Error while streaming Azure blob (aborting)',\n })\n abortRequestAndDestroyStream({ abortController, blob })\n })\n\n streamed = true\n return new Response(stream, { headers, status: rangeResult.status })\n } catch (err: unknown) {\n if (err instanceof RestError && err.statusCode === 404) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n req.payload.logger.error(err)\n return new Response('Internal Server Error', { status: 500 })\n } finally {\n if (!streamed) {\n abortRequestAndDestroyStream({ abortController, blob })\n }\n }\n }\n}\n"],"names":["RestError","getFilePrefix","path","getRangeRequestInfo","sanitizeFilename","isNodeReadableStream","body","pipe","destroy","abortRequestAndDestroyStream","abortController","blob","abort","readableStreamBody","getHandler","collection","getStorageClient","req","headers","incomingHeaders","params","clientUploadContext","filename","prefix","prefixQueryParam","undefined","streamed","AbortController","signal","addEventListener","blockBlobClient","getBlockBlobClient","posix","join","properties","getProperties","fileSize","contentLength","Response","status","rangeHeader","get","rangeResult","type","Headers","download","rangeStart","rangeEnd","abortSignal","key","value","Object","entries","append","String","contentType","etag","etagFromHeaders","upload","modifyResponseHeaders","stream","on","err","payload","logger","error","msg","statusCode","statusText"],"mappings":"AAKA,SAASA,SAAS,QAAQ,sBAAqB;AAC/C,SAASC,aAAa,QAAQ,6CAA4C;AAC1E,OAAOC,UAAU,OAAM;AACvB,SAASC,mBAAmB,QAAQ,mBAAkB;AACtD,SAASC,gBAAgB,QAAQ,iBAAgB;AAEjD,MAAMC,uBAAuB,CAC3BC;IAEA,OACE,OAAOA,SAAS,YAChBA,SAAS,QACT,UAAUA,QACV,OAAOA,KAAKC,IAAI,KAAK,cACrB,aAAaD,QACb,OAAOA,KAAKE,OAAO,KAAK;AAE5B;AAEA,MAAMC,+BAA+B,CAAC,EACpCC,eAAe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