Skip to content

Commit

Permalink
fix: api response format image/admin
Browse files Browse the repository at this point in the history
fix: no file upload error checking
  • Loading branch information
NidheeshaT committed Sep 27, 2023
1 parent 20cc517 commit 4c90413
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/pages/api/image/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { authOptions } from "../auth/[...nextauth]";
import { decodeForm } from "../../../utils/form";
import { uploadImage } from "../../../utils/cloudinary";

interface CloudinaryResponse {
secure_url: string;
}

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
Expand Down Expand Up @@ -33,7 +37,7 @@ export default async function handler(
if (!result)
res.status(500).send("Server Error")

res.status(200).send(result)
res.status(200).send({secure_url:result} as CloudinaryResponse)
}
catch (err) {
res.status(400).send(err)
Expand Down
4 changes: 4 additions & 0 deletions src/utils/form.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import formidable from "formidable"
import {errors} from "formidable";
import type { NextApiRequest } from "next";

export const decodeForm = async (req: NextApiRequest) => {
const form = formidable({ multiples: true})
return new Promise<{ files: formidable.File[]|undefined, fields: formidable.Fields }>(
(resolve, reject) => {
form.parse(req, (err, fields, files) => {
if(err && (err as {code?:number})?.code===errors.noEmptyFiles) {
return reject("Empty file not allowed")
}
if (err) {
return reject(err)
}
Expand Down

0 comments on commit 4c90413

Please sign in to comment.