test: add download zip flow

This commit is contained in:
2026-09-26 02:54:01 +05:00
parent 0933f1cdea
commit 746d6a6c8f
12 changed files with 211 additions and 53 deletions
+18 -5
View File
@@ -1,7 +1,21 @@
import { zipSync } from "fflate";
import { downloadBlob, encode } from "./core/io";
import type { PixelImage } from "./core/types";
import type { ToolImageFile } from "./registry";
export type ZipImageEncoder = (image: PixelImage) => Promise<Uint8Array>;
export async function buildZipEntries(
files: ToolImageFile[],
encodeImage: ZipImageEncoder,
): Promise<Record<string, Uint8Array>> {
const entries: Record<string, Uint8Array> = {};
for (const file of files) {
entries[file.name] = await encodeImage(file.image);
}
return entries;
}
/**
* Собирает набор картинок (1 → many) в zip-архив и скачивает его.
* Картинки кодируются как PNG; кодирование требует DOM — вызов из UI-слоя.
@@ -11,11 +25,10 @@ export async function downloadZip(
zipName: string,
): Promise<void> {
if (files.length === 0) return;
const entries: Record<string, Uint8Array> = {};
for (const file of files) {
const blob = await encode(file.image, "image/png");
entries[file.name] = new Uint8Array(await blob.arrayBuffer());
}
const entries = await buildZipEntries(files, async (image) => {
const blob = await encode(image, "image/png");
return new Uint8Array(await blob.arrayBuffer());
});
const zipped = zipSync(entries);
downloadBlob(new Blob([zipped], { type: "application/zip" }), zipName);
}