Open History to see the last commit
1import type {
2 ComposerImageAttachment,
3 PersistedComposerImageAttachment,
4} from "../../composerDraftStore";
5import { readFileAsDataUrl } from "../../lib/imageCompression";
6
7export async function serializeComposerImages(
8 images: ReadonlyArray<ComposerImageAttachment>,
9 persistedAttachments: ReadonlyArray<PersistedComposerImageAttachment>,
10): Promise<PersistedComposerImageAttachment[]> {
11 const existingPersistedById = new Map(
12 persistedAttachments.map((attachment) => [attachment.id, attachment]),
13 );
14 const staged = await Promise.all(
15 images.map(async (image) => {
16 try {
17 const dataUrl =
18 existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file));
19 return {
20 id: image.id,
21 name: image.name,
22 mimeType: image.mimeType,
23 sizeBytes: image.sizeBytes,
24 dataUrl,
25 ...(image.source ? { source: image.source } : {}),
26 };
27 } catch {
28 return existingPersistedById.get(image.id);
29 }
30 }),
31 );
32 return staged.filter((attachment) => attachment !== undefined);
33}