remove webpack
This commit is contained in:
@@ -16,6 +16,7 @@ const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||
// -F "description=This is a test upload" \
|
||||
// -F "username=testuser"
|
||||
|
||||
let clients = [];
|
||||
export const uploadMiddleware = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
if (req.method === 'GET' && req.url === '/api/app/upload') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
@@ -104,8 +105,19 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
||||
allowEmptyFiles: true, // 允许空
|
||||
minFileSize: 0, // 最小文件大小
|
||||
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||
keepExtensions: true, // 保留文件
|
||||
hashAlgorithm: 'md5', // 文件哈希算法
|
||||
});
|
||||
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)}%`,
|
||||
};
|
||||
// 向所有连接的客户端推送进度信息
|
||||
clients.forEach((client) => client.write(`${JSON.stringify(data)}\n`));
|
||||
});
|
||||
|
||||
// 解析上传的文件
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) {
|
||||
@@ -192,4 +204,17 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
}
|
||||
if (req.url === '/api/events') {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
});
|
||||
clients.push(res);
|
||||
|
||||
// 移除客户端连接
|
||||
req.on('close', () => {
|
||||
clients = clients.filter((client) => client !== res);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user