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 stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
15 await Promise.all(
16 images.map(async (image) => {
17 try {
18 const dataUrl =
19 existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file));
20 stagedAttachmentById.set(image.id, {
21 id: image.id,
22 name: image.name,
23 mimeType: image.mimeType,
24 sizeBytes: image.sizeBytes,
25 dataUrl,
26 ...(image.source ? { source: image.source } : {}),
27 });
28 } catch {
29 const existingPersisted = existingPersistedById.get(image.id);
30 if (existingPersisted) {
31 stagedAttachmentById.set(image.id, existingPersisted);
32 }
33 }
34 }),
35 );
36 return Array.from(stagedAttachmentById.values());
37}