From fa4c3fda2b9a80fe1cd84d6ba8589ea79e1c4a89 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 7 May 2018 11:24:40 -0400 Subject: [PATCH] Extract common MIME types --- ts/test/types/Attachment_test.ts | 10 +++++----- .../types/message/initializeAttachmentMetadata_test.ts | 6 +++--- ts/types/MIME.ts | 6 +++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/ts/test/types/Attachment_test.ts b/ts/test/types/Attachment_test.ts index 343373d4b..cdb671eed 100644 --- a/ts/test/types/Attachment_test.ts +++ b/ts/test/types/Attachment_test.ts @@ -5,7 +5,7 @@ import 'mocha'; import { assert } from 'chai'; import * as Attachment from '../../types/Attachment'; -import { MIMEType } from '../../types/MIME'; +import * as MIME from '../../types/MIME'; // @ts-ignore import { stringToArrayBuffer } from '../../../js/modules/string_to_array_buffer'; @@ -14,7 +14,7 @@ describe('Attachment', () => { it('should return file extension from content type', () => { const input: Attachment.Attachment = { data: stringToArrayBuffer('foo'), - contentType: 'image/gif' as MIMEType, + contentType: MIME.IMAGE_GIF, }; assert.strictEqual(Attachment.getFileExtension(input), 'gif'); }); @@ -22,7 +22,7 @@ describe('Attachment', () => { it('should return file extension for QuickTime videos', () => { const input: Attachment.Attachment = { data: stringToArrayBuffer('foo'), - contentType: 'video/quicktime' as MIMEType, + contentType: MIME.VIDEO_QUICKTIME, }; assert.strictEqual(Attachment.getFileExtension(input), 'mov'); }); @@ -34,7 +34,7 @@ describe('Attachment', () => { const attachment: Attachment.Attachment = { fileName: 'funny-cat.mov', data: stringToArrayBuffer('foo'), - contentType: 'video/quicktime' as MIMEType, + contentType: MIME.VIDEO_QUICKTIME, }; const actual = Attachment.getSuggestedFilename({ attachment }); const expected = 'funny-cat.mov'; @@ -45,7 +45,7 @@ describe('Attachment', () => { it('should generate a filename based on timestamp', () => { const attachment: Attachment.Attachment = { data: stringToArrayBuffer('foo'), - contentType: 'video/quicktime' as MIMEType, + contentType: MIME.VIDEO_QUICKTIME, }; const timestamp = new Date(new Date(0).getTimezoneOffset() * 60 * 1000); const actual = Attachment.getSuggestedFilename({ diff --git a/ts/test/types/message/initializeAttachmentMetadata_test.ts b/ts/test/types/message/initializeAttachmentMetadata_test.ts index d76618f28..9c87215f3 100644 --- a/ts/test/types/message/initializeAttachmentMetadata_test.ts +++ b/ts/test/types/message/initializeAttachmentMetadata_test.ts @@ -3,7 +3,7 @@ import { assert } from 'chai'; import * as Message from '../../../../ts/types/message/initializeAttachmentMetadata'; import { IncomingMessage } from '../../../../ts/types/Message'; -import { MIMEType } from '../../../../ts/types/MIME'; +import * as MIME from '../../../../ts/types/MIME'; // @ts-ignore import { stringToArrayBuffer } from '../../../../js/modules/string_to_array_buffer'; @@ -19,7 +19,7 @@ describe('Message', () => { sent_at: 1523317140800, attachments: [ { - contentType: 'image/jpeg' as MIMEType, + contentType: MIME.IMAGE_JPEG, data: stringToArrayBuffer('foo'), fileName: 'foo.jpg', size: 1111, @@ -35,7 +35,7 @@ describe('Message', () => { sent_at: 1523317140800, attachments: [ { - contentType: 'image/jpeg' as MIMEType, + contentType: MIME.IMAGE_JPEG, data: stringToArrayBuffer('foo'), fileName: 'foo.jpg', size: 1111, diff --git a/ts/types/MIME.ts b/ts/types/MIME.ts index 43ed14efb..60dd01155 100644 --- a/ts/types/MIME.ts +++ b/ts/types/MIME.ts @@ -1,8 +1,12 @@ export type MIMEType = string & { _mimeTypeBrand: any }; +export const APPLICATION_OCTET_STREAM = 'application/octet-stream' as MIMEType; +export const IMAGE_GIF = 'image/gif' as MIMEType; +export const IMAGE_JPEG = 'image/jpeg' as MIMEType; +export const VIDEO_QUICKTIME = 'video/quicktime' as MIMEType; + export const isJPEG = (value: MIMEType): boolean => value === 'image/jpeg'; export const isImage = (value: MIMEType): boolean => value.startsWith('image/'); export const isVideo = (value: MIMEType): boolean => value.startsWith('video/'); export const isAudio = (value: MIMEType): boolean => value.startsWith('audio/'); -export const APPLICATION_OCTET_STREAM = 'application/octet-stream' as MIMEType;