mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +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 { jsonRes } from '@fastgpt/service/common/response';
|
||||||
import { connectToDatabase } from '@/service/mongo';
|
import { connectToDatabase } from '@/service/mongo';
|
||||||
import { readMongoImg } from '@fastgpt/service/common/file/image/controller';
|
import { readMongoImg } from '@fastgpt/service/common/file/image/controller';
|
||||||
|
import { guessImageTypeFromBase64 } from '@fastgpt/service/common/file/utils';
|
||||||
|
|
||||||
// get the models available to the system
|
// get the models available to the system
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -9,9 +10,10 @@ 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 };
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'image/jpeg');
|
const binary = await readMongoImg({ id });
|
||||||
|
const imageType = guessImageTypeFromBase64(binary.toString('base64'));
|
||||||
res.send(await readMongoImg({ id }));
|
res.setHeader('Content-Type', imageType);
|
||||||
|
res.send(binary);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
code: 500,
|
code: 500,
|
||||||
|
Reference in New Issue
Block a user