mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-24 22:03:54 +00:00
perf: upload img
This commit is contained in:
@@ -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}`;
|
||||
}
|
||||
|
Reference in New Issue
Block a user