From dbbff4cfa41aa20077e47c8c7b87d4d00683c571 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 22 Sep 2021 12:05:09 -0400 Subject: [PATCH] feat: allows local update api to replace existing files with newly uploaded ones --- docs/local-api/overview.mdx | 5 +++++ src/collections/operations/local/update.ts | 3 +++ src/collections/operations/update.ts | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/local-api/overview.mdx b/docs/local-api/overview.mdx index 49e008948..fefb3600d 100644 --- a/docs/local-api/overview.mdx +++ b/docs/local-api/overview.mdx @@ -162,6 +162,11 @@ const result = await payload.update({ // a file directly through the Local API by providing // its full, absolute file path. filePath: path.resolve(__dirname, './path-to-image.jpg'), + + // If you are uploading a file and would like to replace + // the existing file instead of generating a new filename, + // you can set the following property to `true` + overwriteExistingFiles: true, }) ``` diff --git a/src/collections/operations/local/update.ts b/src/collections/operations/local/update.ts index 0fca85402..c7823b2e2 100644 --- a/src/collections/operations/local/update.ts +++ b/src/collections/operations/local/update.ts @@ -12,6 +12,7 @@ export type Options = { overrideAccess?: boolean showHiddenFields?: boolean filePath?: string + overwriteExistingFiles?: boolean } export default async function update(options: Options): Promise { @@ -26,6 +27,7 @@ export default async function update(options: Options): Promise { overrideAccess = true, showHiddenFields, filePath, + overwriteExistingFiles = false, } = options; const collection = this.collections[collectionSlug]; @@ -37,6 +39,7 @@ export default async function update(options: Options): Promise { overrideAccess, id, showHiddenFields, + overwriteExistingFiles, req: { user, payloadAPI: 'local', diff --git a/src/collections/operations/update.ts b/src/collections/operations/update.ts index 2b579f49e..0afe26b97 100644 --- a/src/collections/operations/update.ts +++ b/src/collections/operations/update.ts @@ -27,6 +27,7 @@ export type Arguments = { disableVerificationEmail?: boolean overrideAccess?: boolean showHiddenFields?: boolean + overwriteExistingFiles?: boolean } async function update(incomingArgs: Arguments): Promise { @@ -60,6 +61,7 @@ async function update(incomingArgs: Arguments): Promise { }, overrideAccess, showHiddenFields, + overwriteExistingFiles = false, } = args; if (!id) { @@ -135,7 +137,7 @@ async function update(incomingArgs: Arguments): Promise { const file = ((req.files && req.files.file) ? req.files.file : req.file) as UploadedFile; if (file) { - const fsSafeName = await getSafeFilename(staticPath, file.name); + const fsSafeName = overwriteExistingFiles ? await getSafeFilename(staticPath, file.name) : file.name; try { if (!disableLocalStorage) {