Skip to content

Commit 6b7ec6c

Browse files
feat: add the ability to pass in a response to upload handlers (#6926)
Adds the ability to set response headers by using a new `uploads.modifyResponseHeaders` property. You could previously do this in Express in Payload v2. You can do this like so: ```ts upload: { modifyResponseHeaders: ({ headers }) => { headers.set('Cache-Control', 'public, max-age=86400') return headers } }, ```
1 parent 35eb16b commit 6b7ec6c

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

packages/next/src/routes/rest/files/getFile.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export const getFile = async ({ collection, filename, req }: Args): Promise<Resp
3535

3636
if (accessResult instanceof Response) return accessResult
3737

38-
let response: Response = null
3938
if (collection.config.upload.handlers?.length) {
39+
let customResponse = null
4040
for (const handler of collection.config.upload.handlers) {
41-
response = await handler(req, {
41+
customResponse = await handler(req, {
4242
doc: accessResult,
4343
params: {
4444
collection: collection.config.slug,
@@ -47,21 +47,21 @@ export const getFile = async ({ collection, filename, req }: Args): Promise<Resp
4747
})
4848
}
4949

50-
if (response instanceof Response) return response
50+
if (customResponse instanceof Response) return customResponse
5151
}
5252

5353
const fileDir = collection.config.upload?.staticDir || collection.config.slug
5454
const filePath = path.resolve(`${fileDir}/${filename}`)
55-
5655
const stats = await fsPromises.stat(filePath)
57-
5856
const data = streamFile(filePath)
59-
60-
const headers = new Headers(req.headers)
61-
headers.set('Content-Length', stats.size + '')
62-
6357
const fileTypeResult = (await fileTypeFromFile(filePath)) || getFileTypeFallback(filePath)
58+
59+
let headers = new Headers()
6460
headers.set('Content-Type', fileTypeResult.mime)
61+
headers.set('Content-Length', stats.size + '')
62+
headers = collection.config.upload?.modifyResponseHeaders
63+
? collection.config.upload.modifyResponseHeaders({ headers })
64+
: headers
6565

6666
return new Response(data, {
6767
headers: headersWithCors({

packages/payload/src/collections/config/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const collectionSchema = joi.object().keys({
180180
.unknown(),
181181
),
182182
mimeTypes: joi.array().items(joi.string()),
183+
modifyResponseHeaders: joi.func(),
183184
resizeOptions: joi
184185
.object()
185186
.keys({

packages/payload/src/uploads/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,23 @@ export type UploadConfig = {
124124
*/
125125
handlers?: ((
126126
req: PayloadRequestWithData,
127-
args: { doc: TypeWithID; params: { collection: string; filename: string } },
128-
) => Promise<Response> | Response | null)[]
127+
args: {
128+
doc: TypeWithID
129+
params: { collection: string; filename: string }
130+
},
131+
) => Promise<Response> | Promise<void> | Response | void)[]
129132
imageSizes?: ImageSize[]
130133
/**
131134
* Restrict mimeTypes in the file picker. Array of valid mime types or mimetype wildcards
132135
* @example ['image/*', 'application/pdf']
133136
* @default undefined
134137
*/
135138
mimeTypes?: string[]
139+
/**
140+
* Ability to modify the response headers fetching a file.
141+
* @default undefined
142+
*/
143+
modifyResponseHeaders?: ({ headers }: { headers: Headers }) => Headers
136144
/**
137145
* Sharp resize options for the original image.
138146
* @link https://sharp.pixelplumbing.com/api-resize#resize

packages/plugin-cloud/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type GenerateURL = (args: {
4141
export type StaticHandler = (
4242
req: PayloadRequestWithData,
4343
args: { params: { collection: string; filename: string } },
44-
) => Promise<Response> | Response
44+
) => Promise<Response> | Promise<void> | Response | void
4545

4646
export interface PayloadCloudEmailOptions {
4747
apiKey: string

0 commit comments

Comments
 (0)