Reuse files on disk for outgoing messages

This commit is contained in:
trevor-signal
2026-02-23 15:35:11 -05:00
committed by GitHub
parent 491de86ad3
commit 2b243bb457
12 changed files with 410 additions and 111 deletions

View File

@@ -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 };
}