Skip to content

Commit

Permalink
fix: claude3 image type verification failed (labring#1038) (labring#1040
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dimsky authored Mar 22, 2024
1 parent a63467d commit ef15ca8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/service/common/file/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
8 changes: 5 additions & 3 deletions projects/app/src/pages/api/system/img/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ 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) {
try {
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,
Expand Down

0 comments on commit ef15ca8

Please sign in to comment.