perf: upload img

This commit is contained in:
archer
2023-06-14 22:45:47 +08:00
parent 7c52cec0ea
commit cf3eb3b7b5

View File

@@ -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}`;
}