Fix units of maximum attachment size

This commit is contained in:
Fedor Indutny 2023-01-05 13:47:11 -08:00 committed by GitHub
parent 198d6f7e26
commit 487bb58880
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 13 deletions

View file

@ -50,7 +50,7 @@ import {
resetLinkPreview,
suspendLinkPreviews,
} from '../../services/LinkPreview';
import { getMaximumAttachmentSize } from '../../util/attachments';
import { getMaximumAttachmentSizeInKb } from '../../util/attachments';
import { getRecipientsByConversation } from '../../util/getRecipientsByConversation';
import {
getRenderDetailsForLimit,
@ -900,7 +900,7 @@ function preProcessAttachment(
return;
}
const limitKb = getMaximumAttachmentSize();
const limitKb = getMaximumAttachmentSizeInKb();
if (file.size > limitKb) {
return {
toastType: ToastType.FileSize,

View file

@ -14,20 +14,23 @@ import type { LoggerType } from '../types/Logging';
import * as MIME from '../types/MIME';
import * as Errors from '../types/errors';
export const KIBIBYTE = 1024;
const MEBIBYTE = 1024 * 1024;
const DEFAULT_MAX = 100 * MEBIBYTE;
export const getMaximumAttachmentSize = (): number => {
export const getMaximumAttachmentSizeInKb = (): number => {
try {
return parseIntOrThrow(
getValue('global.attachments.maxBytes'),
'preProcessAttachment/maxAttachmentSize'
return (
parseIntOrThrow(
getValue('global.attachments.maxBytes'),
'preProcessAttachment/maxAttachmentSize'
) / KIBIBYTE
);
} catch (error) {
log.warn(
'Failed to parse integer out of global.attachments.maxBytes feature flag'
);
return DEFAULT_MAX;
return DEFAULT_MAX / KIBIBYTE;
}
};

View file

@ -6,7 +6,7 @@ import type {
AttachmentType,
InMemoryAttachmentDraftType,
} from '../types/Attachment';
import { getMaximumAttachmentSize } from './attachments';
import { getMaximumAttachmentSizeInKb, KIBIBYTE } from './attachments';
import * as Errors from '../types/errors';
import { fileToBytes } from './fileToBytes';
import { handleImageAttachment } from './handleImageAttachment';
@ -74,11 +74,11 @@ export function getRenderDetailsForLimit(limitKb: number): {
} {
const units = ['kB', 'MB', 'GB'];
let u = -1;
let limit = limitKb * 1000;
let limit = limitKb * KIBIBYTE;
do {
limit /= 1000;
limit /= KIBIBYTE;
u += 1;
} while (limit >= 1000 && u < units.length - 1);
} while (limit >= KIBIBYTE && u < units.length - 1);
return {
limit: limit.toFixed(0),
@ -87,11 +87,11 @@ export function getRenderDetailsForLimit(limitKb: number): {
}
function isAttachmentSizeOkay(attachment: Readonly<AttachmentType>): boolean {
const limitKb = getMaximumAttachmentSize();
const limitKb = getMaximumAttachmentSizeInKb();
// 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) {
if ((attachment.data.byteLength / KIBIBYTE).toFixed(4) >= limitKb) {
showToast(ToastFileSize, getRenderDetailsForLimit(limitKb));
return false;
}