Add image upload and edit functions
This commit is contained in:
41
src/actions/uploads/createBulkImages.ts
Normal file
41
src/actions/uploads/createBulkImages.ts
Normal file
@ -0,0 +1,41 @@
|
||||
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);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user