use attachment name when we have one for file saving

pull/1660/head
Audric Ackermann 4 years ago
parent ab22126b45
commit 1f99f2f71c
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -47,7 +47,7 @@ describe('Attachment', () => {
contentType: MIME.VIDEO_QUICKTIME,
};
const actual = Attachment.getSuggestedFilename({ attachment });
const expected = 'session-attachment.mov';
const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected);
});
it('should generate a filename without timestamp but with an index', () => {
@ -60,7 +60,7 @@ describe('Attachment', () => {
attachment,
index: 3,
});
const expected = 'session-attachment_003.mov';
const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected);
});
it('should generate a filename with an extension if contentType is not setup', () => {
@ -73,7 +73,7 @@ describe('Attachment', () => {
attachment,
index: 3,
});
const expected = 'session-attachment_003.ini';
const expected = 'funny-cat.ini';
assert.strictEqual(actual, expected);
});
@ -87,7 +87,7 @@ describe('Attachment', () => {
attachment,
index: 3,
});
const expected = 'session-attachment_003.txt';
const expected = 'funny-cat.txt';
assert.strictEqual(actual, expected);
});
it('should generate a filename with an extension if contentType is json', () => {
@ -100,7 +100,7 @@ describe('Attachment', () => {
attachment,
index: 3,
});
const expected = 'session-attachment_003.json';
const expected = 'funny-cat.json';
assert.strictEqual(actual, expected);
});
});
@ -116,14 +116,14 @@ describe('Attachment', () => {
attachment,
timestamp,
});
const expected = 'session-attachment-2000-01-01-000000.mov';
const expected = 'funny-cat.mov';
assert.strictEqual(actual, expected);
});
});
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 = {
fileName: 'funny-cat.mov',
fileName: '',
url: 'funny-cat.mov',
contentType: MIME.VIDEO_QUICKTIME,
};
@ -136,6 +136,22 @@ describe('Attachment', () => {
const expected = 'session-attachment-1970-01-01-000000_003.mov';
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;
index?: number;
}): string => {
if (attachment.fileName?.length > 3) {
return attachment.fileName;
}
const prefix = 'session-attachment';
const suffix = timestamp ? moment(timestamp).format('-YYYY-MM-DD-HHmmss') : '';
const fileType = getFileExtension(attachment);

Loading…
Cancel
Save