diff --git a/.gitignore b/.gitignore index 31a49a8..d021b07 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ dist # dist/app.cjs app.config.json5 -deploy.tar.gz \ No newline at end of file +deploy.tar.gz +cache-file \ No newline at end of file diff --git a/src/lib/upload.ts b/src/lib/upload.ts index 38859ab..b92b7ad 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -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 = () => {