diff --git a/app/utils/file.js b/app/utils/file.js index f984779c4..77b5998be 100644 --- a/app/utils/file.js +++ b/app/utils/file.js @@ -283,7 +283,24 @@ export const hashCode = (str) => { export function getLocalFilePathFromFile(dir, file) { if (dir) { if (file?.name) { - const [filename, extension] = file.name.split('.'); + let extension = file.extension; + let filename = file.name; + + if (!extension) { + extension = getExtensionFromMime(file.mime_type); + } + + if (extension && filename.includes(`.${extension}`)) { + filename = filename.replace(`.${extension}`, ''); + } else { + const fileParts = file.name.split('.'); + + if (fileParts.length > 1) { + extension = fileParts.pop(); + filename = fileParts.join('.'); + } + } + return `${dir}/${filename}-${hashCode(file.id)}.${extension}`; } else if (file?.id && file?.extension) { return `${dir}/${file.id}.${file.extension}`; diff --git a/app/utils/file.test.js b/app/utils/file.test.js index 53c7d0f85..ff3294fe8 100644 --- a/app/utils/file.test.js +++ b/app/utils/file.test.js @@ -118,7 +118,7 @@ describe('getExtensionFromContentDisposition', () => { it('should return the path for the document file', () => { const file = { id: generateId(), - name: 'Some other doocument.txt', + name: 'Some other document.txt', extension: 'txt', }; @@ -126,4 +126,15 @@ describe('getExtensionFromContentDisposition', () => { const [filename] = file.name.split('.'); expect(localFile).toBe(`${DeviceTypes.DOCUMENTS_PATH}/${filename}-${hashCode(file.id)}.${file.extension}`); }); + + it('should return the path for the document file including multiple dots in the filename', () => { + const file = { + id: generateId(), + name: 'Some.other.document.txt', + extension: 'txt', + }; + const expectedFilename = 'Some.other.document'; + const localFile = getLocalPath(file); + expect(localFile).toBe(`${DeviceTypes.DOCUMENTS_PATH}/${expectedFilename}-${hashCode(file.id)}.${file.extension}`); + }); });