82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
// Copyright 2021 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import {
|
|
getPendingAttachment,
|
|
preProcessAttachment,
|
|
processAttachment,
|
|
} from './processAttachment';
|
|
import { AttachmentType } from '../types/Attachment';
|
|
import { AttachmentToastType } from '../types/AttachmentToastType';
|
|
|
|
export type AddAttachmentActionType = (
|
|
conversationId: string,
|
|
attachment: AttachmentType
|
|
) => unknown;
|
|
export type AddPendingAttachmentActionType = (
|
|
conversationId: string,
|
|
pendingAttachment: AttachmentType
|
|
) => unknown;
|
|
export type RemoveAttachmentActionType = (
|
|
conversationId: string,
|
|
filePath: string
|
|
) => unknown;
|
|
|
|
export type HandleAttachmentsProcessingArgsType = {
|
|
addAttachment: AddAttachmentActionType;
|
|
addPendingAttachment: AddPendingAttachmentActionType;
|
|
conversationId: string;
|
|
draftAttachments: ReadonlyArray<AttachmentType>;
|
|
files: ReadonlyArray<File>;
|
|
onShowToast: (toastType: AttachmentToastType) => unknown;
|
|
removeAttachment: RemoveAttachmentActionType;
|
|
};
|
|
|
|
export async function handleAttachmentsProcessing({
|
|
addAttachment,
|
|
addPendingAttachment,
|
|
conversationId,
|
|
draftAttachments,
|
|
files,
|
|
onShowToast,
|
|
removeAttachment,
|
|
}: HandleAttachmentsProcessingArgsType): Promise<void> {
|
|
if (!files.length) {
|
|
return;
|
|
}
|
|
|
|
const nextDraftAttachments = [...draftAttachments];
|
|
const filesToProcess: Array<File> = [];
|
|
for (let i = 0; i < files.length; i += 1) {
|
|
const file = files[i];
|
|
const processingResult = preProcessAttachment(file, nextDraftAttachments);
|
|
if (processingResult) {
|
|
onShowToast(processingResult);
|
|
} else {
|
|
const pendingAttachment = getPendingAttachment(file);
|
|
if (pendingAttachment) {
|
|
addPendingAttachment(conversationId, pendingAttachment);
|
|
filesToProcess.push(file);
|
|
// we keep a running count of the draft attachments so we can show a
|
|
// toast in case we add too many attachments at once
|
|
nextDraftAttachments.push(pendingAttachment);
|
|
}
|
|
}
|
|
}
|
|
|
|
await Promise.all(
|
|
filesToProcess.map(async file => {
|
|
try {
|
|
const attachment = await processAttachment(file);
|
|
if (!attachment) {
|
|
removeAttachment(conversationId, file.path);
|
|
return;
|
|
}
|
|
addAttachment(conversationId, attachment);
|
|
} catch (err) {
|
|
removeAttachment(conversationId, file.path);
|
|
onShowToast(AttachmentToastType.ToastUnableToLoadAttachment);
|
|
}
|
|
})
|
|
);
|
|
}
|