2021-09-24 20:02:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import {
|
|
|
|
getPendingAttachment,
|
|
|
|
preProcessAttachment,
|
|
|
|
processAttachment,
|
|
|
|
} from './processAttachment';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { AttachmentType } from '../types/Attachment';
|
2021-09-24 20:02:30 +00:00
|
|
|
import { AttachmentToastType } from '../types/AttachmentToastType';
|
2021-11-04 01:38:46 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-09-24 20:02:30 +00:00
|
|
|
|
|
|
|
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) {
|
2021-11-04 01:38:46 +00:00
|
|
|
log.error(
|
|
|
|
'handleAttachmentsProcessing: failed to process attachment:',
|
|
|
|
err.stack
|
|
|
|
);
|
2021-09-24 20:02:30 +00:00
|
|
|
removeAttachment(conversationId, file.path);
|
|
|
|
onShowToast(AttachmentToastType.ToastUnableToLoadAttachment);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|