From 784e5c17db2ebeaded6c4588663fb8bf7dfbbb7c Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:42:56 +0000 Subject: [PATCH] fix(nc-gui): fix duplicate filename suffix issue --- packages/nc-gui/utils/fileUtils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/nc-gui/utils/fileUtils.ts b/packages/nc-gui/utils/fileUtils.ts index c7a6a1a913..da3bdf45f9 100644 --- a/packages/nc-gui/utils/fileUtils.ts +++ b/packages/nc-gui/utils/fileUtils.ts @@ -87,8 +87,11 @@ export function populateUniqueFileName(fn: string, attachments: any[], mimeType: } let c = 1 + let originalFn = fn + while (attachments.some((att) => att?.title === fn || att?.fileName === fn)) { - fn = fn.replace(/(.+?)(\.[^.]+)$/, `$1(${c++})$2`) + const match = fn.match(/^(.+?)(\(\d+\))?(\.[^.]+)$/) + fn = match ? `${match[1]}(${c++})${match[3]}` : `${originalFn}(${c++})` } return fn }