signal-desktop/ts/util/isAttachmentSizeOkay.ts

33 lines
940 B
TypeScript
Raw Normal View History

2021-09-22 20:59:54 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { AttachmentType } from '../types/Attachment';
import { getUploadSizeLimitKb } from '../types/Attachment';
2021-09-22 20:59:54 +00:00
import { showToast } from './showToast';
import { ToastFileSize } from '../components/ToastFileSize';
export function isAttachmentSizeOkay(
attachment: Readonly<AttachmentType>
): boolean {
2021-09-24 00:49:05 +00:00
const limitKb = getUploadSizeLimitKb(attachment.contentType);
2021-09-22 20:59:54 +00:00
// 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;
}