From ef15ca894ed97bd097ecd7bbc51434d40ad6442e Mon Sep 17 00:00:00 2001 From: xiaotian Date: Fri, 22 Mar 2024 13:19:42 +0800 Subject: [PATCH] fix: claude3 image type verification failed (#1038) (#1040) --- packages/service/common/file/utils.ts | 18 ++++++++++++++++++ projects/app/src/pages/api/system/img/[id].ts | 8 +++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/service/common/file/utils.ts b/packages/service/common/file/utils.ts index 3a744ef2cee5..d75da595888c 100644 --- a/packages/service/common/file/utils.ts +++ b/packages/service/common/file/utils.ts @@ -9,3 +9,21 @@ export const removeFilesByPaths = (paths: string[]) => { }); }); }; + +const imageTypeMap: Record = { + '/': '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; +}; diff --git a/projects/app/src/pages/api/system/img/[id].ts b/projects/app/src/pages/api/system/img/[id].ts index e626466e48d3..eb5d4e0105b2 100644 --- a/projects/app/src/pages/api/system/img/[id].ts +++ b/projects/app/src/pages/api/system/img/[id].ts @@ -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,