mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 12:20:34 +00:00
@@ -9,3 +9,21 @@ export const removeFilesByPaths = (paths: string[]) => {
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const imageTypeMap: Record<string, string> = {
|
||||
'/': 'image/jpeg',
|
||||
i: 'image/png',
|
||||
R: 'image/gif',
|
||||
U: 'image/webp',
|
||||
Q: 'image/bmp'
|
||||
};
|
||||
|
||||
export const guessImageTypeFromBase64 = (str: string) => {
|
||||
const defaultType = 'image/jpeg';
|
||||
if (typeof str !== 'string' || str.length === 0) {
|
||||
return defaultType;
|
||||
}
|
||||
|
||||
const firstChar = str.charAt(0);
|
||||
return imageTypeMap[firstChar] || defaultType;
|
||||
};
|
||||
|
@@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@fastgpt/service/common/response';
|
||||
import { connectToDatabase } from '@/service/mongo';
|
||||
import { readMongoImg } from '@fastgpt/service/common/file/image/controller';
|
||||
import { guessImageTypeFromBase64 } from '@fastgpt/service/common/file/utils';
|
||||
|
||||
// get the models available to the system
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -9,9 +10,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
await connectToDatabase();
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
res.setHeader('Content-Type', 'image/jpeg');
|
||||
|
||||
res.send(await readMongoImg({ id }));
|
||||
const binary = await readMongoImg({ id });
|
||||
const imageType = guessImageTypeFromBase64(binary.toString('base64'));
|
||||
res.setHeader('Content-Type', imageType);
|
||||
res.send(binary);
|
||||
} catch (error) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
|
Reference in New Issue
Block a user