2021-09-24 20:02:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as log from '../logging/log';
|
2021-11-15 21:54:33 +00:00
|
|
|
import type {
|
2022-12-08 01:26:59 +00:00
|
|
|
AttachmentType,
|
2021-11-15 21:54:33 +00:00
|
|
|
InMemoryAttachmentDraftType,
|
|
|
|
} from '../types/Attachment';
|
2023-02-28 22:17:22 +00:00
|
|
|
import {
|
|
|
|
getMaximumAttachmentSizeInKb,
|
|
|
|
getRenderDetailsForLimit,
|
|
|
|
KIBIBYTE,
|
|
|
|
} from '../types/AttachmentSize';
|
2022-12-08 01:26:59 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-02-28 22:17:22 +00:00
|
|
|
import { getValue as getRemoteConfigValue } from '../RemoteConfig';
|
2021-09-24 20:02:30 +00:00
|
|
|
import { fileToBytes } from './fileToBytes';
|
|
|
|
import { handleImageAttachment } from './handleImageAttachment';
|
|
|
|
import { handleVideoAttachment } from './handleVideoAttachment';
|
2022-12-08 01:26:59 +00:00
|
|
|
import { isHeic, stringToMIMEType } from '../types/MIME';
|
2021-09-24 20:02:30 +00:00
|
|
|
import { isImageTypeSupported, isVideoTypeSupported } from './GoogleChrome';
|
2022-12-08 01:26:59 +00:00
|
|
|
import { showToast } from './showToast';
|
|
|
|
import { ToastFileSize } from '../components/ToastFileSize';
|
2021-09-24 20:02:30 +00:00
|
|
|
|
|
|
|
export async function processAttachment(
|
2023-07-18 01:00:02 +00:00
|
|
|
file: File,
|
|
|
|
options?: { generateScreenshot: boolean }
|
2021-11-15 21:54:33 +00:00
|
|
|
): Promise<InMemoryAttachmentDraftType | void> {
|
2021-09-24 20:02:30 +00:00
|
|
|
const fileType = stringToMIMEType(file.type);
|
|
|
|
|
2021-11-15 21:54:33 +00:00
|
|
|
let attachment: InMemoryAttachmentDraftType;
|
2021-09-24 20:02:30 +00:00
|
|
|
try {
|
2021-12-06 17:20:27 +00:00
|
|
|
if (isImageTypeSupported(fileType) || isHeic(fileType, file.name)) {
|
2021-09-24 20:02:30 +00:00
|
|
|
attachment = await handleImageAttachment(file);
|
|
|
|
} else if (isVideoTypeSupported(fileType)) {
|
2023-07-18 01:00:02 +00:00
|
|
|
attachment = await handleVideoAttachment(file, options);
|
2021-09-24 20:02:30 +00:00
|
|
|
} else {
|
|
|
|
const data = await fileToBytes(file);
|
|
|
|
attachment = {
|
|
|
|
contentType: fileType,
|
|
|
|
data,
|
|
|
|
fileName: file.name,
|
|
|
|
path: file.name,
|
|
|
|
pending: false,
|
|
|
|
size: data.byteLength,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error(
|
|
|
|
`Was unable to generate thumbnail for fileType ${fileType}`,
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(e)
|
2021-09-24 20:02:30 +00:00
|
|
|
);
|
|
|
|
const data = await fileToBytes(file);
|
|
|
|
attachment = {
|
|
|
|
contentType: fileType,
|
|
|
|
data,
|
|
|
|
fileName: file.name,
|
|
|
|
path: file.name,
|
|
|
|
pending: false,
|
|
|
|
size: data.byteLength,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (isAttachmentSizeOkay(attachment)) {
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'Error ensuring that image is properly sized:',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-09-24 20:02:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 01:26:59 +00:00
|
|
|
|
|
|
|
function isAttachmentSizeOkay(attachment: Readonly<AttachmentType>): boolean {
|
2023-02-28 22:17:22 +00:00
|
|
|
const limitKb = getMaximumAttachmentSizeInKb(getRemoteConfigValue);
|
2022-12-08 01:26:59 +00:00
|
|
|
// this needs to be cast properly
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
2023-01-05 21:47:11 +00:00
|
|
|
if ((attachment.data.byteLength / KIBIBYTE).toFixed(4) >= limitKb) {
|
2022-12-08 01:26:59 +00:00
|
|
|
showToast(ToastFileSize, getRenderDetailsForLimit(limitKb));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|