MM-31705 allow file local path to use multiple dots (#5109)

* MM-31705 allow file local path to use multiple dots

* Add unit test
This commit is contained in:
Elias Nahum 2021-01-11 16:53:06 -03:00 committed by GitHub
parent 0d590c742f
commit 2e790212f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -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}`;

View file

@ -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}`);
});
});