From be4a2d613595652c103511f309d94774a79ad4e7 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:11 +0000 Subject: [PATCH 1/6] fix: avoid appending additional `jpeg` when filename ends with `jpg` --- packages/nocodb-sdk/src/lib/helperFunctions.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/helperFunctions.ts b/packages/nocodb-sdk/src/lib/helperFunctions.ts index 9831c4f9a6..e8dc89edf5 100644 --- a/packages/nocodb-sdk/src/lib/helperFunctions.ts +++ b/packages/nocodb-sdk/src/lib/helperFunctions.ts @@ -112,13 +112,20 @@ function populateUniqueFileName( 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]}`; + let ext = mimeType.split('/')[1]; + + // avoid appending `jpeg` if extension is jpg + if(ext === 'jpeg' && fileName?.endsWith('.jpg')) { + ext = 'jpg' + } + + if (!fileName?.endsWith(`.${ext}`)) { + fileName = `${fileName}.${ext}`; } else if ( - fileName?.endsWith(`.${mimeType.split('/')[1]}`) && - fileName.length === `.${mimeType.split('/')[1]}`.length + fileName?.endsWith(`.${ext}`) && + fileName.length === `.${ext}`.length ) { - fileName = `image.${mimeType.split('/')[1]}`; + fileName = `image.${ext}`; } const match = fileName.match(/^(.+?)(\((\d+)\))?(\.[^.]+)$/); From 13ee192f856d10fe023b834fe2f7f0082f5d2322 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:11 +0000 Subject: [PATCH 2/6] fix: http method name correction --- packages/nocodb/src/schema/swagger.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": { From 36f74470c5b4e15555ddb1ddcc56789b48ef3658 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:12 +0000 Subject: [PATCH 3/6] fix: attachment unique filename --- .../nocodb-sdk/src/lib/helperFunctions.ts | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/helperFunctions.ts b/packages/nocodb-sdk/src/lib/helperFunctions.ts index e8dc89edf5..0a3bf62293 100644 --- a/packages/nocodb-sdk/src/lib/helperFunctions.ts +++ b/packages/nocodb-sdk/src/lib/helperFunctions.ts @@ -104,41 +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. - let ext = mimeType.split('/')[1]; - - // avoid appending `jpeg` if extension is jpg - if(ext === 'jpeg' && fileName?.endsWith('.jpg')) { - ext = 'jpg' - } - - if (!fileName?.endsWith(`.${ext}`)) { - fileName = `${fileName}.${ext}`; - } else if ( - fileName?.endsWith(`.${ext}`) && - fileName.length === `.${ext}`.length - ) { - fileName = `image.${ext}`; - } - - 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 { From 8a8f0d78bb0f7d0455764a56727e8d71d6b25b5d Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:12 +0000 Subject: [PATCH 4/6] feat: use actual filename with appended random string --- packages/nocodb/src/services/attachments.service.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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( From fbe1e9a209698ac96fadf3cf6c66b19648758162 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:12 +0000 Subject: [PATCH 5/6] fix: params correction --- packages/nocodb/src/services/public-datas.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/nocodb/src/services/public-datas.service.ts b/packages/nocodb/src/services/public-datas.service.ts index abee2bcb0d..f521ff1d85 100644 --- a/packages/nocodb/src/services/public-datas.service.ts +++ b/packages/nocodb/src/services/public-datas.service.ts @@ -331,7 +331,6 @@ export class PublicDatasService { originalName = populateUniqueFileName( originalName, attachments[fieldName].map((att) => att?.title), - file.mimetype, ); const fileName = `${nanoid(18)}${path.extname(originalName)}`; From fbd25b7e9df343b51610f2e074636f325c6457b4 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 20 Mar 2024 12:43:12 +0000 Subject: [PATCH 6/6] fix: avoid adding extra dot --- packages/nocodb-sdk/src/lib/helperFunctions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nocodb-sdk/src/lib/helperFunctions.ts b/packages/nocodb-sdk/src/lib/helperFunctions.ts index 0a3bf62293..a079d23edb 100644 --- a/packages/nocodb-sdk/src/lib/helperFunctions.ts +++ b/packages/nocodb-sdk/src/lib/helperFunctions.ts @@ -108,7 +108,7 @@ const getFileName = ({ name, count, ext -}) => `${name}${count ? `(${count})` : ''}${ext? `.${ext}` : ''}` +}) => `${name}${count ? `(${count})` : ''}${ext? `${ext}` : ''}` // add count before extension if duplicate name found function populateUniqueFileName(