mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-18 07:36:00 +01:00
Reuse files on disk for outgoing messages
This commit is contained in:
@@ -2,11 +2,9 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import lodash from 'lodash';
|
||||
import type {
|
||||
AttachmentType,
|
||||
LocalAttachmentV2Type,
|
||||
} from '../../types/Attachment.std.js';
|
||||
import type { LoggerType } from '../../types/Logging.std.js';
|
||||
import type { AttachmentType } from '../../types/Attachment.std.js';
|
||||
import type { ContextType } from '../../types/Message2.preload.js';
|
||||
import type { MessageAttributesType } from '../../model-types.js';
|
||||
|
||||
const { isFunction, isTypedArray, isUndefined, omit } = lodash;
|
||||
|
||||
@@ -14,13 +12,17 @@ export async function migrateDataToFileSystem(
|
||||
attachment: AttachmentType,
|
||||
{
|
||||
writeNewAttachmentData,
|
||||
getExistingAttachmentDataForReuse,
|
||||
getPlaintextHashForInMemoryAttachment,
|
||||
logger,
|
||||
}: {
|
||||
writeNewAttachmentData: (
|
||||
data: Uint8Array
|
||||
) => Promise<LocalAttachmentV2Type>;
|
||||
logger: LoggerType;
|
||||
}
|
||||
}: Pick<
|
||||
ContextType,
|
||||
| 'writeNewAttachmentData'
|
||||
| 'getExistingAttachmentDataForReuse'
|
||||
| 'getPlaintextHashForInMemoryAttachment'
|
||||
| 'logger'
|
||||
>,
|
||||
message: Pick<MessageAttributesType, 'id'>
|
||||
): Promise<AttachmentType> {
|
||||
if (!isFunction(writeNewAttachmentData)) {
|
||||
throw new TypeError("'writeNewAttachmentData' must be a function");
|
||||
@@ -42,8 +44,24 @@ export async function migrateDataToFileSystem(
|
||||
return omit({ ...attachment }, ['data']);
|
||||
}
|
||||
|
||||
const local = await writeNewAttachmentData(data);
|
||||
const plaintextHash = getPlaintextHashForInMemoryAttachment(data);
|
||||
|
||||
const attachmentWithoutData = omit({ ...attachment, ...local }, ['data']);
|
||||
return attachmentWithoutData;
|
||||
const existingData = await getExistingAttachmentDataForReuse({
|
||||
plaintextHash,
|
||||
messageId: message.id,
|
||||
contentType: attachment.contentType,
|
||||
logId: 'migrateDataToFileSystem',
|
||||
});
|
||||
const attachmentWithoutData = omit(attachment, ['data']);
|
||||
|
||||
if (existingData) {
|
||||
return {
|
||||
...attachmentWithoutData,
|
||||
plaintextHash,
|
||||
...existingData,
|
||||
};
|
||||
}
|
||||
|
||||
const newAttachmentData = await writeNewAttachmentData(data);
|
||||
return { ...attachmentWithoutData, ...newAttachmentData };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user