Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Android - Error attempting to download an attachment #25742

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the list operator [] for single characters in Regex, as it's more concise and doesn't require as many escape sequences:

// Instead of:
ILLEGAL_FILENAME_CHARACTERS: /\/|<|>|\*|"|:|\?|\\|\|/g,

// Use:
ILLEGAL_FILENAME_CHARACTERS: /[\/<>*":?\\|]/g,

This makes the code cleaner and aligns with common practices for handling individual characters.

},

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, ' ');
robertjchen marked this conversation as resolved.
Show resolved Hide resolved
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, ' '));
});
});
});
Loading