mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00

* perf: read file icon * perf:icon * fix: i18n * perf: hide pro api * perf: upload expired time * perf: upload file frequency limit * perf: upload file ux * perf: input file tip * perf: qa custom chunk size * feat: dataset openapi * fix: auth dataset list * fix: openapi doc * perf: zero temperature change to 0.01 * perf: read file prompt * perf: read file prompt * perf: free plan tip * feat: cron job usage
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { detectFileEncoding } from '@fastgpt/global/common/file/tools';
|
|
import { PassThrough } from 'stream';
|
|
|
|
export const gridFsStream2Buffer = (stream: NodeJS.ReadableStream) => {
|
|
return new Promise<Buffer>((resolve, reject) => {
|
|
let tmpBuffer: Buffer = Buffer.from([]);
|
|
|
|
stream.on('data', (chunk) => {
|
|
tmpBuffer = Buffer.concat([tmpBuffer, chunk]);
|
|
});
|
|
stream.on('end', () => {
|
|
resolve(tmpBuffer);
|
|
});
|
|
stream.on('error', (err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
};
|
|
|
|
export const stream2Encoding = async (stream: NodeJS.ReadableStream) => {
|
|
const start = Date.now();
|
|
const copyStream = stream.pipe(new PassThrough());
|
|
|
|
/* get encoding */
|
|
const buffer = await (() => {
|
|
return new Promise<Buffer>((resolve, reject) => {
|
|
let tmpBuffer: Buffer = Buffer.from([]);
|
|
|
|
stream.on('data', (chunk) => {
|
|
if (tmpBuffer.length < 200) {
|
|
tmpBuffer = Buffer.concat([tmpBuffer, chunk]);
|
|
|
|
if (tmpBuffer.length >= 200) {
|
|
resolve(tmpBuffer);
|
|
}
|
|
}
|
|
});
|
|
stream.on('end', () => {
|
|
resolve(tmpBuffer);
|
|
});
|
|
stream.on('error', (err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
})();
|
|
|
|
const enc = detectFileEncoding(buffer);
|
|
|
|
return {
|
|
encoding: enc,
|
|
stream: copyStream
|
|
};
|
|
};
|