refactor upload image endpoint
This commit is contained in:
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user