mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-20 18:54:09 +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 { connectToDatabase, Image } from '@/service/mongo';
|
||||||
import { authUser } from '@/service/utils/auth';
|
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) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
const { userId } = await authUser({ req, authToken: true });
|
const { userId } = await authUser({ req, authToken: true });
|
||||||
const { base64Img } = req.body;
|
const { base64Img } = req.body as Props;
|
||||||
const base64Data = base64Img.split(',')[1];
|
|
||||||
|
|
||||||
const { _id } = await Image.create({
|
const data = await uploadImg({
|
||||||
userId,
|
userId,
|
||||||
binary: Buffer.from(base64Data, 'base64')
|
base64Img
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonRes(res, { data: `/api/system/img/${_id}` });
|
jsonRes(res, { data });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
code: 500,
|
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