refactor upload image endpoint
This commit is contained in:
@@ -13,6 +13,14 @@ export async function POST({ request }: APIContext) {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('image');
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -30,8 +38,4 @@ export async function POST({ request }: APIContext) {
|
||||
await fs.writeFile(filepath, buffer);
|
||||
|
||||
return httpResponse({ url: `/api/image/${filename}`}, 201);
|
||||
|
||||
} catch (error) {
|
||||
return httpResponse({ error: `Failed to upload image, ${error}` }, 500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user