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 formData = await request.formData();
const file = formData.get('image'); 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); return httpResponse({ error: 'No image provided' }, 400);
} }
@@ -30,8 +38,4 @@ export async function POST({ request }: APIContext) {
await fs.writeFile(filepath, buffer); await fs.writeFile(filepath, buffer);
return httpResponse({ url: `/api/image/${filename}`}, 201); return httpResponse({ url: `/api/image/${filename}`}, 201);
} catch (error) {
return httpResponse({ error: `Failed to upload image, ${error}` }, 500);
}
} }