From 8b4051e4731cd07c5aa020eb42b12edb042a28a9 Mon Sep 17 00:00:00 2001 From: June Date: Tue, 13 Jan 2026 12:03:33 +1300 Subject: [PATCH] refactor upload image endpoint --- src/pages/api/image/index.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pages/api/image/index.ts b/src/pages/api/image/index.ts index 7b48996..2e3d21c 100644 --- a/src/pages/api/image/index.ts +++ b/src/pages/api/image/index.ts @@ -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); +} \ No newline at end of file