diff --git a/packages/nocodb-sdk/src/lib/helperFunctions.ts b/packages/nocodb-sdk/src/lib/helperFunctions.ts index 9831c4f9a6..a079d23edb 100644 --- a/packages/nocodb-sdk/src/lib/helperFunctions.ts +++ b/packages/nocodb-sdk/src/lib/helperFunctions.ts @@ -104,34 +104,31 @@ const getAvailableRollupForUiType = (type: string) => { } }; +const getFileName = ({ + name, + count, + ext +}) => `${name}${count ? `(${count})` : ''}${ext? `${ext}` : ''}` + +// add count before extension if duplicate name found function populateUniqueFileName( fileName: string, - attachments: string[], - mimeType: string + attachments: string[] ) { - if (!mimeType) return fileName; - - // If the file extension is not present, the while loop will go into an infinite loop. So, add the extension first if not present. - if (!fileName?.endsWith(`.${mimeType.split('/')[1]}`)) { - fileName = `${fileName}.${mimeType.split('/')[1]}`; - } else if ( - fileName?.endsWith(`.${mimeType.split('/')[1]}`) && - fileName.length === `.${mimeType.split('/')[1]}`.length - ) { - fileName = `image.${mimeType.split('/')[1]}`; - } - - const match = fileName.match(/^(.+?)(\((\d+)\))?(\.[^.]+)$/); - - if (!match) return fileName; - - let c = !isNaN(parseInt(match[3])) ? parseInt(match[3]) : 1; - - while (attachments.some((fn) => fn === fileName)) { - fileName = `${match[1]}(${c++})${match[4]}`; - } - - return fileName; + return fileName.replace(/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/, (fileName,name, count, ext) =>{ + let genFileName = fileName; + let c = count || 1; + + // iterate until a unique name + while (attachments.some((fn) => fn === genFileName)) { + genFileName = getFileName({ + name, + ext, + count: c++ + }); + } + return genFileName; + }); } export { diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index 0b20a3b8cd..b9f8845b3a 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -16514,7 +16514,7 @@ } ] }, - "put": { + "patch": { "summary": "Update Table Rows", "operationId": "db-data-table-row-update", "responses": { diff --git a/packages/nocodb/src/services/attachments.service.ts b/packages/nocodb/src/services/attachments.service.ts index 8f4d7fd58e..197c7f3d74 100644 --- a/packages/nocodb/src/services/attachments.service.ts +++ b/packages/nocodb/src/services/attachments.service.ts @@ -43,7 +43,9 @@ export class AttachmentsService { param.files?.map((file) => async () => { try { const originalName = utf8ify(file.originalname); - const fileName = `${nanoid(18)}${path.extname(originalName)}`; + const fileName = `${path.parse(originalName).name}_${nanoid( + 5, + )}${path.extname(originalName)}`; const url = await storageAdapter.fileCreate( slash(path.join(destPath, fileName)), @@ -140,10 +142,11 @@ export class AttachmentsService { param.urls?.map?.((urlMeta) => async () => { try { const { url, fileName: _fileName } = urlMeta; + const fileNameWithExt = _fileName || url.split('/').pop(); - const fileName = `${nanoid(18)}${path.extname( - _fileName || url.split('/').pop(), - )}`; + const fileName = `${path.parse(fileNameWithExt).name}_${nanoid( + 5, + )}${path.extname(fileNameWithExt)}`; const attachmentUrl: string | null = await storageAdapter.fileCreateByUrl( diff --git a/packages/nocodb/src/services/public-datas.service.ts b/packages/nocodb/src/services/public-datas.service.ts index d1af9579d9..06a7366343 100644 --- a/packages/nocodb/src/services/public-datas.service.ts +++ b/packages/nocodb/src/services/public-datas.service.ts @@ -326,7 +326,6 @@ export class PublicDatasService { originalName = populateUniqueFileName( originalName, attachments[fieldName].map((att) => att?.title), - file.mimetype, ); const fileName = `${nanoid(18)}${path.extname(originalName)}`;