@payloadcms/figma 0.1.0-alpha.10 → 0.1.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/control-plane.d.ts +11 -0
- package/dist/api/control-plane.js +26 -0
- package/dist/auth/headless-oauth-flow.js +4 -0
- package/dist/auth/oauth-flow.js +17 -0
- package/dist/auth/refresh.js +12 -12
- package/dist/auth/requested-scopes.d.ts +1 -0
- package/dist/auth/requested-scopes.js +3 -0
- package/dist/auth/token-store.d.ts +16 -0
- package/dist/auth/token-store.js +33 -0
- package/dist/auth/types.d.ts +5 -3
- package/dist/commands/bootstrap.js +3 -0
- package/dist/commands/init.js +71 -1
- package/dist/commands/upgrade.js +65 -1
- package/dist/commands/upload-schema.js +3 -4
- package/dist/config/oauth.js +6 -1
- package/dist/constants.d.ts +0 -2
- package/dist/constants.js +0 -3
- package/dist/db-content-api/utilities/data/index.js +6 -0
- package/dist/db-content-api/utilities/schema/buildDocumentSchema.js +9 -4
- package/dist/plugin/build-config.d.ts +9 -1
- package/dist/plugin/build-config.js +158 -17
- package/dist/plugin/cloud-limits/CloudLimitModal/index.d.ts +2 -1
- package/dist/plugin/cloud-limits/icons/ExternalLinkIcon.d.ts +2 -1
- package/dist/storage-content-api/api-types.d.ts +5 -2
- package/dist/storage-content-api/buildBaseMediaResponseHeaders.d.ts +1 -0
- package/dist/storage-content-api/buildBaseMediaResponseHeaders.js +8 -0
- package/dist/storage-content-api/buildClientUploadFetchBackHeaders.d.ts +12 -0
- package/dist/storage-content-api/buildClientUploadFetchBackHeaders.js +26 -0
- package/dist/storage-content-api/buildMediaRequestHeaders.d.ts +1 -0
- package/dist/storage-content-api/buildMediaRequestHeaders.js +14 -0
- package/dist/storage-content-api/buildMediaResponseHeaders.d.ts +4 -0
- package/dist/storage-content-api/buildMediaResponseHeaders.js +52 -0
- package/dist/storage-content-api/client-uploads/generate.d.ts +7 -3
- package/dist/storage-content-api/client-uploads/generate.js +20 -20
- package/dist/storage-content-api/copyFile.d.ts +16 -0
- package/dist/storage-content-api/copyFile.js +90 -0
- package/dist/storage-content-api/deleteFile.d.ts +8 -0
- package/dist/storage-content-api/deleteFile.js +18 -0
- package/dist/storage-content-api/getDownloadResponseHeaderPolicy.d.ts +3 -3
- package/dist/storage-content-api/getDownloadResponseHeaderPolicy.js +10 -4
- package/dist/storage-content-api/getMediaType.d.ts +1 -0
- package/dist/storage-content-api/getMediaType.js +3 -0
- package/dist/storage-content-api/handleUpload.d.ts +1 -2
- package/dist/storage-content-api/handleUpload.js +13 -157
- package/dist/storage-content-api/hasSupportedContentEncoding.d.ts +1 -0
- package/dist/storage-content-api/hasSupportedContentEncoding.js +6 -0
- package/dist/storage-content-api/index.js +16 -8
- package/dist/storage-content-api/mediaErrorResponse.d.ts +1 -0
- package/dist/storage-content-api/mediaErrorResponse.js +12 -0
- package/dist/storage-content-api/shouldAttachMedia.d.ts +1 -0
- package/dist/storage-content-api/shouldAttachMedia.js +34 -0
- package/dist/storage-content-api/staticHandler.d.ts +2 -1
- package/dist/storage-content-api/staticHandler.js +85 -31
- package/dist/storage-content-api/uploadFileData.d.ts +10 -0
- package/dist/storage-content-api/uploadFileData.js +145 -0
- package/dist/utils/cache-bootstrap.js +1 -0
- package/dist/utils/env-management.d.ts +15 -0
- package/dist/utils/env-management.js +47 -0
- package/dist/utils/load-payload-config.js +1 -6
- package/dist/utils/payload-config-ast.d.ts +5 -0
- package/dist/utils/payload-config-ast.js +73 -4
- package/dist/utils/payload-config-modifier.js +3 -0
- package/dist/utils/payload-secret.d.ts +28 -0
- package/dist/utils/payload-secret.js +34 -0
- package/package.json +15 -13
- package/dist/storage-content-api/handleDelete.d.ts +0 -7
- package/dist/storage-content-api/handleDelete.js +0 -15
- package/dist/storage-content-api/utilities/getSafeFilename.d.ts +0 -11
- package/dist/storage-content-api/utilities/getSafeFilename.js +0 -59
- package/dist/storage-content-api/utilities/index.d.ts +0 -2
- package/dist/storage-content-api/utilities/index.js +0 -2
- package/dist/storage-content-api/utilities/parseUploadReference.d.ts +0 -17
- package/dist/storage-content-api/utilities/parseUploadReference.js +0 -27
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DeleteFile } from '@payloadcms/plugin-cloud-storage/types';
|
|
2
|
+
import type { StorageClient } from './client.js';
|
|
3
|
+
type Args = {
|
|
4
|
+
client: StorageClient;
|
|
5
|
+
};
|
|
6
|
+
/** Deletes the object at `storageFilePath`. Payload resolves the complete path. */
|
|
7
|
+
export declare const getDeleteFile: ({ client }: Args) => DeleteFile;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
/** Deletes the object at `storageFilePath`. Payload resolves the complete path. */ export const getDeleteFile = ({ client })=>{
|
|
3
|
+
return async ({ collection, storageFilePath })=>{
|
|
4
|
+
const { error, response } = await client.DELETE('/api/v0/uploads/{collectionId}/{filename}', {
|
|
5
|
+
params: {
|
|
6
|
+
path: {
|
|
7
|
+
collectionId: collection.slug,
|
|
8
|
+
filename: storageFilePath
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
if (error || !response.ok) {
|
|
13
|
+
throw new APIError(`Failed to delete file: ${error?.message ?? response.statusText}`, response.status, {
|
|
14
|
+
code: error?.code
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import type { Payload } from 'payload';
|
|
1
2
|
export declare const DEFAULT_MEDIA_CACHE_CONTROL = "private, no-store";
|
|
2
3
|
export declare const MAX_MEDIA_CACHE_CONTROL_LENGTH = 1024;
|
|
3
4
|
type DownloadResponseHeaderPolicy = {
|
|
4
5
|
cacheControl: string;
|
|
5
|
-
hasIgnoredHeaders: boolean;
|
|
6
|
-
rejectedCacheControlOverride: boolean;
|
|
7
6
|
};
|
|
8
|
-
export declare const getDownloadResponseHeaderPolicy: ({ responseHeaders, }: {
|
|
7
|
+
export declare const getDownloadResponseHeaderPolicy: ({ logger, responseHeaders, }: {
|
|
8
|
+
logger: Pick<Payload["logger"], "debug">;
|
|
9
9
|
responseHeaders: Headers;
|
|
10
10
|
}) => DownloadResponseHeaderPolicy;
|
|
11
11
|
export {};
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
export const DEFAULT_MEDIA_CACHE_CONTROL = 'private, no-store';
|
|
2
2
|
export const MAX_MEDIA_CACHE_CONTROL_LENGTH = 1024;
|
|
3
3
|
const PRINTABLE_ASCII_REGEX = /^[\x20-\x7e]+$/;
|
|
4
|
-
export const getDownloadResponseHeaderPolicy = ({ responseHeaders })=>{
|
|
4
|
+
export const getDownloadResponseHeaderPolicy = ({ logger, responseHeaders })=>{
|
|
5
5
|
const configuredCacheControl = responseHeaders.get('Cache-Control')?.trim();
|
|
6
6
|
const isCacheControlValid = hasValidCacheControl(configuredCacheControl);
|
|
7
7
|
const hasIgnoredHeaders = [
|
|
8
8
|
...responseHeaders.keys()
|
|
9
9
|
].some((name)=>name !== 'cache-control');
|
|
10
|
+
const rejectedCacheControlOverride = !isCacheControlValid;
|
|
11
|
+
if (hasIgnoredHeaders || rejectedCacheControlOverride) {
|
|
12
|
+
logger.debug({
|
|
13
|
+
hasIgnoredResponseHeaders: hasIgnoredHeaders,
|
|
14
|
+
msg: 'Filtered media response header overrides',
|
|
15
|
+
rejectedCacheControlOverride
|
|
16
|
+
});
|
|
17
|
+
}
|
|
10
18
|
return {
|
|
11
|
-
cacheControl: isCacheControlValid && configuredCacheControl ? configuredCacheControl : DEFAULT_MEDIA_CACHE_CONTROL
|
|
12
|
-
hasIgnoredHeaders,
|
|
13
|
-
rejectedCacheControlOverride: !isCacheControlValid
|
|
19
|
+
cacheControl: isCacheControlValid && configuredCacheControl ? configuredCacheControl : DEFAULT_MEDIA_CACHE_CONTROL
|
|
14
20
|
};
|
|
15
21
|
};
|
|
16
22
|
function hasValidCacheControl(cacheControl) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getMediaType(headers: Headers): string;
|
|
@@ -4,7 +4,6 @@ import type { StorageClient } from './client.js';
|
|
|
4
4
|
type Args = {
|
|
5
5
|
client: StorageClient;
|
|
6
6
|
collection: CollectionConfig;
|
|
7
|
-
prefix?: string;
|
|
8
7
|
};
|
|
9
|
-
export declare const getHandleUpload: ({ client, collection
|
|
8
|
+
export declare const getHandleUpload: ({ client, collection }: Args) => HandleUpload;
|
|
10
9
|
export {};
|
|
@@ -1,162 +1,18 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const MAX_FILE_SIZE_BEFORE_MULTIPART = 100 * 1024 * 1024 // 100MB
|
|
8
|
-
;
|
|
9
|
-
export const getHandleUpload = ({ client, collection, prefix = '' })=>{
|
|
10
|
-
return async ({ data, file })=>{
|
|
11
|
-
// Skip if the client already uploaded THIS SPECIFIC file. Resized versions
|
|
12
|
-
// (different filename) should still be uploaded.
|
|
13
|
-
const reference = parseUploadReference(file.uploadReference);
|
|
14
|
-
const isOriginalClientUploaded = reference.clientUploaded === true && file.filename === reference.filename;
|
|
15
|
-
if (isOriginalClientUploaded) {
|
|
16
|
-
return data;
|
|
17
|
-
}
|
|
18
|
-
const collectionId = collection.slug;
|
|
19
|
-
const fileKey = path.posix.join(data.prefix || prefix, file.filename);
|
|
2
|
+
import { uploadFileData } from './uploadFileData.js';
|
|
3
|
+
// Payload skips the original of a verified direct browser upload, so every file here
|
|
4
|
+
// must be written. `storageFilePath` already includes the prefixes and `_objectKey`.
|
|
5
|
+
export const getHandleUpload = ({ client, collection })=>{
|
|
6
|
+
return async ({ data, file, storageFilePath })=>{
|
|
20
7
|
const fileData = file.tempFilePath ? fs.readFileSync(file.tempFilePath) : file.buffer;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
} else {
|
|
31
|
-
await uploadSingleFile({
|
|
32
|
-
client,
|
|
33
|
-
collectionId,
|
|
34
|
-
fileData,
|
|
35
|
-
fileKey,
|
|
36
|
-
mimeType: file.mimeType
|
|
37
|
-
});
|
|
38
|
-
}
|
|
8
|
+
await uploadFileData({
|
|
9
|
+
client,
|
|
10
|
+
collectionId: collection.slug,
|
|
11
|
+
fileData,
|
|
12
|
+
fileKey: storageFilePath,
|
|
13
|
+
fileSize: file.filesize || fileData.length,
|
|
14
|
+
mimeType: file.mimeType
|
|
15
|
+
});
|
|
39
16
|
return data;
|
|
40
17
|
};
|
|
41
18
|
};
|
|
42
|
-
async function uploadSingleFile({ client, collectionId, fileData, fileKey, mimeType }) {
|
|
43
|
-
const { data, error, response } = await client.POST('/api/v0/uploads/sign', {
|
|
44
|
-
body: {
|
|
45
|
-
collectionId,
|
|
46
|
-
contentType: mimeType,
|
|
47
|
-
expiresIn: 3600,
|
|
48
|
-
filename: fileKey
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
if (error) {
|
|
52
|
-
throw new APIError(`Failed to get presigned upload URL: ${error.message}`, response.status, {
|
|
53
|
-
code: error.code
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
if (!data) {
|
|
57
|
-
throw new Error('No response while getting presigned upload URL');
|
|
58
|
-
}
|
|
59
|
-
const uploadResponse = await fetch(data.url, {
|
|
60
|
-
// TS's BodyInit type rejects Buffer<ArrayBufferLike>; wrap as Uint8Array
|
|
61
|
-
// (a valid ArrayBufferView) — byte-identical.
|
|
62
|
-
body: new Uint8Array(fileData),
|
|
63
|
-
headers: {
|
|
64
|
-
'Content-Type': mimeType
|
|
65
|
-
},
|
|
66
|
-
method: 'PUT'
|
|
67
|
-
});
|
|
68
|
-
if (!uploadResponse.ok) {
|
|
69
|
-
throw new Error(`Failed to upload to S3: ${uploadResponse.status} ${uploadResponse.statusText}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async function uploadMultipart({ client, collectionId, fileData, fileKey, mimeType }) {
|
|
73
|
-
const { data: initiateData, error: initiateError, response: initiateResponse } = await client.POST('/api/v0/uploads/multipart/initiate', {
|
|
74
|
-
body: {
|
|
75
|
-
collectionId,
|
|
76
|
-
contentType: mimeType,
|
|
77
|
-
filename: fileKey
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
if (initiateError) {
|
|
81
|
-
throw new APIError(`Failed to initiate multipart upload: ${initiateError.message}`, initiateResponse.status, {
|
|
82
|
-
code: initiateError.code
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
if (!initiateData) {
|
|
86
|
-
throw new Error('No response while initiating multipart upload');
|
|
87
|
-
}
|
|
88
|
-
const { uploadId } = initiateData;
|
|
89
|
-
try {
|
|
90
|
-
const numParts = Math.ceil(fileData.length / PART_SIZE);
|
|
91
|
-
const partNumbers = Array.from({
|
|
92
|
-
length: numParts
|
|
93
|
-
}, (_, i)=>i + 1);
|
|
94
|
-
const { data: partsData, error: partsError, response: partsResponse } = await client.POST('/api/v0/uploads/multipart/parts', {
|
|
95
|
-
body: {
|
|
96
|
-
collectionId,
|
|
97
|
-
expiresIn: 3600,
|
|
98
|
-
filename: fileKey,
|
|
99
|
-
partNumbers,
|
|
100
|
-
uploadId
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
if (partsError) {
|
|
104
|
-
throw new APIError(`Failed to get part URLs: ${partsError.message}`, partsResponse.status, {
|
|
105
|
-
code: partsError.code
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
if (!partsData) {
|
|
109
|
-
throw new Error('No response while getting part URLs');
|
|
110
|
-
}
|
|
111
|
-
const { parts } = partsData;
|
|
112
|
-
if (!parts || !Array.isArray(parts)) {
|
|
113
|
-
throw new Error('Invalid response from part URLs endpoint: parts is not an array');
|
|
114
|
-
}
|
|
115
|
-
const uploadedParts = await Promise.all(parts.map(async ({ partNumber, url })=>{
|
|
116
|
-
const start = (partNumber - 1) * PART_SIZE;
|
|
117
|
-
const end = Math.min(start + PART_SIZE, fileData.length);
|
|
118
|
-
const partData = fileData.slice(start, end);
|
|
119
|
-
const uploadResponse = await fetch(url, {
|
|
120
|
-
body: partData,
|
|
121
|
-
headers: {
|
|
122
|
-
'Content-Type': mimeType
|
|
123
|
-
},
|
|
124
|
-
method: 'PUT'
|
|
125
|
-
});
|
|
126
|
-
const etag = uploadResponse.headers.get('ETag');
|
|
127
|
-
if (!etag) {
|
|
128
|
-
throw new Error(`No ETag returned for part ${partNumber}`);
|
|
129
|
-
}
|
|
130
|
-
return {
|
|
131
|
-
ETag: etag,
|
|
132
|
-
PartNumber: partNumber
|
|
133
|
-
};
|
|
134
|
-
}));
|
|
135
|
-
const { error: completeError, response: completeResponse } = await client.POST('/api/v0/uploads/multipart/complete', {
|
|
136
|
-
body: {
|
|
137
|
-
collectionId,
|
|
138
|
-
filename: fileKey,
|
|
139
|
-
parts: uploadedParts,
|
|
140
|
-
uploadId
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
if (completeError) {
|
|
144
|
-
throw new APIError(`Failed to complete multipart upload: ${completeError.message}`, completeResponse.status, {
|
|
145
|
-
code: completeError.code
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
} catch (error) {
|
|
149
|
-
try {
|
|
150
|
-
await client.POST('/api/v0/uploads/multipart/abort', {
|
|
151
|
-
body: {
|
|
152
|
-
collectionId,
|
|
153
|
-
filename: fileKey,
|
|
154
|
-
uploadId
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
} catch {
|
|
158
|
-
// Ignore abort errors
|
|
159
|
-
}
|
|
160
|
-
throw error;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hasSupportedContentEncoding(headers: Headers): boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Fetch can automatically decompress a file without updating its size or byte-range headers.
|
|
2
|
+
// Reject compressed responses so the headers still match the bytes we send.
|
|
3
|
+
export function hasSupportedContentEncoding(headers) {
|
|
4
|
+
const contentEncoding = headers.get('content-encoding')?.trim().toLowerCase();
|
|
5
|
+
return contentEncoding == null || contentEncoding === '' || contentEncoding === 'identity';
|
|
6
|
+
}
|
|
@@ -1,36 +1,44 @@
|
|
|
1
1
|
import { createStorageClient } from './client.js';
|
|
2
2
|
import { getGenerateUploadInstructions } from './client-uploads/generate.js';
|
|
3
|
+
import { getCopyFile } from './copyFile.js';
|
|
4
|
+
import { getDeleteFile } from './deleteFile.js';
|
|
3
5
|
import { getGenerateURL } from './generateURL.js';
|
|
4
|
-
import { getHandleDelete } from './handleDelete.js';
|
|
5
6
|
import { getHandleUpload } from './handleUpload.js';
|
|
6
7
|
import { getHandler } from './staticHandler.js';
|
|
7
8
|
export function contentApiStorageAdapter(config) {
|
|
8
9
|
return ({ collection, prefix })=>{
|
|
9
10
|
const client = createStorageClient(config);
|
|
10
11
|
const baseUrl = config.baseUrl;
|
|
12
|
+
const deleteFile = getDeleteFile({
|
|
13
|
+
client
|
|
14
|
+
});
|
|
11
15
|
return {
|
|
12
16
|
name: 'content-api',
|
|
17
|
+
copyFile: getCopyFile({
|
|
18
|
+
client
|
|
19
|
+
}),
|
|
20
|
+
deleteFile,
|
|
13
21
|
generateURL: getGenerateURL({
|
|
14
22
|
baseUrl
|
|
15
23
|
}),
|
|
16
|
-
handleDelete:
|
|
17
|
-
client
|
|
18
|
-
}),
|
|
24
|
+
handleDelete: deleteFile,
|
|
19
25
|
handleUpload: getHandleUpload({
|
|
20
26
|
client,
|
|
21
|
-
collection
|
|
22
|
-
prefix
|
|
27
|
+
collection
|
|
23
28
|
}),
|
|
24
29
|
staticHandler: getHandler({
|
|
25
30
|
client,
|
|
26
|
-
collection
|
|
31
|
+
collection,
|
|
32
|
+
prefix
|
|
27
33
|
}),
|
|
28
34
|
uploadInstructions: {
|
|
29
35
|
enabled: true,
|
|
30
36
|
generate: getGenerateUploadInstructions({
|
|
31
37
|
access: config.uploadInstructionsAccess,
|
|
32
|
-
client
|
|
38
|
+
client,
|
|
39
|
+
collectionPrefix: prefix
|
|
33
40
|
}),
|
|
41
|
+
requiresUploadReceipt: true,
|
|
34
42
|
useInAdmin: true
|
|
35
43
|
}
|
|
36
44
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function mediaErrorResponse(status: number, body: string, contentRange?: string): Response;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { buildBaseMediaResponseHeaders } from './buildBaseMediaResponseHeaders.js';
|
|
2
|
+
export function mediaErrorResponse(status, body, contentRange) {
|
|
3
|
+
const headers = buildBaseMediaResponseHeaders();
|
|
4
|
+
headers.set('Content-Type', 'text/plain;charset=UTF-8');
|
|
5
|
+
if (contentRange) {
|
|
6
|
+
headers.set('Content-Range', contentRange);
|
|
7
|
+
}
|
|
8
|
+
return new Response(body, {
|
|
9
|
+
headers,
|
|
10
|
+
status
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function shouldAttachMedia(upstreamHeaders: Headers): boolean;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getMediaType } from './getMediaType.js';
|
|
2
|
+
// Only these media types may render inline on the application's origin.
|
|
3
|
+
// Unknown or active content must download as an attachment.
|
|
4
|
+
const INLINE_MEDIA_TYPES = new Set([
|
|
5
|
+
'application/font-woff',
|
|
6
|
+
'audio/aac',
|
|
7
|
+
'audio/flac',
|
|
8
|
+
'audio/mp4',
|
|
9
|
+
'audio/mpeg',
|
|
10
|
+
'audio/ogg',
|
|
11
|
+
'audio/wav',
|
|
12
|
+
'audio/webm',
|
|
13
|
+
'font/otf',
|
|
14
|
+
'font/ttf',
|
|
15
|
+
'font/woff',
|
|
16
|
+
'font/woff2',
|
|
17
|
+
'image/avif',
|
|
18
|
+
'image/bmp',
|
|
19
|
+
'image/gif',
|
|
20
|
+
'image/jpeg',
|
|
21
|
+
'image/png',
|
|
22
|
+
'image/vnd.microsoft.icon',
|
|
23
|
+
'image/webp',
|
|
24
|
+
'image/x-icon',
|
|
25
|
+
'video/mp4',
|
|
26
|
+
'video/mpeg',
|
|
27
|
+
'video/ogg',
|
|
28
|
+
'video/quicktime',
|
|
29
|
+
'video/webm'
|
|
30
|
+
]);
|
|
31
|
+
export function shouldAttachMedia(upstreamHeaders) {
|
|
32
|
+
const mediaType = getMediaType(upstreamHeaders);
|
|
33
|
+
return !mediaType || !INLINE_MEDIA_TYPES.has(mediaType);
|
|
34
|
+
}
|
|
@@ -4,6 +4,7 @@ import type { StorageClient } from './client.js';
|
|
|
4
4
|
type Args = {
|
|
5
5
|
client: StorageClient;
|
|
6
6
|
collection: CollectionConfig;
|
|
7
|
+
prefix?: string;
|
|
7
8
|
};
|
|
8
|
-
export declare const getHandler: ({ client, collection }: Args) => StaticHandler;
|
|
9
|
+
export declare const getHandler: ({ client, collection, prefix }: Args) => StaticHandler;
|
|
9
10
|
export {};
|
|
@@ -1,23 +1,43 @@
|
|
|
1
|
-
import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { buildStoragePathData, getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
+
import { buildClientUploadFetchBackHeaders } from './buildClientUploadFetchBackHeaders.js';
|
|
3
|
+
import { buildMediaRequestHeaders } from './buildMediaRequestHeaders.js';
|
|
4
|
+
import { buildMediaResponseHeaders } from './buildMediaResponseHeaders.js';
|
|
5
|
+
import { getDownloadResponseHeaderPolicy } from './getDownloadResponseHeaderPolicy.js';
|
|
6
|
+
import { hasSupportedContentEncoding } from './hasSupportedContentEncoding.js';
|
|
7
|
+
import { mediaErrorResponse } from './mediaErrorResponse.js';
|
|
8
|
+
import { resolveDownloadResponseHeaders } from './resolveDownloadResponseHeaders.js';
|
|
5
9
|
const PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS = 300;
|
|
6
|
-
export const getHandler = ({ client, collection })=>{
|
|
7
|
-
return async (req, { params: { filename, uploadReference } })=>{
|
|
10
|
+
export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
11
|
+
return async (req, { doc, headers, params: { filename, operation, uploadReference } })=>{
|
|
8
12
|
try {
|
|
9
|
-
const
|
|
13
|
+
const prefixArgs = {
|
|
10
14
|
collection,
|
|
15
|
+
collectionPrefix: prefix,
|
|
16
|
+
doc,
|
|
11
17
|
filename,
|
|
12
18
|
req,
|
|
13
19
|
uploadReference
|
|
20
|
+
};
|
|
21
|
+
const docPrefix = await getFilePrefix(prefixArgs);
|
|
22
|
+
const { storageFilePath: key } = buildStoragePathData({
|
|
23
|
+
collectionPrefix: prefix,
|
|
24
|
+
docPrefix,
|
|
25
|
+
filename
|
|
26
|
+
});
|
|
27
|
+
const resolvedResponseHeaders = resolveDownloadResponseHeaders({
|
|
28
|
+
collection,
|
|
29
|
+
payloadResponseHeaders: headers
|
|
30
|
+
});
|
|
31
|
+
const responseHeaderPolicy = getDownloadResponseHeaderPolicy({
|
|
32
|
+
logger: req.payload.logger,
|
|
33
|
+
responseHeaders: resolvedResponseHeaders
|
|
14
34
|
});
|
|
15
|
-
const key = path.posix.join(prefix, filename);
|
|
16
35
|
const { data, error } = await client.POST('/api/v0/uploads/download/sign', {
|
|
17
36
|
body: {
|
|
18
37
|
collectionId: collection.slug,
|
|
19
38
|
expiresIn: PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS,
|
|
20
|
-
filename: key
|
|
39
|
+
filename: key,
|
|
40
|
+
responseHeaders: responseHeaderPolicy
|
|
21
41
|
}
|
|
22
42
|
});
|
|
23
43
|
if (error || !data) {
|
|
@@ -26,29 +46,53 @@ export const getHandler = ({ client, collection })=>{
|
|
|
26
46
|
status: 500
|
|
27
47
|
});
|
|
28
48
|
}
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
const isClientUpload = Boolean(uploadReference);
|
|
50
|
+
// Lambda responses have a 6 MB limit, so redirect normal media to its signed storage URL.
|
|
51
|
+
// Payload reads client uploads and transform sources itself, so they need a response body.
|
|
52
|
+
if (isRunningInLambda() && !isClientUpload && operation !== 'transform') {
|
|
53
|
+
return Response.redirect(data.url, 302);
|
|
54
|
+
}
|
|
55
|
+
const requestHeaders = buildMediaRequestHeaders(req.headers);
|
|
56
|
+
let fileResponse;
|
|
57
|
+
try {
|
|
58
|
+
fileResponse = await fetch(data.url, {
|
|
59
|
+
credentials: 'omit',
|
|
60
|
+
headers: requestHeaders,
|
|
61
|
+
redirect: 'manual',
|
|
62
|
+
referrerPolicy: 'no-referrer',
|
|
63
|
+
signal: req.signal
|
|
64
|
+
});
|
|
65
|
+
} catch {
|
|
66
|
+
// Fetch errors can contain the signed URL; log only a fixed message to avoid exposing it.
|
|
67
|
+
req.payload.logger.error('Failed to fetch file from storage');
|
|
68
|
+
return mediaErrorResponse(500, 'Internal Server Error');
|
|
69
|
+
}
|
|
70
|
+
if (fileResponse.status === 416 && requestHeaders.has('Range')) {
|
|
71
|
+
await cancelResponseBody(fileResponse);
|
|
72
|
+
return mediaErrorResponse(416, 'Range Not Satisfiable', fileResponse.headers.get('Content-Range') ?? undefined);
|
|
50
73
|
}
|
|
51
|
-
|
|
74
|
+
if (!fileResponse.ok) {
|
|
75
|
+
await cancelResponseBody(fileResponse);
|
|
76
|
+
req.payload.logger.error(`Failed to fetch file from storage: ${fileResponse.status}`);
|
|
77
|
+
return mediaErrorResponse(500, 'Internal Server Error');
|
|
78
|
+
}
|
|
79
|
+
if (!hasSupportedContentEncoding(fileResponse.headers)) {
|
|
80
|
+
await cancelResponseBody(fileResponse);
|
|
81
|
+
req.payload.logger.error('Unsupported Content-Encoding from storage');
|
|
82
|
+
return mediaErrorResponse(502, 'Bad Gateway');
|
|
83
|
+
}
|
|
84
|
+
const responseHeaders = isClientUpload ? buildClientUploadFetchBackHeaders({
|
|
85
|
+
filename,
|
|
86
|
+
upstreamHeaders: fileResponse.headers
|
|
87
|
+
}) : buildMediaResponseHeaders({
|
|
88
|
+
filename,
|
|
89
|
+
upstreamHeaders: fileResponse.headers
|
|
90
|
+
});
|
|
91
|
+
return new Response(fileResponse.body, {
|
|
92
|
+
headers: responseHeaders,
|
|
93
|
+
status: fileResponse.status,
|
|
94
|
+
statusText: fileResponse.statusText
|
|
95
|
+
});
|
|
52
96
|
} catch (err) {
|
|
53
97
|
req.payload.logger.error({
|
|
54
98
|
err,
|
|
@@ -60,3 +104,13 @@ export const getHandler = ({ client, collection })=>{
|
|
|
60
104
|
}
|
|
61
105
|
};
|
|
62
106
|
};
|
|
107
|
+
function isRunningInLambda() {
|
|
108
|
+
return process.env.AWS_EXECUTION_ENV?.startsWith('AWS_Lambda_') === true;
|
|
109
|
+
}
|
|
110
|
+
async function cancelResponseBody(response) {
|
|
111
|
+
try {
|
|
112
|
+
await response.body?.cancel();
|
|
113
|
+
} catch {
|
|
114
|
+
// The response will not be consumed after an error; cancellation is best-effort cleanup.
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { StorageClient } from './client.js';
|
|
2
|
+
/** Uploads file bytes to `fileKey`, with a multipart upload for large files. */
|
|
3
|
+
export declare function uploadFileData({ client, collectionId, fileData, fileKey, fileSize, mimeType, }: {
|
|
4
|
+
client: StorageClient;
|
|
5
|
+
collectionId: string;
|
|
6
|
+
fileData: Buffer;
|
|
7
|
+
fileKey: string;
|
|
8
|
+
fileSize?: number;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
}): Promise<void>;
|