More lenient attachment v2 migration

This commit is contained in:
Fedor Indutny 2024-07-15 14:27:54 -07:00 committed by GitHub
parent 12b57601ac
commit 3f9032035f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 72 additions and 43 deletions

View file

@ -2,10 +2,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
import LRU from 'lru-cache';
import type {
AddressableAttachmentType,
LocalAttachmentV2Type,
} from '../types/Attachment';
import * as log from '../logging/log';
import { AttachmentDisposition } from './getLocalAttachmentUrl';
let setCheck = false;
@ -15,6 +17,7 @@ const lru = new LRU<string, Promise<LocalAttachmentV2Type>>({
});
export type EncryptLegacyAttachmentOptionsType = Readonly<{
logId: string;
disposition?: AttachmentDisposition;
readAttachmentData: (
attachment: Partial<AddressableAttachmentType>
@ -43,12 +46,18 @@ export async function encryptLegacyAttachment<
promise = doEncrypt(attachment, options);
lru.set(cacheKey, promise);
}
const modern = await promise;
try {
const modern = await promise;
return {
...attachment,
...modern,
};
return {
...attachment,
...modern,
};
} catch (error) {
const { logId } = options;
log.error(`${logId}: migration failed, falling back to original`, error);
return attachment;
}
}
async function doEncrypt<T extends Partial<AddressableAttachmentType>>(