@payloadcms/figma 0.1.0-alpha.11 → 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 +2 -0
- package/dist/api/control-plane.js +1 -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/types.d.ts +5 -3
- package/dist/commands/bootstrap.js +3 -0
- 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/plugin/build-config.js +76 -16
- package/dist/storage-content-api/api-types.d.ts +2 -2
- package/dist/storage-content-api/buildClientUploadFetchBackHeaders.d.ts +1 -1
- 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/handleUpload.d.ts +1 -2
- package/dist/storage-content-api/handleUpload.js +13 -157
- package/dist/storage-content-api/index.js +16 -8
- package/dist/storage-content-api/staticHandler.d.ts +2 -1
- package/dist/storage-content-api/staticHandler.js +14 -27
- 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/load-payload-config.js +1 -6
- package/dist/utils/payload-config-ast.d.ts +5 -0
- package/dist/utils/payload-config-ast.js +68 -2
- package/dist/utils/payload-config-modifier.js +3 -0
- package/package.json +13 -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,90 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
import { uploadFileData } from './uploadFileData.js';
|
|
3
|
+
const PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS = 300;
|
|
4
|
+
/**
|
|
5
|
+
* Copies `from` to `to` and keeps `from`, so older versions can still read it.
|
|
6
|
+
*
|
|
7
|
+
* The Content API has no copy endpoint, so the bytes are downloaded and uploaded again with
|
|
8
|
+
* the source Content-Type. The Content API stores no other object metadata.
|
|
9
|
+
*
|
|
10
|
+
* The destination check is not atomic. Payload puts a new `_objectKey` in `to`, so two
|
|
11
|
+
* copies to the same key at the same time are not expected.
|
|
12
|
+
*/ export const getCopyFile = ({ client })=>{
|
|
13
|
+
return async ({ collection, from, mimeType, req, to })=>{
|
|
14
|
+
if (from === to) {
|
|
15
|
+
throw new Error('Storage copy requires different source and destination keys');
|
|
16
|
+
}
|
|
17
|
+
const collectionId = collection.slug;
|
|
18
|
+
// The Content API role can list the bucket, so a missing object returns 404, not 403.
|
|
19
|
+
const destination = await fetchObject({
|
|
20
|
+
client,
|
|
21
|
+
collectionId,
|
|
22
|
+
headers: {
|
|
23
|
+
Range: 'bytes=0-0'
|
|
24
|
+
},
|
|
25
|
+
key: to,
|
|
26
|
+
signal: req.signal
|
|
27
|
+
});
|
|
28
|
+
await cancelResponseBody(destination);
|
|
29
|
+
if (destination.status !== 404) {
|
|
30
|
+
if (destination.ok || destination.status === 416) {
|
|
31
|
+
throw new APIError(`Storage destination already exists: ${to}`, 409);
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Failed to check storage destination: ${destination.status}`);
|
|
34
|
+
}
|
|
35
|
+
const source = await fetchObject({
|
|
36
|
+
client,
|
|
37
|
+
collectionId,
|
|
38
|
+
key: from,
|
|
39
|
+
signal: req.signal
|
|
40
|
+
});
|
|
41
|
+
if (!source.ok) {
|
|
42
|
+
await cancelResponseBody(source);
|
|
43
|
+
throw new Error(`Failed to read storage source: ${source.status}`);
|
|
44
|
+
}
|
|
45
|
+
const fileData = Buffer.from(await source.arrayBuffer());
|
|
46
|
+
await uploadFileData({
|
|
47
|
+
client,
|
|
48
|
+
collectionId,
|
|
49
|
+
fileData,
|
|
50
|
+
fileKey: to,
|
|
51
|
+
mimeType: source.headers.get('Content-Type') || mimeType || 'application/octet-stream'
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
async function fetchObject({ client, collectionId, headers, key, signal }) {
|
|
56
|
+
const { data, error, response } = await client.POST('/api/v0/uploads/download/sign', {
|
|
57
|
+
body: {
|
|
58
|
+
collectionId,
|
|
59
|
+
expiresIn: PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS,
|
|
60
|
+
filename: key,
|
|
61
|
+
responseHeaders: {
|
|
62
|
+
cacheControl: 'private, no-store'
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
if (error) {
|
|
67
|
+
throw new APIError(`Failed to get presigned download URL: ${error.message}`, response.status, {
|
|
68
|
+
code: error.code
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (!data) {
|
|
72
|
+
throw new Error('No response while getting presigned download URL');
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
return await fetch(data.url, {
|
|
76
|
+
headers,
|
|
77
|
+
signal
|
|
78
|
+
});
|
|
79
|
+
} catch {
|
|
80
|
+
// Fetch errors can contain the signed URL; throw only a fixed message to avoid exposing it.
|
|
81
|
+
throw new Error('Failed to fetch file from storage');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function cancelResponseBody(response) {
|
|
85
|
+
try {
|
|
86
|
+
await response.body?.cancel();
|
|
87
|
+
} catch {
|
|
88
|
+
// The body is not read; cancellation is best-effort cleanup.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -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
|
+
};
|
|
@@ -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
|
-
}
|
|
@@ -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
|
};
|
|
@@ -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,6 +1,4 @@
|
|
|
1
|
-
import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { isImage } from 'payload/shared';
|
|
1
|
+
import { buildStoragePathData, getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
4
2
|
import { buildClientUploadFetchBackHeaders } from './buildClientUploadFetchBackHeaders.js';
|
|
5
3
|
import { buildMediaRequestHeaders } from './buildMediaRequestHeaders.js';
|
|
6
4
|
import { buildMediaResponseHeaders } from './buildMediaResponseHeaders.js';
|
|
@@ -8,18 +6,24 @@ import { getDownloadResponseHeaderPolicy } from './getDownloadResponseHeaderPoli
|
|
|
8
6
|
import { hasSupportedContentEncoding } from './hasSupportedContentEncoding.js';
|
|
9
7
|
import { mediaErrorResponse } from './mediaErrorResponse.js';
|
|
10
8
|
import { resolveDownloadResponseHeaders } from './resolveDownloadResponseHeaders.js';
|
|
11
|
-
import { parseUploadReference } from './utilities/index.js';
|
|
12
9
|
const PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS = 300;
|
|
13
|
-
export const getHandler = ({ client, collection })=>{
|
|
14
|
-
return async (req, { headers, params: { filename, uploadReference } })=>{
|
|
10
|
+
export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
11
|
+
return async (req, { doc, headers, params: { filename, operation, uploadReference } })=>{
|
|
15
12
|
try {
|
|
16
|
-
const
|
|
13
|
+
const prefixArgs = {
|
|
17
14
|
collection,
|
|
15
|
+
collectionPrefix: prefix,
|
|
16
|
+
doc,
|
|
18
17
|
filename,
|
|
19
18
|
req,
|
|
20
19
|
uploadReference
|
|
20
|
+
};
|
|
21
|
+
const docPrefix = await getFilePrefix(prefixArgs);
|
|
22
|
+
const { storageFilePath: key } = buildStoragePathData({
|
|
23
|
+
collectionPrefix: prefix,
|
|
24
|
+
docPrefix,
|
|
25
|
+
filename
|
|
21
26
|
});
|
|
22
|
-
const key = path.posix.join(prefix, filename);
|
|
23
27
|
const resolvedResponseHeaders = resolveDownloadResponseHeaders({
|
|
24
28
|
collection,
|
|
25
29
|
payloadResponseHeaders: headers
|
|
@@ -43,13 +47,9 @@ export const getHandler = ({ client, collection })=>{
|
|
|
43
47
|
});
|
|
44
48
|
}
|
|
45
49
|
const isClientUpload = Boolean(uploadReference);
|
|
46
|
-
const clientUploadMimeType = isClientUpload ? parseUploadReference(uploadReference).mimeType : undefined;
|
|
47
50
|
// Lambda responses have a 6 MB limit, so redirect normal media to its signed storage URL.
|
|
48
|
-
//
|
|
49
|
-
if (
|
|
50
|
-
clientUploadMimeType,
|
|
51
|
-
isClientUpload
|
|
52
|
-
})) {
|
|
51
|
+
// Payload reads client uploads and transform sources itself, so they need a response body.
|
|
52
|
+
if (isRunningInLambda() && !isClientUpload && operation !== 'transform') {
|
|
53
53
|
return Response.redirect(data.url, 302);
|
|
54
54
|
}
|
|
55
55
|
const requestHeaders = buildMediaRequestHeaders(req.headers);
|
|
@@ -82,7 +82,6 @@ export const getHandler = ({ client, collection })=>{
|
|
|
82
82
|
return mediaErrorResponse(502, 'Bad Gateway');
|
|
83
83
|
}
|
|
84
84
|
const responseHeaders = isClientUpload ? buildClientUploadFetchBackHeaders({
|
|
85
|
-
fallbackContentType: clientUploadMimeType,
|
|
86
85
|
filename,
|
|
87
86
|
upstreamHeaders: fileResponse.headers
|
|
88
87
|
}) : buildMediaResponseHeaders({
|
|
@@ -108,18 +107,6 @@ export const getHandler = ({ client, collection })=>{
|
|
|
108
107
|
function isRunningInLambda() {
|
|
109
108
|
return process.env.AWS_EXECUTION_ENV?.startsWith('AWS_Lambda_') === true;
|
|
110
109
|
}
|
|
111
|
-
// Stream all files outside Lambda. In Lambda, redirect normal requests and non-image uploads.
|
|
112
|
-
// Return image uploads directly so Payload can inspect their dimensions; do the same when
|
|
113
|
-
// the upload type is unknown, since it might be an image.
|
|
114
|
-
function shouldReturnRedirect({ clientUploadMimeType, isClientUpload }) {
|
|
115
|
-
if (!isRunningInLambda()) {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
if (!isClientUpload) {
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
return Boolean(clientUploadMimeType && !isImage(clientUploadMimeType));
|
|
122
|
-
}
|
|
123
110
|
async function cancelResponseBody(response) {
|
|
124
111
|
try {
|
|
125
112
|
await response.body?.cancel();
|
|
@@ -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>;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
const PART_SIZE = 64 * 1024 * 1024 // 64MB per part
|
|
3
|
+
;
|
|
4
|
+
const MAX_FILE_SIZE_BEFORE_MULTIPART = 100 * 1024 * 1024 // 100MB
|
|
5
|
+
;
|
|
6
|
+
/** Uploads file bytes to `fileKey`, with a multipart upload for large files. */ export async function uploadFileData({ client, collectionId, fileData, fileKey, fileSize = fileData.length, mimeType }) {
|
|
7
|
+
if (fileSize > MAX_FILE_SIZE_BEFORE_MULTIPART) {
|
|
8
|
+
await uploadMultipart({
|
|
9
|
+
client,
|
|
10
|
+
collectionId,
|
|
11
|
+
fileData,
|
|
12
|
+
fileKey,
|
|
13
|
+
mimeType
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
await uploadSingleFile({
|
|
17
|
+
client,
|
|
18
|
+
collectionId,
|
|
19
|
+
fileData,
|
|
20
|
+
fileKey,
|
|
21
|
+
mimeType
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function uploadSingleFile({ client, collectionId, fileData, fileKey, mimeType }) {
|
|
26
|
+
const { data, error, response } = await client.POST('/api/v0/uploads/sign', {
|
|
27
|
+
body: {
|
|
28
|
+
collectionId,
|
|
29
|
+
contentType: mimeType,
|
|
30
|
+
expiresIn: 3600,
|
|
31
|
+
filename: fileKey
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
if (error) {
|
|
35
|
+
throw new APIError(`Failed to get presigned upload URL: ${error.message}`, response.status, {
|
|
36
|
+
code: error.code
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (!data) {
|
|
40
|
+
throw new Error('No response while getting presigned upload URL');
|
|
41
|
+
}
|
|
42
|
+
const uploadResponse = await fetch(data.url, {
|
|
43
|
+
// TS's BodyInit type rejects Buffer<ArrayBufferLike>; wrap as Uint8Array
|
|
44
|
+
// (a valid ArrayBufferView) — byte-identical.
|
|
45
|
+
body: new Uint8Array(fileData),
|
|
46
|
+
headers: {
|
|
47
|
+
'Content-Type': mimeType
|
|
48
|
+
},
|
|
49
|
+
method: 'PUT'
|
|
50
|
+
});
|
|
51
|
+
if (!uploadResponse.ok) {
|
|
52
|
+
throw new Error(`Failed to upload to S3: ${uploadResponse.status} ${uploadResponse.statusText}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function uploadMultipart({ client, collectionId, fileData, fileKey, mimeType }) {
|
|
56
|
+
const { data: initiateData, error: initiateError, response: initiateResponse } = await client.POST('/api/v0/uploads/multipart/initiate', {
|
|
57
|
+
body: {
|
|
58
|
+
collectionId,
|
|
59
|
+
contentType: mimeType,
|
|
60
|
+
filename: fileKey
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
if (initiateError) {
|
|
64
|
+
throw new APIError(`Failed to initiate multipart upload: ${initiateError.message}`, initiateResponse.status, {
|
|
65
|
+
code: initiateError.code
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (!initiateData) {
|
|
69
|
+
throw new Error('No response while initiating multipart upload');
|
|
70
|
+
}
|
|
71
|
+
const { uploadId } = initiateData;
|
|
72
|
+
try {
|
|
73
|
+
const numParts = Math.ceil(fileData.length / PART_SIZE);
|
|
74
|
+
const partNumbers = Array.from({
|
|
75
|
+
length: numParts
|
|
76
|
+
}, (_, i)=>i + 1);
|
|
77
|
+
const { data: partsData, error: partsError, response: partsResponse } = await client.POST('/api/v0/uploads/multipart/parts', {
|
|
78
|
+
body: {
|
|
79
|
+
collectionId,
|
|
80
|
+
expiresIn: 3600,
|
|
81
|
+
filename: fileKey,
|
|
82
|
+
partNumbers,
|
|
83
|
+
uploadId
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
if (partsError) {
|
|
87
|
+
throw new APIError(`Failed to get part URLs: ${partsError.message}`, partsResponse.status, {
|
|
88
|
+
code: partsError.code
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (!partsData) {
|
|
92
|
+
throw new Error('No response while getting part URLs');
|
|
93
|
+
}
|
|
94
|
+
const { parts } = partsData;
|
|
95
|
+
if (!parts || !Array.isArray(parts)) {
|
|
96
|
+
throw new Error('Invalid response from part URLs endpoint: parts is not an array');
|
|
97
|
+
}
|
|
98
|
+
const uploadedParts = await Promise.all(parts.map(async ({ partNumber, url })=>{
|
|
99
|
+
const start = (partNumber - 1) * PART_SIZE;
|
|
100
|
+
const end = Math.min(start + PART_SIZE, fileData.length);
|
|
101
|
+
const partData = fileData.slice(start, end);
|
|
102
|
+
const uploadResponse = await fetch(url, {
|
|
103
|
+
body: partData,
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': mimeType
|
|
106
|
+
},
|
|
107
|
+
method: 'PUT'
|
|
108
|
+
});
|
|
109
|
+
const etag = uploadResponse.headers.get('ETag');
|
|
110
|
+
if (!etag) {
|
|
111
|
+
throw new Error(`No ETag returned for part ${partNumber}`);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
ETag: etag,
|
|
115
|
+
PartNumber: partNumber
|
|
116
|
+
};
|
|
117
|
+
}));
|
|
118
|
+
const { error: completeError, response: completeResponse } = await client.POST('/api/v0/uploads/multipart/complete', {
|
|
119
|
+
body: {
|
|
120
|
+
collectionId,
|
|
121
|
+
filename: fileKey,
|
|
122
|
+
parts: uploadedParts,
|
|
123
|
+
uploadId
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
if (completeError) {
|
|
127
|
+
throw new APIError(`Failed to complete multipart upload: ${completeError.message}`, completeResponse.status, {
|
|
128
|
+
code: completeError.code
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
try {
|
|
133
|
+
await client.POST('/api/v0/uploads/multipart/abort', {
|
|
134
|
+
body: {
|
|
135
|
+
collectionId,
|
|
136
|
+
filename: fileKey,
|
|
137
|
+
uploadId
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
} catch {
|
|
141
|
+
// Ignore abort errors
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -6,6 +6,7 @@ import * as log from './log.js';
|
|
|
6
6
|
const { oauthCredentials } = bootstrapInfo;
|
|
7
7
|
for (const env of bootstrapInfo.environments){
|
|
8
8
|
tokenStore.setBootstrapData(projectId, env.name, {
|
|
9
|
+
contentApiUrl: env.contentApiUrl,
|
|
9
10
|
contentSystemId: env.contentSystemId,
|
|
10
11
|
oauthClientId: oauthCredentials.oauthClientId,
|
|
11
12
|
oauthClientSecret: oauthCredentials.oauthClientSecret,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
1
|
import path from 'node:path';
|
|
3
2
|
import { pathToFileURL } from 'node:url';
|
|
4
3
|
import { findPayloadConfig } from './payload-config-finder.js';
|
|
@@ -16,11 +15,7 @@ import { findPayloadConfig } from './payload-config-finder.js';
|
|
|
16
15
|
const absoluteConfigPath = path.resolve(projectPath, configPath);
|
|
17
16
|
const { loadEnv } = await import('payload/node');
|
|
18
17
|
loadEnv();
|
|
19
|
-
const
|
|
20
|
-
const payloadEntryPath = requireFromProject.resolve('payload');
|
|
21
|
-
const requireFromPayload = createRequire(payloadEntryPath);
|
|
22
|
-
const tsxApiPath = requireFromPayload.resolve('tsx/esm/api');
|
|
23
|
-
const { tsImport } = await import(pathToFileURL(tsxApiPath).href);
|
|
18
|
+
const tsImport = (await import('tsx/esm/api')).tsImport;
|
|
24
19
|
const parentURL = pathToFileURL(`${projectPath}${path.sep}`).href;
|
|
25
20
|
const imported = await tsImport(absoluteConfigPath, parentURL);
|
|
26
21
|
return await (imported.default ?? imported);
|
|
@@ -34,6 +34,11 @@ export type DetectionResult = {
|
|
|
34
34
|
/** NPM package sharp is imported from (always 'sharp') */
|
|
35
35
|
importSource: string;
|
|
36
36
|
};
|
|
37
|
+
/** Payload v4 Sharp upload transformer (`upload.transformers: [sharpTransformer()]`) if present */
|
|
38
|
+
sharpTransformer?: {
|
|
39
|
+
/** NPM package the transformer is imported from (always '@payloadcms/transformer-sharp') */
|
|
40
|
+
importSource: string;
|
|
41
|
+
};
|
|
37
42
|
};
|
|
38
43
|
/**
|
|
39
44
|
* Detect what changes are needed in the payload config file
|