diff --git a/packages/payload/src/collections/operations/local/create.ts b/packages/payload/src/collections/operations/local/create.ts index 1862bdfe132..c158f08a739 100644 --- a/packages/payload/src/collections/operations/local/create.ts +++ b/packages/payload/src/collections/operations/local/create.ts @@ -217,9 +217,10 @@ export async function createLocal< const req = await createLocalReq(options as CreateLocalReqOptions, payload) + const __originalReqFile = req.file req.file = file ?? (await getFileByPath(filePath!)) - return createOperation({ + const operationResult = await createOperation({ collection, data: deepCopyObjectSimple(data), // Ensure mutation of data in create operation hooks doesn't affect the original data depth, @@ -235,4 +236,9 @@ export async function createLocal< select, showHiddenFields, }) + // Restore original request file if it existed before this operation + if (typeof __originalReqFile !== 'undefined') { + req.file = __originalReqFile + } + return operationResult } diff --git a/packages/payload/src/collections/operations/local/update.ts b/packages/payload/src/collections/operations/local/update.ts index abfc1db555e..abdbbbf77c6 100644 --- a/packages/payload/src/collections/operations/local/update.ts +++ b/packages/payload/src/collections/operations/local/update.ts @@ -250,6 +250,9 @@ async function updateLocal< } const req = await createLocalReq(options as CreateLocalReqOptions, payload) + + // Preserve any existing file on the request (e.g., from outer hook) before overwriting + const __originalReqFile = req.file req.file = file ?? (await getFileByPath(filePath!)) const args = { @@ -276,12 +279,21 @@ async function updateLocal< where, } + let operationResult if (options.id) { // @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve - return updateByIDOperation(args) + operationResult = await updateByIDOperation(args) + } else { + // @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve + operationResult = await updateOperation(args) } - // @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve - return updateOperation(args) + + // Restore original request file if it existed before this operation + if (typeof __originalReqFile !== 'undefined') { + req.file = __originalReqFile + } + + return operationResult } export { updateLocal } diff --git a/test/uploads/seed.ts b/test/uploads/seed.ts index a4ca6418c31..1786422b0bd 100644 --- a/test/uploads/seed.ts +++ b/test/uploads/seed.ts @@ -1,5 +1,7 @@ import type { CollectionSlug, Payload, RequiredDataFromCollectionSlug } from 'payload' +type File = any + import path from 'path' import { getFileByPath } from 'payload' import { fileURLToPath } from 'url'