42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { createImageFromFile } from "./createImageFromFile";
|
|
|
|
type BulkResult =
|
|
| { ok: true; artworkId: string; name: string }
|
|
| { ok: false; name: string; error: string };
|
|
|
|
export async function createImagesBulk(formData: FormData): Promise<BulkResult[]> {
|
|
const entries = formData.getAll("file");
|
|
const files = entries.filter((x): x is File => x instanceof File);
|
|
|
|
if (files.length === 0) {
|
|
throw new Error("No files received. Ensure you send FormData with key 'file'.");
|
|
}
|
|
|
|
const results: BulkResult[] = [];
|
|
|
|
for (const f of files) {
|
|
try {
|
|
if (!f.type.startsWith("image/")) {
|
|
results.push({ ok: false, name: f.name, error: "Unsupported file type" });
|
|
continue;
|
|
}
|
|
|
|
const artwork = await createImageFromFile(f, { colorMode: "defer" });
|
|
if (!artwork) {
|
|
results.push({ ok: false, name: f.name, error: "Upload failed" });
|
|
continue;
|
|
}
|
|
|
|
results.push({ ok: true, artworkId: artwork.id, name: f.name });
|
|
} catch (err) {
|
|
results.push({
|
|
ok: false,
|
|
name: f.name,
|
|
error: err instanceof Error ? err.message : "Upload failed",
|
|
});
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|