Merge pull request #1661 from oxen-io/clearnet

Use filename attachment name if it's there
pull/1666/head
Audric Ackermann 4 years ago committed by GitHub
commit c689b3c7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -47,7 +47,7 @@ describe('Attachment', () => {
contentType: MIME.VIDEO_QUICKTIME, contentType: MIME.VIDEO_QUICKTIME,
}; };
const actual = Attachment.getSuggestedFilename({ attachment }); const actual = Attachment.getSuggestedFilename({ attachment });
const expected = 'session-attachment.mov'; const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
it('should generate a filename without timestamp but with an index', () => { it('should generate a filename without timestamp but with an index', () => {
@ -60,7 +60,7 @@ describe('Attachment', () => {
attachment, attachment,
index: 3, index: 3,
}); });
const expected = 'session-attachment_003.mov'; const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
it('should generate a filename with an extension if contentType is not setup', () => { it('should generate a filename with an extension if contentType is not setup', () => {
@ -73,7 +73,7 @@ describe('Attachment', () => {
attachment, attachment,
index: 3, index: 3,
}); });
const expected = 'session-attachment_003.ini'; const expected = 'funny-cat.ini';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
@ -87,7 +87,7 @@ describe('Attachment', () => {
attachment, attachment,
index: 3, index: 3,
}); });
const expected = 'session-attachment_003.txt'; const expected = 'funny-cat.txt';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
it('should generate a filename with an extension if contentType is json', () => { it('should generate a filename with an extension if contentType is json', () => {
@ -100,7 +100,7 @@ describe('Attachment', () => {
attachment, attachment,
index: 3, index: 3,
}); });
const expected = 'session-attachment_003.json'; const expected = 'funny-cat.json';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
}); });
@ -116,14 +116,14 @@ describe('Attachment', () => {
attachment, attachment,
timestamp, timestamp,
}); });
const expected = 'session-attachment-2000-01-01-000000.mov'; const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
}); });
context('for attachment with index', () => { context('for attachment with index', () => {
it('should generate a filename based on timestamp', () => { it('should generate a filename based on timestamp if filename is not set', () => {
const attachment: Attachment.AttachmentType = { const attachment: Attachment.AttachmentType = {
fileName: 'funny-cat.mov', fileName: '',
url: 'funny-cat.mov', url: 'funny-cat.mov',
contentType: MIME.VIDEO_QUICKTIME, contentType: MIME.VIDEO_QUICKTIME,
}; };
@ -136,6 +136,22 @@ describe('Attachment', () => {
const expected = 'session-attachment-1970-01-01-000000_003.mov'; const expected = 'session-attachment-1970-01-01-000000_003.mov';
assert.strictEqual(actual, expected); assert.strictEqual(actual, expected);
}); });
it('should generate a filename based on filename if present', () => {
const attachment: Attachment.AttachmentType = {
fileName: 'funny-cat.mov',
url: 'funny-cat.mov',
contentType: MIME.VIDEO_QUICKTIME,
};
const timestamp = new Date(new Date(0).getTimezoneOffset() * 60 * 1000);
const actual = Attachment.getSuggestedFilename({
attachment,
timestamp,
index: 3,
});
const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected);
});
}); });
}); });

@ -334,6 +334,9 @@ export const getSuggestedFilename = ({
timestamp?: number | Date; timestamp?: number | Date;
index?: number; index?: number;
}): string => { }): string => {
if (attachment.fileName?.length > 3) {
return attachment.fileName;
}
const prefix = 'session-attachment'; const prefix = 'session-attachment';
const suffix = timestamp ? moment(timestamp).format('-YYYY-MM-DD-HHmmss') : ''; const suffix = timestamp ? moment(timestamp).format('-YYYY-MM-DD-HHmmss') : '';
const fileType = getFileExtension(attachment); const fileType = getFileExtension(attachment);

Loading…
Cancel
Save