Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/payload/src/collections/operations/local/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSlug, TSelect>({
const operationResult = await createOperation<TSlug, TSelect>({
collection,
data: deepCopyObjectSimple(data), // Ensure mutation of data in create operation hooks doesn't affect the original data
depth,
Expand All @@ -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
}
18 changes: 15 additions & 3 deletions packages/payload/src/collections/operations/local/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<TSlug, TSelect>(args)
operationResult = await updateByIDOperation<TSlug, TSelect>(args)
} else {
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
operationResult = await updateOperation<TSlug, TSelect>(args)
}
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
return updateOperation<TSlug, TSelect>(args)

// Restore original request file if it existed before this operation
if (typeof __originalReqFile !== 'undefined') {
req.file = __originalReqFile
}

return operationResult
}

export { updateLocal }
2 changes: 2 additions & 0 deletions test/uploads/seed.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Loading