FastGPT/packages/service/common/file/utils.ts
Archer 7bcee82f5f
Some checks are pending
Document deploy / sync-images (push) Waiting to run
Document deploy / generate-timestamp (push) Blocked by required conditions
Document deploy / build-images (map[domain:https://fastgpt.cn suffix:cn]) (push) Blocked by required conditions
Document deploy / build-images (map[domain:https://fastgpt.io suffix:io]) (push) Blocked by required conditions
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.cn kube_config:KUBE_CONFIG_CN suffix:cn]) (push) Blocked by required conditions
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.io kube_config:KUBE_CONFIG_IO suffix:io]) (push) Blocked by required conditions
Build FastGPT images in Personal warehouse / get-vars (push) Waiting to run
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:amd64 runs-on:ubuntu-24.04]) (push) Blocked by required conditions
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:arm64 runs-on:ubuntu-24.04-arm]) (push) Blocked by required conditions
Build FastGPT images in Personal warehouse / release-fastgpt-images (push) Blocked by required conditions
perf: memory leak (#5370)
* perf: memory leak

* perf: workflow share buffer;Circle checker;Get file from stream

* doc

* remove report.md
2025-08-03 22:37:45 +08:00

89 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { isProduction } from '@fastgpt/global/common/system/constants';
import fs from 'fs';
import path from 'path';
export const getFileMaxSize = () => {
const mb = global.feConfigs?.uploadFileMaxSize || 1000;
return mb * 1024 * 1024;
};
export const removeFilesByPaths = (paths: string[]) => {
paths.forEach((path) => {
fs.unlink(path, (err) => {
if (err) {
// console.error(err);
}
});
});
};
export const guessBase64ImageType = (str: string) => {
const imageTypeMap: Record<string, string> = {
'/': 'image/jpeg',
i: 'image/png',
R: 'image/gif',
U: 'image/webp',
Q: 'image/bmp',
P: 'image/svg+xml',
T: 'image/tiff',
J: 'image/jp2',
S: 'image/x-tga',
I: 'image/ief',
V: 'image/vnd.microsoft.icon',
W: 'image/vnd.wap.wbmp',
X: 'image/x-xbitmap',
Z: 'image/x-xpixmap',
Y: 'image/x-xwindowdump'
};
const defaultType = 'image/jpeg';
if (typeof str !== 'string' || str.length === 0) {
return defaultType;
}
const firstChar = str.charAt(0);
return imageTypeMap[firstChar] || defaultType;
};
export const getFileContentTypeFromHeader = (header: string): string | undefined => {
const contentType = header.split(';')[0];
return contentType;
};
export const clearDirFiles = (dirPath: string) => {
if (!fs.existsSync(dirPath)) {
return;
}
fs.rmdirSync(dirPath, {
recursive: true
});
};
export const clearTmpUploadFiles = () => {
if (!isProduction) return;
const tmpPath = '/tmp';
fs.readdir(tmpPath, (err, files) => {
if (err) return;
for (const file of files) {
if (file === 'v8-compile-cache-0') continue;
const filePath = path.join(tmpPath, file);
fs.stat(filePath, (err, stats) => {
if (err) return;
// 如果文件是在2小时前上传的则认为是临时文件并删除它
if (Date.now() - stats.mtime.getTime() > 2 * 60 * 60 * 1000) {
fs.unlink(filePath, (err) => {
if (err) return;
console.log(`Deleted temp file: ${filePath}`);
});
}
});
}
});
};