This commit is contained in:
2026-01-17 14:48:49 +08:00
parent b9b4c993f4
commit 0b5a0557ee
14 changed files with 613 additions and 233 deletions

View File

@@ -1,216 +1 @@
// import Busboy from 'busboy';
// import { simpleRouter } from '../app.ts'
// import http from 'http';
// import path from 'path';
// import { createWriteStream } from 'fs';
// import { checkAuth } from '@/routes/index.ts';
// import { pipeBusboy } from '@/module/assistant/proxy/pipe.ts';
// simpleRouter.get('/client/upload', async (req, res) => {
// if (res.headersSent) return; // 如果响应已发送,不再处理
// res.writeHead(200, { 'Content-Type': 'application/json' });
// res.end(JSON.stringify({ message: 'Upload endpoint reached' }));
// })
// export const error = (msg: string, code = 500) => {
// return JSON.stringify({ code, message: msg });
// };
// export const parseIfJson = (data = '{}') => {
// try {
// const _data = JSON.parse(data);
// if (typeof _data === 'object') return _data;
// return {};
// } catch (error) {
// return {};
// }
// };
// const uploadResources = async (req: http.IncomingMessage, res: http.ServerResponse) => {
// const { tokenUser, token } = await checkAuth(req, res);
// if (!tokenUser) {
// res.end(error('Token is invalid.'));
// return;
// }
// const url = new URL(req.url || '', 'http://localhost');
// const share = !!url.searchParams.get('public');
// const meta = parseIfJson(url.searchParams.get('meta'));
// const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// // 使用 busboy 解析 multipart/form-data
// const busboy = Busboy({ headers: req.headers, preservePath: true });
// const fields: any = {};
// const files: any[] = [];
// const filePromises: Promise<void>[] = [];
// let bytesReceived = 0;
// let bytesExpected = parseInt(req.headers['content-length'] || '0');
// busboy.on('field', (fieldname, value) => {
// fields[fieldname] = value;
// });
// busboy.on('file', (fieldname, fileStream, info) => {
// const { filename, encoding, mimeType } = info;
// const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`);
// const writeStream = createWriteStream(tempPath);
// const filePromise = new Promise<void>((resolve, reject) => {
// fileStream.on('data', (chunk) => {
// bytesReceived += chunk.length;
// if (bytesExpected > 0) {
// const progress = (bytesReceived / bytesExpected) * 100;
// const data = {
// progress: progress.toFixed(2),
// message: `Upload progress: ${progress.toFixed(2)}%`,
// };
// console.log('progress-upload', data);
// writeEvents(req, data);
// }
// });
// fileStream.pipe(writeStream);
// writeStream.on('finish', () => {
// files.push({
// filepath: tempPath,
// originalFilename: filename,
// mimetype: mimeType,
// });
// resolve();
// });
// writeStream.on('error', (err) => {
// reject(err);
// });
// });
// filePromises.push(filePromise);
// });
// busboy.on('finish', async () => {
// // 等待所有文件写入完成
// try {
// await Promise.all(filePromises);
// } catch (err) {
// logger.error(`File write error: ${err.message}`);
// res.end(error(`File write error: ${err.message}`));
// return;
// }
// const clearFiles = () => {
// files.forEach((file) => {
// if (file?.filepath && fs.existsSync(file.filepath)) {
// fs.unlinkSync(file.filepath);
// }
// });
// };
// // 检查是否有文件上传
// if (files.length === 0) {
// res.end(error('files is required'));
// return;
// }
// let { appKey, version, username, directory, description } = getKey(fields, ['appKey', 'version', 'username', 'directory', 'description']);
// let uid = tokenUser.id;
// if (username) {
// const user = await User.getUserByToken(token);
// const has = await user.hasUser(username, true);
// if (!has) {
// res.end(error('username is not found'));
// clearFiles();
// return;
// }
// const _user = await User.findOne({ where: { username } });
// uid = _user?.id || '';
// }
// if (!appKey || !version) {
// const config = await ConfigModel.getUploadConfig({ uid });
// if (config) {
// appKey = config.config?.data?.key || '';
// version = config.config?.data?.version || '';
// }
// }
// if (!appKey || !version) {
// res.end(error('appKey or version is not found, please check the upload config.'));
// clearFiles();
// return;
// }
// const { code, message } = validateDirectory(directory);
// if (code !== 200) {
// res.end(error(message));
// clearFiles();
// return;
// }
// // 逐个处理每个上传的文件
// const uploadedFiles = files;
// logger.info(
// 'upload files',
// uploadedFiles.map((item) => {
// return pick(item, ['filepath', 'originalFilename']);
// }),
// );
// 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 = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
// // 上传到 MinIO 并保留文件夹结构
// const isHTML = relativePath.endsWith('.html');
// const metadata: any = {};
// if (share) {
// metadata.share = 'public';
// }
// Object.assign(metadata, meta);
// await minioClient.fPutObject(bucketName, minioPath, tempPath, {
// 'Content-Type': getContentType(relativePath),
// 'app-source': 'user-app',
// 'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
// ...metadata,
// });
// uploadResults.push({
// name: relativePath,
// path: minioPath,
// });
// fs.unlinkSync(tempPath); // 删除临时文件
// }
// if (!noCheckAppFiles) {
// const _data = { appKey, version, username, files: uploadResults, description, }
// if (_data.description) {
// delete _data.description;
// }
// // 受控
// const r = await app.call({
// path: 'app',
// key: 'uploadFiles',
// payload: {
// token: token,
// data: _data,
// },
// });
// const data: any = {
// code: r.code,
// data: {
// app: r.body,
// upload: uploadResults,
// },
// };
// if (r.message) {
// data.message = r.message;
// }
// console.log('upload data', data);
// res.writeHead(200, { 'Content-Type': 'application/json' });
// res.end(JSON.stringify(data));
// } else {
// res.writeHead(200, { 'Content-Type': 'application/json' });
// res.end(
// JSON.stringify({
// code: 200,
// data: {
// detect: [],
// upload: uploadResults,
// },
// }),
// );
// }
// });
// pipeBusboy(req, res, busboy);
// }
export * from './upload.ts'