Skip to content

Commit

Permalink
Merge pull request #25742 from dukenv0307/fix/25491-android-error-att…
Browse files Browse the repository at this point in the history
…empting-to-download-an-attachment

Fix: Android - Error attempting to download an attachment
  • Loading branch information
robertjchen authored Aug 25, 2023
2 parents fe02d63 + 27381e8 commit a906a7c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions src/libs/fileDownload/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/FileUtilsTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CONST from '../../src/CONST';
import DateUtils from '../../src/libs/DateUtils';
import * as FileUtils from '../../src/libs/fileDownload/FileUtils';

Expand Down Expand Up @@ -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, '_'));
});
});
});

0 comments on commit a906a7c

Please sign in to comment.