fix: claude3 image type verification failed (#1038) (#1040)

This commit is contained in:
xiaotian
2024-03-22 13:19:42 +08:00
committed by GitHub
parent a63467d751
commit ef15ca894e
2 changed files with 23 additions and 3 deletions

View File

@@ -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;
};

View File

@@ -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,