Attachment downloads: Use filename if we have it, ignore index = 1
Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
parent
9673340a30
commit
ca489f8b98
|
@ -62,7 +62,25 @@ describe('Attachment', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
context('for attachment with index', () => {
|
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({
|
const attachment: Attachment.AttachmentType = fakeAttachment({
|
||||||
data: Bytes.fromString('foo'),
|
data: Bytes.fromString('foo'),
|
||||||
contentType: MIME.VIDEO_QUICKTIME,
|
contentType: MIME.VIDEO_QUICKTIME,
|
||||||
|
@ -78,6 +96,23 @@ describe('Attachment', () => {
|
||||||
const expected = 'signal-1970-01-02-000000_003.mov';
|
const expected = 'signal-1970-01-02-000000_003.mov';
|
||||||
assert.strictEqual(actual, expected);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -996,8 +996,9 @@ export const getSuggestedFilename = ({
|
||||||
timestamp?: number | Date;
|
timestamp?: number | Date;
|
||||||
index?: number;
|
index?: number;
|
||||||
}): string => {
|
}): string => {
|
||||||
if (!isNumber(index) && attachment.fileName) {
|
const { fileName } = attachment;
|
||||||
return attachment.fileName;
|
if (fileName) {
|
||||||
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const prefix = 'signal';
|
const prefix = 'signal';
|
||||||
|
@ -1006,7 +1007,10 @@ export const getSuggestedFilename = ({
|
||||||
: '';
|
: '';
|
||||||
const fileType = getFileExtension(attachment);
|
const fileType = getFileExtension(attachment);
|
||||||
const extension = fileType ? `.${fileType}` : '';
|
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}`;
|
return `${prefix}${suffix}${indexSuffix}${extension}`;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue