fix: upload app for cache-file

This commit is contained in:
xion 2024-10-19 13:08:37 +08:00
parent cffaf48641
commit 089f629096
2 changed files with 17 additions and 2 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ dist
app.config.json5
deploy.tar.gz
cache-file

View File

@ -8,6 +8,7 @@ import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { User } from '@/models/user.ts';
const filePath = useFileStore('upload', { needExists: true });
const cacheFilePath = useFileStore('cache-file', { needExists: true });
// curl -X POST http://localhost:4000/api/upload -F "file=@readme.md"
// curl -X POST http://localhost:4000/api/upload \
// -F "file=@readme.md" \
@ -51,11 +52,17 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
const form = new IncomingForm({
multiples: true, // 支持多文件上传
uploadDir: filePath, // 上传文件存储目录
allowEmptyFiles: true, // 允许空文件
});
// 解析上传的文件
form.parse(req, async (err, fields, files) => {
if (err) {
res.end(error(`Upload error: ${err.message}`));
// 删除临时文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
uploadedFiles.forEach((file) => {
fs.unlinkSync(file.filepath);
});
return;
}
// 逐个处理每个上传的文件
@ -93,13 +100,20 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({
multiples: true, // 支持多文件上传
uploadDir: filePath, // 上传文件存储目录
uploadDir: cacheFilePath, // 上传文件存储目录
allowEmptyFiles: true, // 允许空
minFileSize: 0, // 最小文件大小
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
});
// 解析上传的文件
form.parse(req, async (err, fields, files) => {
if (err) {
res.end(error(`Upload error: ${err.message}`));
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
uploadedFiles.forEach((file) => {
fs.unlinkSync(file.filepath);
});
return;
}
const clearFiles = () => {