ConversationView: Move attachments processing into redux
This commit is contained in:
parent
ff6750e4fd
commit
452e0b7b31
25 changed files with 544 additions and 763 deletions
|
@ -1,85 +1,20 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import path from 'path';
|
||||
|
||||
import * as log from '../logging/log';
|
||||
import type {
|
||||
AttachmentDraftType,
|
||||
AttachmentType,
|
||||
InMemoryAttachmentDraftType,
|
||||
} from '../types/Attachment';
|
||||
import * as Errors from '../types/errors';
|
||||
import { getMaximumAttachmentSize } from './attachments';
|
||||
import { AttachmentToastType } from '../types/AttachmentToastType';
|
||||
import * as Errors from '../types/errors';
|
||||
import { fileToBytes } from './fileToBytes';
|
||||
import { handleImageAttachment } from './handleImageAttachment';
|
||||
import { handleVideoAttachment } from './handleVideoAttachment';
|
||||
import { isAttachmentSizeOkay } from './isAttachmentSizeOkay';
|
||||
import { isFileDangerous } from './isFileDangerous';
|
||||
import { isHeic, isImage, isVideo, stringToMIMEType } from '../types/MIME';
|
||||
import { isHeic, stringToMIMEType } from '../types/MIME';
|
||||
import { isImageTypeSupported, isVideoTypeSupported } from './GoogleChrome';
|
||||
|
||||
export function getPendingAttachment(
|
||||
file: File
|
||||
): AttachmentDraftType | undefined {
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileType = stringToMIMEType(file.type);
|
||||
const { name: fileName } = path.parse(file.name);
|
||||
|
||||
return {
|
||||
contentType: fileType,
|
||||
fileName,
|
||||
size: file.size,
|
||||
path: file.name,
|
||||
pending: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function preProcessAttachment(
|
||||
file: File,
|
||||
draftAttachments: Array<AttachmentDraftType>
|
||||
): AttachmentToastType | undefined {
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.size > getMaximumAttachmentSize()) {
|
||||
return AttachmentToastType.ToastFileSize;
|
||||
}
|
||||
|
||||
if (isFileDangerous(file.name)) {
|
||||
return AttachmentToastType.ToastDangerousFileType;
|
||||
}
|
||||
|
||||
if (draftAttachments.length >= 32) {
|
||||
return AttachmentToastType.ToastMaxAttachments;
|
||||
}
|
||||
|
||||
const haveNonImageOrVideo = draftAttachments.some(
|
||||
(attachment: AttachmentDraftType) => {
|
||||
return (
|
||||
!isImage(attachment.contentType) && !isVideo(attachment.contentType)
|
||||
);
|
||||
}
|
||||
);
|
||||
// You can't add another attachment if you already have a non-image staged
|
||||
if (haveNonImageOrVideo) {
|
||||
return AttachmentToastType.ToastUnsupportedMultiAttachment;
|
||||
}
|
||||
|
||||
const fileType = stringToMIMEType(file.type);
|
||||
const imageOrVideo = isImage(fileType) || isVideo(fileType);
|
||||
|
||||
// You can't add a non-image attachment if you already have attachments staged
|
||||
if (!imageOrVideo && draftAttachments.length > 0) {
|
||||
return AttachmentToastType.ToastCannotMixMultiAndNonMultiAttachments;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
import { showToast } from './showToast';
|
||||
import { ToastFileSize } from '../components/ToastFileSize';
|
||||
|
||||
export async function processAttachment(
|
||||
file: File
|
||||
|
@ -132,3 +67,34 @@ export async function processAttachment(
|
|||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function getRenderDetailsForLimit(limitKb: number): {
|
||||
limit: string;
|
||||
units: string;
|
||||
} {
|
||||
const units = ['kB', 'MB', 'GB'];
|
||||
let u = -1;
|
||||
let limit = limitKb * 1000;
|
||||
do {
|
||||
limit /= 1000;
|
||||
u += 1;
|
||||
} while (limit >= 1000 && u < units.length - 1);
|
||||
|
||||
return {
|
||||
limit: limit.toFixed(0),
|
||||
units: units[u],
|
||||
};
|
||||
}
|
||||
|
||||
function isAttachmentSizeOkay(attachment: Readonly<AttachmentType>): boolean {
|
||||
const limitKb = getMaximumAttachmentSize();
|
||||
// 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) {
|
||||
showToast(ToastFileSize, getRenderDetailsForLimit(limitKb));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue