diff --git a/src/CONST.js b/src/CONST.js index 6df38b53cc5b..6353213204d0 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -1198,6 +1198,7 @@ const CONST = { TIME_STARTS_01: /^01:\d{2} [AP]M$/, TIME_FORMAT: /^\d{2}:\d{2} [AP]M$/, DATE_TIME_FORMAT: /^\d{2}-\d{2} \d{2}:\d{2} [AP]M$/, + ILLEGAL_FILENAME_CHARACTERS: /\/|<|>|\*|"|:|\?|\\|\|/g, }, PRONOUNS: { diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index a98a33b2934f..cee2b8877ef6 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -125,6 +125,8 @@ function cleanFileName(fileName) { function appendTimeToFileName(fileName) { const file = splitExtensionFromFileName(fileName); let newFileName = `${file.fileName}-${DateUtils.getDBTime()}`; + // Replace illegal characters before trying to download the attachment. + newFileName = newFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'); if (file.fileExtension) { newFileName += `.${file.fileExtension}`; } diff --git a/tests/unit/FileUtilsTest.js b/tests/unit/FileUtilsTest.js index 5b6ad7fe3ad3..34dc0dfcf129 100644 --- a/tests/unit/FileUtilsTest.js +++ b/tests/unit/FileUtilsTest.js @@ -1,3 +1,4 @@ +import CONST from '../../src/CONST'; import DateUtils from '../../src/libs/DateUtils'; import * as FileUtils from '../../src/libs/fileDownload/FileUtils'; @@ -26,13 +27,13 @@ describe('FileUtils', () => { it('should append current time to the end of the file name', () => { const actualFileName = FileUtils.appendTimeToFileName('image.jpg'); const expectedFileName = `image-${DateUtils.getDBTime()}.jpg`; - expect(actualFileName).toEqual(expectedFileName); + expect(actualFileName).toEqual(expectedFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_')); }); it('should append current time to the end of the file name without extension', () => { const actualFileName = FileUtils.appendTimeToFileName('image'); const expectedFileName = `image-${DateUtils.getDBTime()}`; - expect(actualFileName).toEqual(expectedFileName); + expect(actualFileName).toEqual(expectedFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_')); }); }); });