Signal-Desktop/ts/types/MIME.ts

49 lines
2.1 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
export type MIMEType = string & { _mimeTypeBrand: never };
2018-04-09 23:24:24 +00:00
2021-08-09 20:06:21 +00:00
export const stringToMIMEType = (value: string): MIMEType => {
return value as MIMEType;
};
export const MIMETypeToString = (value: MIMEType): string => {
return value as string;
};
2021-08-09 20:06:21 +00:00
export const APPLICATION_OCTET_STREAM = stringToMIMEType(
'application/octet-stream'
);
export const APPLICATION_JSON = stringToMIMEType('application/json');
export const AUDIO_AAC = stringToMIMEType('audio/aac');
export const AUDIO_MP3 = stringToMIMEType('audio/mp3');
export const IMAGE_GIF = stringToMIMEType('image/gif');
export const IMAGE_JPEG = stringToMIMEType('image/jpeg');
export const IMAGE_PNG = stringToMIMEType('image/png');
export const IMAGE_WEBP = stringToMIMEType('image/webp');
export const IMAGE_ICO = stringToMIMEType('image/x-icon');
export const IMAGE_BMP = stringToMIMEType('image/bmp');
export const VIDEO_MP4 = stringToMIMEType('video/mp4');
export const VIDEO_QUICKTIME = stringToMIMEType('video/quicktime');
export const LONG_MESSAGE = stringToMIMEType('text/x-signal-plain');
2022-08-02 19:31:55 +00:00
export const TEXT_ATTACHMENT = stringToMIMEType('text/x-signal-story');
2018-05-07 15:24:40 +00:00
export const isHeic = (value: string, fileName: string): boolean =>
value === 'image/heic' ||
value === 'image/heif' ||
fileName.endsWith('.heic') ||
fileName.endsWith('.heif');
export const isGif = (value: string): value is MIMEType =>
value === 'image/gif';
export const isJPEG = (value: string): value is MIMEType =>
value === 'image/jpeg';
export const isImage = (value: string): value is MIMEType =>
Boolean(value) && value.startsWith('image/');
export const isVideo = (value: string): value is MIMEType =>
Boolean(value) && value.startsWith('video/');
2020-04-16 19:19:37 +00:00
// As of 2020-04-16 aif files do not play in Electron nor Chrome. We should only
// recognize them as file attachments.
export const isAudio = (value: string): value is MIMEType =>
Boolean(value) && value.startsWith('audio/') && !value.endsWith('aiff');
export const isLongMessage = (value: unknown): value is MIMEType =>
value === LONG_MESSAGE;