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

View file

@ -14,20 +14,23 @@ import type { LoggerType } from '../types/Logging';
import * as MIME from '../types/MIME'; import * as MIME from '../types/MIME';
import * as Errors from '../types/errors'; import * as Errors from '../types/errors';
export const KIBIBYTE = 1024;
const MEBIBYTE = 1024 * 1024; const MEBIBYTE = 1024 * 1024;
const DEFAULT_MAX = 100 * MEBIBYTE; const DEFAULT_MAX = 100 * MEBIBYTE;
export const getMaximumAttachmentSize = (): number => { export const getMaximumAttachmentSizeInKb = (): number => {
try { try {
return parseIntOrThrow( return (
getValue('global.attachments.maxBytes'), parseIntOrThrow(
'preProcessAttachment/maxAttachmentSize' getValue('global.attachments.maxBytes'),
'preProcessAttachment/maxAttachmentSize'
) / KIBIBYTE
); );
} catch (error) { } catch (error) {
log.warn( log.warn(
'Failed to parse integer out of global.attachments.maxBytes feature flag' '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, AttachmentType,
InMemoryAttachmentDraftType, InMemoryAttachmentDraftType,
} from '../types/Attachment'; } from '../types/Attachment';
import { getMaximumAttachmentSize } from './attachments'; import { getMaximumAttachmentSizeInKb, KIBIBYTE } from './attachments';
import * as Errors from '../types/errors'; import * as Errors from '../types/errors';
import { fileToBytes } from './fileToBytes'; import { fileToBytes } from './fileToBytes';
import { handleImageAttachment } from './handleImageAttachment'; import { handleImageAttachment } from './handleImageAttachment';
@ -74,11 +74,11 @@ export function getRenderDetailsForLimit(limitKb: number): {
} { } {
const units = ['kB', 'MB', 'GB']; const units = ['kB', 'MB', 'GB'];
let u = -1; let u = -1;
let limit = limitKb * 1000; let limit = limitKb * KIBIBYTE;
do { do {
limit /= 1000; limit /= KIBIBYTE;
u += 1; u += 1;
} while (limit >= 1000 && u < units.length - 1); } while (limit >= KIBIBYTE && u < units.length - 1);
return { return {
limit: limit.toFixed(0), limit: limit.toFixed(0),
@ -87,11 +87,11 @@ export function getRenderDetailsForLimit(limitKb: number): {
} }
function isAttachmentSizeOkay(attachment: Readonly<AttachmentType>): boolean { function isAttachmentSizeOkay(attachment: Readonly<AttachmentType>): boolean {
const limitKb = getMaximumAttachmentSize(); const limitKb = getMaximumAttachmentSizeInKb();
// this needs to be cast properly // this needs to be cast properly
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
if ((attachment.data.byteLength / 1024).toFixed(4) >= limitKb) { if ((attachment.data.byteLength / KIBIBYTE).toFixed(4) >= limitKb) {
showToast(ToastFileSize, getRenderDetailsForLimit(limitKb)); showToast(ToastFileSize, getRenderDetailsForLimit(limitKb));
return false; return false;
} }