@payloadcms/storage-uploadthing 3.83.0 → 3.84.0-canary.1
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/package.json +4 -4
- package/dist/handleDelete.d.ts +0 -8
- package/dist/handleDelete.d.ts.map +0 -1
- package/dist/handleDelete.js +0 -26
- package/dist/handleDelete.js.map +0 -1
- package/dist/handleUpload.d.ts +0 -10
- package/dist/handleUpload.d.ts.map +0 -1
- package/dist/handleUpload.js +0 -49
- package/dist/handleUpload.js.map +0 -1
- package/dist/staticHandler.d.ts +0 -8
- package/dist/staticHandler.d.ts.map +0 -1
- package/dist/staticHandler.js +0 -140
- package/dist/staticHandler.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-uploadthing",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.84.0-canary.1",
|
|
4
4
|
"description": "Payload storage adapter for uploadthing",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"uploadthing": "7.3.0",
|
|
41
|
-
"@payloadcms/plugin-cloud-storage": "3.
|
|
41
|
+
"@payloadcms/plugin-cloud-storage": "3.84.0-canary.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"payload": "3.
|
|
44
|
+
"payload": "3.84.0-canary.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"payload": "3.
|
|
47
|
+
"payload": "3.84.0-canary.1"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": "^18.20.2 || >=20.9.0"
|
package/dist/handleDelete.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
-
import type { UTApi } from 'uploadthing/server';
|
|
3
|
-
type Args = {
|
|
4
|
-
utApi: UTApi;
|
|
5
|
-
};
|
|
6
|
-
export declare const getHandleDelete: ({ utApi }: Args) => HandleDelete;
|
|
7
|
-
export {};
|
|
8
|
-
//# 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,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAM/C,KAAK,IAAI,GAAG;IACV,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,eAAO,MAAM,eAAe,cAAe,IAAI,KAAG,YAuBjD,CAAA"}
|
package/dist/handleDelete.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { APIError } from 'payload';
|
|
2
|
-
import { getKeyFromFilename } from './utilities.js';
|
|
3
|
-
export const getHandleDelete = ({ utApi })=>{
|
|
4
|
-
return async ({ doc, filename, req })=>{
|
|
5
|
-
const key = getKeyFromFilename(doc, filename);
|
|
6
|
-
if (!key) {
|
|
7
|
-
req.payload.logger.error({
|
|
8
|
-
msg: `Error deleting file: ${filename} - unable to extract key from doc`
|
|
9
|
-
});
|
|
10
|
-
throw new APIError(`Error deleting file: ${filename}`);
|
|
11
|
-
}
|
|
12
|
-
try {
|
|
13
|
-
if (key) {
|
|
14
|
-
await utApi.deleteFiles(key);
|
|
15
|
-
}
|
|
16
|
-
} catch (err) {
|
|
17
|
-
req.payload.logger.error({
|
|
18
|
-
err,
|
|
19
|
-
msg: `Error deleting file with key: ${filename} - key: ${key}`
|
|
20
|
-
});
|
|
21
|
-
throw new APIError(`Error deleting file: ${filename}`);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
//# sourceMappingURL=handleDelete.js.map
|
package/dist/handleDelete.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/handleDelete.ts"],"sourcesContent":["import type { HandleDelete } from '@payloadcms/plugin-cloud-storage/types'\nimport type { UTApi } from 'uploadthing/server'\n\nimport { APIError } from 'payload'\n\nimport { getKeyFromFilename } from './utilities.js'\n\ntype Args = {\n utApi: UTApi\n}\n\nexport const getHandleDelete = ({ utApi }: Args): HandleDelete => {\n return async ({ doc, filename, req }) => {\n const key = getKeyFromFilename(doc, filename)\n\n if (!key) {\n req.payload.logger.error({\n msg: `Error deleting file: ${filename} - unable to extract key from doc`,\n })\n throw new APIError(`Error deleting file: ${filename}`)\n }\n\n try {\n if (key) {\n await utApi.deleteFiles(key)\n }\n } catch (err) {\n req.payload.logger.error({\n err,\n msg: `Error deleting file with key: ${filename} - key: ${key}`,\n })\n throw new APIError(`Error deleting file: ${filename}`)\n }\n }\n}\n"],"names":["APIError","getKeyFromFilename","getHandleDelete","utApi","doc","filename","req","key","payload","logger","error","msg","deleteFiles","err"],"mappings":"AAGA,SAASA,QAAQ,QAAQ,UAAS;AAElC,SAASC,kBAAkB,QAAQ,iBAAgB;AAMnD,OAAO,MAAMC,kBAAkB,CAAC,EAAEC,KAAK,EAAQ;IAC7C,OAAO,OAAO,EAAEC,GAAG,EAAEC,QAAQ,EAAEC,GAAG,EAAE;QAClC,MAAMC,MAAMN,mBAAmBG,KAAKC;QAEpC,IAAI,CAACE,KAAK;YACRD,IAAIE,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBACvBC,KAAK,CAAC,qBAAqB,EAAEN,SAAS,iCAAiC,CAAC;YAC1E;YACA,MAAM,IAAIL,SAAS,CAAC,qBAAqB,EAAEK,UAAU;QACvD;QAEA,IAAI;YACF,IAAIE,KAAK;gBACP,MAAMJ,MAAMS,WAAW,CAACL;YAC1B;QACF,EAAE,OAAOM,KAAK;YACZP,IAAIE,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC;gBACvBG;gBACAF,KAAK,CAAC,8BAA8B,EAAEN,SAAS,QAAQ,EAAEE,KAAK;YAChE;YACA,MAAM,IAAIP,SAAS,CAAC,qBAAqB,EAAEK,UAAU;QACvD;IACF;AACF,EAAC"}
|
package/dist/handleUpload.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
-
import type { UTApi } from 'uploadthing/server';
|
|
3
|
-
import type { ACL } from './index.js';
|
|
4
|
-
type HandleUploadArgs = {
|
|
5
|
-
acl: ACL;
|
|
6
|
-
utApi: UTApi;
|
|
7
|
-
};
|
|
8
|
-
export declare const getHandleUpload: ({ acl, utApi }: HandleUploadArgs) => HandleUpload;
|
|
9
|
-
export {};
|
|
10
|
-
//# 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,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAK/C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,YAAY,CAAA;AAErC,KAAK,gBAAgB,GAAG;IACtB,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,eAAO,MAAM,eAAe,mBAAoB,gBAAgB,KAAG,YAqDlE,CAAA"}
|
package/dist/handleUpload.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { APIError } from 'payload';
|
|
2
|
-
import { UTFile } from 'uploadthing/server';
|
|
3
|
-
export const getHandleUpload = ({ acl, utApi })=>{
|
|
4
|
-
return async ({ clientUploadContext, data, file })=>{
|
|
5
|
-
try {
|
|
6
|
-
if (clientUploadContext && typeof clientUploadContext === 'object' && 'key' in clientUploadContext && typeof clientUploadContext.key === 'string') {
|
|
7
|
-
// Clear the old file
|
|
8
|
-
await utApi.deleteFiles(clientUploadContext.key);
|
|
9
|
-
}
|
|
10
|
-
const { buffer, filename, mimeType } = file;
|
|
11
|
-
const blob = new Blob([
|
|
12
|
-
buffer
|
|
13
|
-
], {
|
|
14
|
-
type: mimeType
|
|
15
|
-
});
|
|
16
|
-
const res = await utApi.uploadFiles(new UTFile([
|
|
17
|
-
blob
|
|
18
|
-
], filename), {
|
|
19
|
-
acl
|
|
20
|
-
});
|
|
21
|
-
if (res.error) {
|
|
22
|
-
throw new APIError(`Error uploading file: ${res.error.code} - ${res.error.message}`);
|
|
23
|
-
}
|
|
24
|
-
// Find matching data.sizes entry
|
|
25
|
-
const foundSize = Object.keys(data.sizes || {}).find((key)=>data.sizes?.[key]?.filename === filename);
|
|
26
|
-
if (foundSize) {
|
|
27
|
-
data.sizes[foundSize]._key = res.data?.key;
|
|
28
|
-
} else {
|
|
29
|
-
data._key = res.data?.key;
|
|
30
|
-
data.filename = res.data?.name;
|
|
31
|
-
}
|
|
32
|
-
return data;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
if (error instanceof Error) {
|
|
35
|
-
// Interrogate uploadthing error which returns FiberFailure
|
|
36
|
-
if ('toJSON' in error && typeof error.toJSON === 'function') {
|
|
37
|
-
const json = error.toJSON();
|
|
38
|
-
if (json.cause?.defect?.error && json.cause.defect.data?.error) {
|
|
39
|
-
throw new APIError(`Error uploading file with uploadthing: ${json.cause.defect.error} - ${json.cause.defect.data.error}`);
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
throw new APIError(`Error uploading file with uploadthing: ${error.message}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
//# sourceMappingURL=handleUpload.js.map
|
package/dist/handleUpload.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/handleUpload.ts"],"sourcesContent":["import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'\nimport type { UTApi } from 'uploadthing/server'\n\nimport { APIError } from 'payload'\nimport { UTFile } from 'uploadthing/server'\n\nimport type { ACL } from './index.js'\n\ntype HandleUploadArgs = {\n acl: ACL\n utApi: UTApi\n}\n\nexport const getHandleUpload = ({ acl, utApi }: HandleUploadArgs): HandleUpload => {\n return async ({ clientUploadContext, data, file }) => {\n try {\n if (\n clientUploadContext &&\n typeof clientUploadContext === 'object' &&\n 'key' in clientUploadContext &&\n typeof clientUploadContext.key === 'string'\n ) {\n // Clear the old file\n await utApi.deleteFiles(clientUploadContext.key)\n }\n\n const { buffer, filename, mimeType } = file\n\n const blob = new Blob([buffer], { type: mimeType })\n const res = await utApi.uploadFiles(new UTFile([blob], filename), { acl })\n\n if (res.error) {\n throw new APIError(`Error uploading file: ${res.error.code} - ${res.error.message}`)\n }\n\n // Find matching data.sizes entry\n const foundSize = Object.keys(data.sizes || {}).find(\n (key) => data.sizes?.[key]?.filename === filename,\n )\n\n if (foundSize) {\n data.sizes[foundSize]._key = res.data?.key\n } else {\n data._key = res.data?.key\n data.filename = res.data?.name\n }\n\n return data\n } catch (error: unknown) {\n if (error instanceof Error) {\n // Interrogate uploadthing error which returns FiberFailure\n if ('toJSON' in error && typeof error.toJSON === 'function') {\n const json = error.toJSON() as {\n cause?: { defect?: { _id?: string; data?: { error?: string }; error?: string } }\n }\n if (json.cause?.defect?.error && json.cause.defect.data?.error) {\n throw new APIError(\n `Error uploading file with uploadthing: ${json.cause.defect.error} - ${json.cause.defect.data.error}`,\n )\n }\n } else {\n throw new APIError(`Error uploading file with uploadthing: ${error.message}`)\n }\n }\n }\n }\n}\n"],"names":["APIError","UTFile","getHandleUpload","acl","utApi","clientUploadContext","data","file","key","deleteFiles","buffer","filename","mimeType","blob","Blob","type","res","uploadFiles","error","code","message","foundSize","Object","keys","sizes","find","_key","name","Error","toJSON","json","cause","defect"],"mappings":"A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1B,2DAA2D;gBAC3D,IAAI,YAAYV,SAAS,OAAOA,MAAMW,MAAM,KAAK,YAAY;oBAC3D,MAAMC,OAAOZ,MAAMW,MAAM;oBAGzB,IAAIC,KAAKC,KAAK,EAAEC,QAAQd,SAASY,KAAKC,KAAK,CAACC,MAAM,CAAC1B,IAAI,EAAEY,OAAO;wBAC9D,MAAM,IAAIlB,SACR,CAAC,uCAAuC,EAAE8B,KAAKC,KAAK,CAACC,MAAM,CAACd,KAAK,CAAC,GAAG,EAAEY,KAAKC,KAAK,CAACC,MAAM,CAAC1B,IAAI,CAACY,KAAK,EAAE;oBAEzG;gBACF,OAAO;oBACL,MAAM,IAAIlB,SAAS,CAAC,uCAAuC,EAAEkB,MAAME,OAAO,EAAE;gBAC9E;YACF;QACF;IACF;AACF,EAAC"}
|
package/dist/staticHandler.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
-
import type { UTApi } from 'uploadthing/server';
|
|
3
|
-
type Args = {
|
|
4
|
-
utApi: UTApi;
|
|
5
|
-
};
|
|
6
|
-
export declare const getHandler: ({ utApi }: Args) => StaticHandler;
|
|
7
|
-
export {};
|
|
8
|
-
//# 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,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAA;AAE3E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAM/C,KAAK,IAAI,GAAG;IACV,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,eAAO,MAAM,UAAU,cAAe,IAAI,KAAG,aAgJ5C,CAAA"}
|
package/dist/staticHandler.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { getRangeRequestInfo } from 'payload/internal';
|
|
2
|
-
import { getKeyFromFilename } from './utilities.js';
|
|
3
|
-
export const getHandler = ({ utApi })=>{
|
|
4
|
-
return async (req, { doc, headers: incomingHeaders, params: { clientUploadContext, collection, filename } })=>{
|
|
5
|
-
try {
|
|
6
|
-
let key;
|
|
7
|
-
const collectionConfig = req.payload.collections[collection]?.config;
|
|
8
|
-
if (clientUploadContext && typeof clientUploadContext === 'object' && 'key' in clientUploadContext && typeof clientUploadContext.key === 'string') {
|
|
9
|
-
key = clientUploadContext.key;
|
|
10
|
-
} else {
|
|
11
|
-
let retrievedDoc = doc;
|
|
12
|
-
if (!retrievedDoc) {
|
|
13
|
-
const or = [
|
|
14
|
-
{
|
|
15
|
-
filename: {
|
|
16
|
-
equals: filename
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
];
|
|
20
|
-
if (collectionConfig?.upload.imageSizes) {
|
|
21
|
-
collectionConfig.upload.imageSizes.forEach(({ name })=>{
|
|
22
|
-
or.push({
|
|
23
|
-
[`sizes.${name}.filename`]: {
|
|
24
|
-
equals: filename
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
const result = await req.payload.db.findOne({
|
|
30
|
-
collection,
|
|
31
|
-
req,
|
|
32
|
-
where: {
|
|
33
|
-
or
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
if (result) {
|
|
37
|
-
retrievedDoc = result;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
if (!retrievedDoc) {
|
|
41
|
-
return new Response(null, {
|
|
42
|
-
status: 404,
|
|
43
|
-
statusText: 'Not Found'
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
key = getKeyFromFilename(retrievedDoc, filename);
|
|
47
|
-
}
|
|
48
|
-
if (!key) {
|
|
49
|
-
return new Response(null, {
|
|
50
|
-
status: 404,
|
|
51
|
-
statusText: 'Not Found'
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
const { url: signedURL } = await utApi.getSignedURL(key);
|
|
55
|
-
if (!signedURL) {
|
|
56
|
-
return new Response(null, {
|
|
57
|
-
status: 404,
|
|
58
|
-
statusText: 'Not Found'
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
// Get file metadata first for range validation
|
|
62
|
-
const headResponse = await fetch(signedURL, {
|
|
63
|
-
method: 'HEAD'
|
|
64
|
-
});
|
|
65
|
-
if (!headResponse.ok) {
|
|
66
|
-
return new Response(null, {
|
|
67
|
-
status: 404,
|
|
68
|
-
statusText: 'Not Found'
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
const fileSize = Number(headResponse.headers.get('content-length'));
|
|
72
|
-
// Handle range request
|
|
73
|
-
const rangeHeader = req.headers.get('range');
|
|
74
|
-
const rangeResult = getRangeRequestInfo({
|
|
75
|
-
fileSize,
|
|
76
|
-
rangeHeader
|
|
77
|
-
});
|
|
78
|
-
if (rangeResult.type === 'invalid') {
|
|
79
|
-
return new Response(null, {
|
|
80
|
-
headers: new Headers(rangeResult.headers),
|
|
81
|
-
status: rangeResult.status
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
const response = await fetch(signedURL, {
|
|
85
|
-
headers: rangeResult.type === 'partial' ? {
|
|
86
|
-
Range: `bytes=${rangeResult.rangeStart}-${rangeResult.rangeEnd}`
|
|
87
|
-
} : undefined
|
|
88
|
-
});
|
|
89
|
-
if (!response.ok || !response.body) {
|
|
90
|
-
return new Response(null, {
|
|
91
|
-
status: 404,
|
|
92
|
-
statusText: 'Not Found'
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match');
|
|
96
|
-
const objectEtag = response.headers.get('etag');
|
|
97
|
-
let headers = new Headers(incomingHeaders);
|
|
98
|
-
// Add range-related headers from the result
|
|
99
|
-
for (const [key, value] of Object.entries(rangeResult.headers)){
|
|
100
|
-
headers.append(key, value);
|
|
101
|
-
}
|
|
102
|
-
const contentType = response.headers.get('content-type');
|
|
103
|
-
if (contentType) {
|
|
104
|
-
headers.append('Content-Type', contentType);
|
|
105
|
-
}
|
|
106
|
-
if (objectEtag) {
|
|
107
|
-
headers.append('ETag', objectEtag);
|
|
108
|
-
}
|
|
109
|
-
// Add Content-Security-Policy header for SVG files to prevent executable code
|
|
110
|
-
if (contentType === 'image/svg+xml') {
|
|
111
|
-
headers.append('Content-Security-Policy', "script-src 'none'");
|
|
112
|
-
}
|
|
113
|
-
if (collectionConfig?.upload && typeof collectionConfig.upload === 'object' && typeof collectionConfig.upload.modifyResponseHeaders === 'function') {
|
|
114
|
-
headers = collectionConfig.upload.modifyResponseHeaders({
|
|
115
|
-
headers
|
|
116
|
-
}) || headers;
|
|
117
|
-
}
|
|
118
|
-
if (etagFromHeaders && etagFromHeaders === objectEtag) {
|
|
119
|
-
return new Response(null, {
|
|
120
|
-
headers,
|
|
121
|
-
status: 304
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
return new Response(response.body, {
|
|
125
|
-
headers,
|
|
126
|
-
status: rangeResult.status
|
|
127
|
-
});
|
|
128
|
-
} catch (err) {
|
|
129
|
-
req.payload.logger.error({
|
|
130
|
-
err,
|
|
131
|
-
msg: 'Unexpected error in staticHandler'
|
|
132
|
-
});
|
|
133
|
-
return new Response('Internal Server Error', {
|
|
134
|
-
status: 500
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
//# sourceMappingURL=staticHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/staticHandler.ts"],"sourcesContent":["import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'\nimport type { Where } from 'payload'\nimport type { UTApi } from 'uploadthing/server'\n\nimport { getRangeRequestInfo } from 'payload/internal'\n\nimport { getKeyFromFilename } from './utilities.js'\n\ntype Args = {\n utApi: UTApi\n}\n\nexport const getHandler = ({ utApi }: Args): StaticHandler => {\n return async (\n req,\n { doc, headers: incomingHeaders, params: { clientUploadContext, collection, filename } },\n ) => {\n try {\n let key: string\n const collectionConfig = req.payload.collections[collection]?.config\n\n if (\n clientUploadContext &&\n typeof clientUploadContext === 'object' &&\n 'key' in clientUploadContext &&\n typeof clientUploadContext.key === 'string'\n ) {\n key = clientUploadContext.key\n } else {\n let retrievedDoc = doc\n\n if (!retrievedDoc) {\n const or: Where[] = [\n {\n filename: {\n equals: filename,\n },\n },\n ]\n\n if (collectionConfig?.upload.imageSizes) {\n collectionConfig.upload.imageSizes.forEach(({ name }) => {\n or.push({\n [`sizes.${name}.filename`]: {\n equals: filename,\n },\n })\n })\n }\n\n const result = await req.payload.db.findOne({\n collection,\n req,\n where: { or },\n })\n\n if (result) {\n retrievedDoc = result\n }\n }\n\n if (!retrievedDoc) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n key = getKeyFromFilename(retrievedDoc, filename)!\n }\n\n if (!key) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n const { url: signedURL } = await utApi.getSignedURL(key)\n\n if (!signedURL) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n // Get file metadata first for range validation\n const headResponse = await fetch(signedURL, { method: 'HEAD' })\n if (!headResponse.ok) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n const fileSize = Number(headResponse.headers.get('content-length'))\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 const response = await fetch(signedURL, {\n headers:\n rangeResult.type === 'partial'\n ? { Range: `bytes=${rangeResult.rangeStart}-${rangeResult.rangeEnd}` }\n : undefined,\n })\n\n if (!response.ok || !response.body) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n\n const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match')\n const objectEtag = response.headers.get('etag')\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 const contentType = response.headers.get('content-type')\n if (contentType) {\n headers.append('Content-Type', contentType)\n }\n\n if (objectEtag) {\n headers.append('ETag', objectEtag)\n }\n\n // Add Content-Security-Policy header for SVG files to prevent executable code\n if (contentType === 'image/svg+xml') {\n headers.append('Content-Security-Policy', \"script-src 'none'\")\n }\n\n if (\n collectionConfig?.upload &&\n typeof collectionConfig.upload === 'object' &&\n typeof collectionConfig.upload.modifyResponseHeaders === 'function'\n ) {\n headers = collectionConfig.upload.modifyResponseHeaders({ headers }) || headers\n }\n\n if (etagFromHeaders && etagFromHeaders === objectEtag) {\n return new Response(null, {\n headers,\n status: 304,\n })\n }\n\n return new Response(response.body, {\n headers,\n status: rangeResult.status,\n })\n } catch (err) {\n req.payload.logger.error({ err, msg: 'Unexpected error in staticHandler' })\n return new Response('Internal Server Error', { status: 500 })\n }\n }\n}\n"],"names":["getRangeRequestInfo","getKeyFromFilename","getHandler","utApi","req","doc","headers","incomingHeaders","params","clientUploadContext","collection","filename","key","collectionConfig","payload","collections","config","retrievedDoc","or","equals","upload","imageSizes","forEach","name","push","result","db","findOne","where","Response","status","statusText","url","signedURL","getSignedURL","headResponse","fetch","method","ok","fileSize","Number","get","rangeHeader","rangeResult","type","Headers","response","Range","rangeStart","rangeEnd","undefined","body","etagFromHeaders","objectEtag","value","Object","entries","append","contentType","modifyResponseHeaders","err","logger","error","msg"],"mappings":"AAIA,SAASA,mBAAmB,QAAQ,mBAAkB;AAEtD,SAASC,kBAAkB,QAAQ,iBAAgB;AAMnD,OAAO,MAAMC,aAAa,CAAC,EAAEC,KAAK,EAAQ;IACxC,OAAO,OACLC,KACA,EAAEC,GAAG,EAAEC,SAASC,eAAe,EAAEC,QAAQ,EAAEC,mBAAmB,EAAEC,UAAU,EAAEC,QAAQ,EAAE,EAAE;QAExF,IAAI;YACF,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