signal-desktop/ts/util/isAttachmentSizeOkay.ts
2021-09-23 17:49:05 -07:00

31 lines
897 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { AttachmentType, getUploadSizeLimitKb } from '../types/Attachment';
import { showToast } from './showToast';
import { ToastFileSize } from '../components/ToastFileSize';
export function isAttachmentSizeOkay(
attachment: Readonly<AttachmentType>
): boolean {
const limitKb = getUploadSizeLimitKb(attachment.contentType);
// this needs to be cast properly
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if ((attachment.data.byteLength / 1024).toFixed(4) >= limitKb) {
const units = ['kB', 'MB', 'GB'];
let u = -1;
let limit = limitKb * 1000;
do {
limit /= 1000;
u += 1;
} while (limit >= 1000 && u < units.length - 1);
showToast(ToastFileSize, {
limit,
units: units[u],
});
return false;
}
return true;
}