perf: forbid image to base64 (#3038)

* perf: forbid image to base64

* update file upload path

* feat: support promptCall use image

* fix: echarts load

* update doc
This commit is contained in:
Archer
2024-11-01 14:29:20 +08:00
committed by GitHub
parent 7ef1821557
commit 912b264a47
12 changed files with 197 additions and 58 deletions

View File

@@ -70,9 +70,9 @@ COPY --from=maindeps /app/node_modules/@zilliz/milvus2-sdk-node ./node_modules/@
# copy package.json to version file
COPY --from=builder /app/projects/app/package.json ./package.json
# copy config
COPY ./projects/app/data /app/data
RUN chown -R nextjs:nodejs /app/data
ENV NODE_ENV=production

View File

@@ -0,0 +1,63 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@fastgpt/service/common/response';
import { connectToDatabase } from '@/service/mongo';
import { authFileToken } from '@fastgpt/service/support/permission/controller';
import { getDownloadStream, getFileById } from '@fastgpt/service/common/file/gridfs/controller';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
import { stream2Encoding } from '@fastgpt/service/common/file/gridfs/utils';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
await connectToDatabase();
const { token, filename } = req.query as { token: string; filename: string };
const { fileId, bucketName } = await authFileToken(token);
if (!fileId) {
throw new Error('fileId is empty');
}
const [file, fileStream] = await Promise.all([
getFileById({ bucketName, fileId }),
getDownloadStream({ bucketName, fileId })
]);
if (!file) {
return Promise.reject(CommonErrEnum.fileNotFound);
}
const { stream, encoding } = await (async () => {
if (file.metadata?.encoding) {
return {
stream: fileStream,
encoding: file.metadata.encoding
};
}
return stream2Encoding(fileStream);
})();
res.setHeader('Content-Type', `${file.contentType}; charset=${encoding}`);
res.setHeader('Cache-Control', 'public, max-age=31536000');
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(filename)}"`);
stream.pipe(res);
stream.on('error', () => {
res.status(500).end();
});
stream.on('end', () => {
res.end();
});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}
export const config = {
api: {
responseLimit: '100mb'
}
};

View File

@@ -57,14 +57,12 @@ async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
jsonRes(res, {
data: {
fileId,
previewUrl: `${ReadFileBaseUrl}?filename=${file.originalname}&token=${await createFileToken(
{
bucketName,
teamId,
tmbId,
fileId
}
)}`
previewUrl: `${ReadFileBaseUrl}/${file.originalname}?token=${await createFileToken({
bucketName,
teamId,
tmbId,
fileId
})}`
}
});
} catch (error) {