更新依赖项,使用 Busboy 替代 formidable 处理文件上传,优化上传逻辑,改进权限检查
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { IncomingForm } from 'formidable';
|
||||
import Busboy from 'busboy';
|
||||
import { checkAuth } from '../middleware/auth.ts';
|
||||
import { router, clients, writeEvents } from '../router.ts';
|
||||
import { error } from '../middleware/auth.ts';
|
||||
@@ -7,50 +7,94 @@ import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
import { app, minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { getContentType } from '@/utils/get-content-type.ts';
|
||||
import path from 'path';
|
||||
import { createWriteStream } from 'fs';
|
||||
import crypto from 'crypto';
|
||||
import { pipeBusboy } from '@/modules/fm-manager/index.ts';
|
||||
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||
|
||||
router.post('/api/micro-app/upload', async (req, res) => {
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
const { tokenUser, token } = await checkAuth(req, res);
|
||||
if (!tokenUser) return;
|
||||
//
|
||||
// 使用 formidable 解析 multipart/form-data
|
||||
const form = new IncomingForm({
|
||||
multiples: false, // 支持多文件上传
|
||||
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||
allowEmptyFiles: true, // 允许空
|
||||
minFileSize: 0, // 最小文件大小
|
||||
maxFiles: 1, // 最大文件数量
|
||||
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||
keepExtensions: true, // 保留文件
|
||||
hashAlgorithm: 'md5', // 文件哈希算法
|
||||
|
||||
// 使用 busboy 解析 multipart/form-data
|
||||
const busboy = Busboy({ headers: req.headers });
|
||||
const fields: any = {};
|
||||
let file: any = null;
|
||||
let filePromise: Promise<void> | null = null;
|
||||
let bytesReceived = 0;
|
||||
let bytesExpected = parseInt(req.headers['content-length'] || '0');
|
||||
|
||||
busboy.on('field', (fieldname, value) => {
|
||||
fields[fieldname] = value;
|
||||
});
|
||||
form.on('progress', (bytesReceived, bytesExpected) => {
|
||||
const progress = (bytesReceived / bytesExpected) * 100;
|
||||
console.log(`Upload progress: ${progress.toFixed(2)}%`);
|
||||
const data = {
|
||||
progress: progress.toFixed(2),
|
||||
message: `Upload progress: ${progress.toFixed(2)}%`,
|
||||
};
|
||||
writeEvents(req, data);
|
||||
});
|
||||
// 解析上传的文件
|
||||
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);
|
||||
|
||||
busboy.on('file', (fieldname, fileStream, info) => {
|
||||
const { filename, encoding, mimeType } = info;
|
||||
const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`);
|
||||
const writeStream = createWriteStream(tempPath);
|
||||
const hash = crypto.createHash('md5');
|
||||
let size = 0;
|
||||
|
||||
filePromise = new Promise<void>((resolve, reject) => {
|
||||
fileStream.on('data', (chunk) => {
|
||||
bytesReceived += chunk.length;
|
||||
size += chunk.length;
|
||||
hash.update(chunk);
|
||||
if (bytesExpected > 0) {
|
||||
const progress = (bytesReceived / bytesExpected) * 100;
|
||||
console.log(`Upload progress: ${progress.toFixed(2)}%`);
|
||||
const data = {
|
||||
progress: progress.toFixed(2),
|
||||
message: `Upload progress: ${progress.toFixed(2)}%`,
|
||||
};
|
||||
writeEvents(req, data);
|
||||
}
|
||||
});
|
||||
return;
|
||||
|
||||
fileStream.pipe(writeStream);
|
||||
|
||||
writeStream.on('finish', () => {
|
||||
file = {
|
||||
filepath: tempPath,
|
||||
originalFilename: filename,
|
||||
mimetype: mimeType,
|
||||
hash: hash.digest('hex'),
|
||||
size: size,
|
||||
};
|
||||
resolve();
|
||||
});
|
||||
|
||||
writeStream.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
busboy.on('finish', async () => {
|
||||
// 等待文件写入完成
|
||||
if (filePromise) {
|
||||
try {
|
||||
await filePromise;
|
||||
} catch (err) {
|
||||
console.error(`File write error: ${err.message}`);
|
||||
res.end(error(`File write error: ${err.message}`));
|
||||
return;
|
||||
}
|
||||
}
|
||||
const clearFiles = () => {
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
if (file?.filepath && fs.existsSync(file.filepath)) {
|
||||
fs.unlinkSync(file.filepath);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (!file) {
|
||||
res.end(error('No file uploaded'));
|
||||
return;
|
||||
}
|
||||
|
||||
let appKey, collection;
|
||||
const { appKey: _appKey, collection: _collecion } = fields;
|
||||
if (Array.isArray(_appKey)) {
|
||||
@@ -68,31 +112,28 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
appKey = appKey || 'micro-app';
|
||||
console.log('Appkey', appKey);
|
||||
console.log('collection', collection);
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
|
||||
// 处理上传的文件
|
||||
const uploadResults = [];
|
||||
for (let i = 0; i < uploadedFiles.length; i++) {
|
||||
const file = uploadedFiles[i];
|
||||
// @ts-ignore
|
||||
const tempPath = file.filepath; // 文件上传时的临时路径
|
||||
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
|
||||
// 比如 child2/b.txt
|
||||
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
|
||||
// 上传到 MinIO 并保留文件夹结构
|
||||
const isHTML = relativePath.endsWith('.html');
|
||||
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
|
||||
'Content-Type': getContentType(relativePath),
|
||||
'app-source': 'user-micro-app',
|
||||
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
|
||||
});
|
||||
uploadResults.push({
|
||||
name: relativePath,
|
||||
path: minioPath,
|
||||
hash: file.hash,
|
||||
size: file.size,
|
||||
});
|
||||
fs.unlinkSync(tempPath); // 删除临时文件
|
||||
}
|
||||
const tempPath = file.filepath; // 文件上传时的临时路径
|
||||
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
|
||||
// 比如 child2/b.txt
|
||||
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
|
||||
// 上传到 MinIO 并保留文件夹结构
|
||||
const isHTML = relativePath.endsWith('.html');
|
||||
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
|
||||
'Content-Type': getContentType(relativePath),
|
||||
'app-source': 'user-micro-app',
|
||||
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
|
||||
});
|
||||
uploadResults.push({
|
||||
name: relativePath,
|
||||
path: minioPath,
|
||||
hash: file.hash,
|
||||
size: file.size,
|
||||
});
|
||||
fs.unlinkSync(tempPath); // 删除临时文件
|
||||
|
||||
// 受控
|
||||
const r = await app.call({
|
||||
path: 'micro-app',
|
||||
@@ -115,6 +156,8 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
}
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
|
||||
pipeBusboy(req, res, busboy);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user