Attachment downloads: Use filename if we have it, ignore index = 1

Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
automated-signal 2022-07-11 10:40:42 -07:00 committed by GitHub
parent 9673340a30
commit ca489f8b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View File

@ -62,7 +62,25 @@ describe('Attachment', () => {
});
});
context('for attachment with index', () => {
it('should generate a filename based on timestamp', () => {
it('should use filename if provided', () => {
const attachment: Attachment.AttachmentType = fakeAttachment({
fileName: 'funny-cat.mov',
data: Bytes.fromString('foo'),
contentType: MIME.VIDEO_QUICKTIME,
});
const timestamp = new Date(
DAY + new Date(DAY).getTimezoneOffset() * 60 * 1000
);
const actual = Attachment.getSuggestedFilename({
attachment,
timestamp,
index: 3,
});
const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected);
});
it('should use provided index if > 1 and filename not provided', () => {
const attachment: Attachment.AttachmentType = fakeAttachment({
data: Bytes.fromString('foo'),
contentType: MIME.VIDEO_QUICKTIME,
@ -78,6 +96,23 @@ describe('Attachment', () => {
const expected = 'signal-1970-01-02-000000_003.mov';
assert.strictEqual(actual, expected);
});
it('should not use provided index == 1 if filename not provided', () => {
const attachment: Attachment.AttachmentType = fakeAttachment({
data: Bytes.fromString('foo'),
contentType: MIME.VIDEO_QUICKTIME,
});
const timestamp = new Date(
DAY + new Date(DAY).getTimezoneOffset() * 60 * 1000
);
const actual = Attachment.getSuggestedFilename({
attachment,
timestamp,
index: 1,
});
const expected = 'signal-1970-01-02-000000.mov';
assert.strictEqual(actual, expected);
});
});
});

View File

@ -996,8 +996,9 @@ export const getSuggestedFilename = ({
timestamp?: number | Date;
index?: number;
}): string => {
if (!isNumber(index) && attachment.fileName) {
return attachment.fileName;
const { fileName } = attachment;
if (fileName) {
return fileName;
}
const prefix = 'signal';
@ -1006,7 +1007,10 @@ export const getSuggestedFilename = ({
: '';
const fileType = getFileExtension(attachment);
const extension = fileType ? `.${fileType}` : '';
const indexSuffix = index ? `_${padStart(index.toString(), 3, '0')}` : '';
const indexSuffix =
isNumber(index) && index > 1
? `_${padStart(index.toString(), 3, '0')}`
: '';
return `${prefix}${suffix}${indexSuffix}${extension}`;
};