diff --git a/client/src/pages/api/system/uploadImage.ts b/client/src/pages/api/system/uploadImage.ts index bf02fc6f9..1220e125c 100644 --- a/client/src/pages/api/system/uploadImage.ts +++ b/client/src/pages/api/system/uploadImage.ts @@ -3,20 +3,20 @@ import { jsonRes } from '@/service/response'; import { connectToDatabase, Image } from '@/service/mongo'; import { authUser } from '@/service/utils/auth'; -// get the models available to the system +type Props = { base64Img: string }; + export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { await connectToDatabase(); const { userId } = await authUser({ req, authToken: true }); - const { base64Img } = req.body; - const base64Data = base64Img.split(',')[1]; + const { base64Img } = req.body as Props; - const { _id } = await Image.create({ + const data = await uploadImg({ userId, - binary: Buffer.from(base64Data, 'base64') + base64Img }); - jsonRes(res, { data: `/api/system/img/${_id}` }); + jsonRes(res, { data }); } catch (error) { jsonRes(res, { code: 500, @@ -24,3 +24,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }); } } + +export async function uploadImg({ base64Img, userId }: Props & { userId: string }) { + const base64Data = base64Img.split(',')[1]; + + const { _id } = await Image.create({ + userId, + binary: Buffer.from(base64Data, 'base64') + }); + + return `/api/system/img/${_id}`; +}