fix: upload app for cache-file
This commit is contained in:
parent
cffaf48641
commit
089f629096
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,4 +5,5 @@ dist
|
|||||||
# dist/app.cjs
|
# dist/app.cjs
|
||||||
|
|
||||||
app.config.json5
|
app.config.json5
|
||||||
deploy.tar.gz
|
deploy.tar.gz
|
||||||
|
cache-file
|
@ -8,6 +8,7 @@ import { bucketName } from '@/modules/minio.ts';
|
|||||||
import { getContentType } from '@/utils/get-content-type.ts';
|
import { getContentType } from '@/utils/get-content-type.ts';
|
||||||
import { User } from '@/models/user.ts';
|
import { User } from '@/models/user.ts';
|
||||||
const filePath = useFileStore('upload', { needExists: true });
|
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"
|
||||||
// curl -X POST http://localhost:4000/api/upload \
|
// curl -X POST http://localhost:4000/api/upload \
|
||||||
// -F "file=@readme.md" \
|
// -F "file=@readme.md" \
|
||||||
@ -51,11 +52,17 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
|||||||
const form = new IncomingForm({
|
const form = new IncomingForm({
|
||||||
multiples: true, // 支持多文件上传
|
multiples: true, // 支持多文件上传
|
||||||
uploadDir: filePath, // 上传文件存储目录
|
uploadDir: filePath, // 上传文件存储目录
|
||||||
|
allowEmptyFiles: true, // 允许空文件
|
||||||
});
|
});
|
||||||
// 解析上传的文件
|
// 解析上传的文件
|
||||||
form.parse(req, async (err, fields, files) => {
|
form.parse(req, async (err, fields, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.end(error(`Upload error: ${err.message}`));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// 逐个处理每个上传的文件
|
// 逐个处理每个上传的文件
|
||||||
@ -93,13 +100,20 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
|||||||
// 使用 formidable 解析 multipart/form-data
|
// 使用 formidable 解析 multipart/form-data
|
||||||
const form = new IncomingForm({
|
const form = new IncomingForm({
|
||||||
multiples: true, // 支持多文件上传
|
multiples: true, // 支持多文件上传
|
||||||
uploadDir: filePath, // 上传文件存储目录
|
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||||
|
allowEmptyFiles: true, // 允许空
|
||||||
|
minFileSize: 0, // 最小文件大小
|
||||||
|
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||||
});
|
});
|
||||||
|
|
||||||
// 解析上传的文件
|
// 解析上传的文件
|
||||||
form.parse(req, async (err, fields, files) => {
|
form.parse(req, async (err, fields, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.end(error(`Upload error: ${err.message}`));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
const clearFiles = () => {
|
const clearFiles = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user