Skip to content

Commit

Permalink
Attachment downloads: Use filename if we have it, ignore index = 1
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Nonnenberg <scott@signal.org>
  • Loading branch information
automated-signal and scottnonnenberg-signal committed Jul 11, 2022
1 parent 9673340 commit ca489f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
37 changes: 36 additions & 1 deletion ts/test-node/types/Attachment_test.ts
Expand Up @@ -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,
Expand All @@ -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);
});
});
});

Expand Down
10 changes: 7 additions & 3 deletions ts/types/Attachment.ts
Expand Up @@ -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';
Expand All @@ -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}`;
};
Expand Down

0 comments on commit ca489f8

Please sign in to comment.