mirror of
https://github.com/labring/FastGPT.git
synced 2025-12-25 20:02:47 +00:00
feat(image): metadata support image mime type (#2026)
Some checks are pending
Build FastGPT images in Personal warehouse / build-fastgpt-images (push) Waiting to run
Some checks are pending
Build FastGPT images in Personal warehouse / build-fastgpt-images (push) Waiting to run
This commit is contained in:
parent
dd2a9bdee5
commit
090c880860
|
|
@ -9,6 +9,7 @@ export type MongoImageSchemaType = {
|
||||||
type: `${MongoImageTypeEnum}`;
|
type: `${MongoImageTypeEnum}`;
|
||||||
|
|
||||||
metadata?: {
|
metadata?: {
|
||||||
|
mime?: string; // image mime type.
|
||||||
relatedId?: string; // This id is associated with a set of images
|
relatedId?: string; // This id is associated with a set of images
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ export function getMongoImgUrl(id: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const maxImgSize = 1024 * 1024 * 12;
|
export const maxImgSize = 1024 * 1024 * 12;
|
||||||
|
const base64MimeRegex = /data:image\/([^\)]+);base64/;
|
||||||
export async function uploadMongoImg({
|
export async function uploadMongoImg({
|
||||||
type,
|
type,
|
||||||
base64Img,
|
base64Img,
|
||||||
|
|
@ -22,7 +23,8 @@ export async function uploadMongoImg({
|
||||||
return Promise.reject('Image too large');
|
return Promise.reject('Image too large');
|
||||||
}
|
}
|
||||||
|
|
||||||
const base64Data = base64Img.split(',')[1];
|
const [base64Mime, base64Data] = base64Img.split(',')
|
||||||
|
const mime = `image/${base64Mime.match(base64MimeRegex)?.[1] ?? 'jpeg'}`
|
||||||
const binary = Buffer.from(base64Data, 'base64');
|
const binary = Buffer.from(base64Data, 'base64');
|
||||||
|
|
||||||
const { _id } = await MongoImage.create({
|
const { _id } = await MongoImage.create({
|
||||||
|
|
@ -30,7 +32,7 @@ export async function uploadMongoImg({
|
||||||
teamId,
|
teamId,
|
||||||
binary,
|
binary,
|
||||||
expiredTime,
|
expiredTime,
|
||||||
metadata,
|
metadata: Object.assign({ mime }, metadata),
|
||||||
shareId
|
shareId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -42,7 +44,7 @@ export async function readMongoImg({ id }: { id: string }) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return Promise.reject('Image not found');
|
return Promise.reject('Image not found');
|
||||||
}
|
}
|
||||||
return data?.binary;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function delImgByRelatedId({
|
export async function delImgByRelatedId({
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
const { id } = req.query as { id: string };
|
const { id } = req.query as { id: string };
|
||||||
|
|
||||||
const binary = await readMongoImg({ id });
|
const { binary, metadata } = await readMongoImg({ id });
|
||||||
|
|
||||||
res.setHeader('Content-Type', guessBase64ImageType(binary.toString('base64')));
|
res.setHeader('Content-Type', metadata?.mime ?? guessBase64ImageType(binary.toString('base64')));
|
||||||
res.send(binary);
|
res.send(binary);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue