Signal-Desktop/ts/types/Attachment.ts

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-04-09 23:29:38 +00:00
import is from '@sindresorhus/is';
2018-04-25 17:27:28 +00:00
import moment from 'moment';
2018-04-09 23:29:38 +00:00
import * as GoogleChrome from '../util/GoogleChrome';
2018-04-26 20:42:10 +00:00
import { saveURLAsFile } from '../util/saveURLAsFile';
2018-04-25 17:27:28 +00:00
import { arrayBufferToObjectURL } from '../util/arrayBufferToObjectURL';
2018-04-09 23:28:54 +00:00
import { MIMEType } from './MIME';
2018-04-26 20:47:05 +00:00
export type Attachment = {
2018-04-09 23:28:54 +00:00
fileName?: string;
contentType?: MIMEType;
size?: number;
data: ArrayBuffer;
// // Omit unused / deprecated keys:
// schemaVersion?: number;
// id?: string;
// width?: number;
// height?: number;
// thumbnail?: ArrayBuffer;
// key?: ArrayBuffer;
// digest?: ArrayBuffer;
// flags?: number;
2018-04-26 20:47:05 +00:00
} & Partial<AttachmentSchemaVersion3>;
interface AttachmentSchemaVersion3 {
path: string;
2018-04-12 20:23:26 +00:00
}
2018-04-09 23:29:38 +00:00
2018-04-25 17:27:28 +00:00
const SAVE_CONTENT_TYPE = 'application/octet-stream' as MIMEType;
2018-04-09 23:29:38 +00:00
export const isVisualMedia = (attachment: Attachment): boolean => {
const { contentType } = attachment;
if (is.undefined(contentType)) {
return false;
}
const isSupportedImageType = GoogleChrome.isImageTypeSupported(contentType);
const isSupportedVideoType = GoogleChrome.isVideoTypeSupported(contentType);
return isSupportedImageType || isSupportedVideoType;
};
2018-04-25 17:27:28 +00:00
export const save = ({
attachment,
2018-04-26 20:42:10 +00:00
document,
2018-04-26 20:48:08 +00:00
getAbsolutePath,
2018-04-25 17:27:28 +00:00
timestamp,
}: {
attachment: Attachment;
2018-04-26 20:42:10 +00:00
document: Document;
2018-04-26 20:48:08 +00:00
getAbsolutePath: (relativePath: string) => string;
2018-04-25 17:27:28 +00:00
timestamp?: number;
}): void => {
2018-04-30 15:01:57 +00:00
const isObjectURLRequired = is.undefined(attachment.path);
2018-04-26 20:48:08 +00:00
const url = !is.undefined(attachment.path)
? getAbsolutePath(attachment.path)
: arrayBufferToObjectURL({
data: attachment.data,
type: SAVE_CONTENT_TYPE,
});
2018-04-26 20:42:10 +00:00
const filename = getSuggestedFilename({ attachment, timestamp });
saveURLAsFile({ url, filename, document });
2018-04-30 15:01:57 +00:00
if (isObjectURLRequired) {
URL.revokeObjectURL(url);
}
2018-04-25 17:27:28 +00:00
};
export const getSuggestedFilename = ({
attachment,
timestamp,
}: {
attachment: Attachment;
timestamp?: number | Date;
}): string => {
if (attachment.fileName) {
return attachment.fileName;
}
const prefix = 'signal-attachment';
const suffix = timestamp
? moment(timestamp).format('-YYYY-MM-DD-HHmmss')
: '';
const fileType = getFileExtension(attachment);
const extension = fileType ? `.${fileType}` : '';
return `${prefix}${suffix}${extension}`;
};
export const getFileExtension = (attachment: Attachment): string | null => {
if (!attachment.contentType) {
return null;
}
switch (attachment.contentType) {
case 'video/quicktime':
return 'mov';
default:
// TODO: Use better MIME --> file extension mapping:
return attachment.contentType.split('/')[1];
}
};