refactor upload image endpoint

This commit is contained in:
2026-01-13 12:03:33 +13:00
parent 3f61caf04f
commit 8b4051e473

View File

@@ -13,7 +13,15 @@ export async function POST({ request }: APIContext) {
const formData = await request.formData();
const file = formData.get('image');
if (!file || !(file instanceof File)) {
return await writeImage(file)
} catch (error) {
return httpResponse({ error: `Failed to upload image, ${error}` }, 500);
}
}
async function writeImage(file) {
if (!file || !(file instanceof File)) {
return httpResponse({ error: 'No image provided' }, 400);
}
@@ -28,10 +36,6 @@ export async function POST({ request }: APIContext) {
// Save file
const buffer = Buffer.from(await file.arrayBuffer());
await fs.writeFile(filepath, buffer);
return httpResponse({ url: `/api/image/${filename}`}, 201);
} catch (error) {
return httpResponse({ error: `Failed to upload image, ${error}` }, 500);
}
}
return httpResponse({ url: `/api/image/${filename}`}, 201);
}