import { useFileStore } from '@kevisual/use-config/file-store'; import { checkAuth, error, router, writeEvents, getKey } from '../router.ts'; import { IncomingForm } from 'formidable'; import { app, minioClient } from '@/app.ts'; import { bucketName } from '@/modules/minio.ts'; import { getContentType } from '@/utils/get-content-type.ts'; import { User } from '@/models/user.ts'; import fs from 'fs'; import { ConfigModel } from '@/routes/config/models/model.ts'; import { validateDirectory } from './util.ts'; import { pick } from 'lodash-es'; import { getFileStat } from '@/routes/file/index.ts'; import { logger } from '@/logger/index.ts'; const cacheFilePath = useFileStore('cache-file', { needExists: true }); router.get('/api/s1/resources/upload', async (req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Upload API is ready'); }); export const parseIfJson = (data = '{}') => { try { const _data = JSON.parse(data); if (typeof _data === 'object') return _data; return {}; } catch (error) { return {}; } }; router.post('/api/s1/resources/upload/check', async (req, res) => { const { tokenUser, token } = await checkAuth(req, res); if (!tokenUser) { res.end(error('Token is invalid.')); return; } console.log('data', req.url); res.writeHead(200, { 'Content-Type': 'application/json' }); const data = await router.getBody(req); type Data = { appKey: string; version: string; username: string; directory: string; files: { path: string; hash: string }[]; }; let { appKey, version, username, directory, files } = pick(data, ['appKey', 'version', 'username', 'directory', 'files']) as Data; 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')); return; } const _user = await User.findOne({ where: { username } }); uid = _user?.id || ''; } if (!appKey || !version) { res.end(error('appKey and version is required')); } const { code, message } = validateDirectory(directory); if (code !== 200) { res.end(error(message)); return; } type CheckResult = { path: string; stat: any; resourcePath: string; hash: string; uploadHash: string; isUpload?: boolean; }; const checkResult: CheckResult[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; const relativePath = file.path; const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`; let stat = await getFileStat(minioPath, true); const statHash = stat?.etag || ''; checkResult.push({ path: relativePath, uploadHash: file.hash, resourcePath: minioPath, isUpload: statHash === file.hash, stat, hash: statHash, }); } res.end(JSON.stringify({ code: 200, data: checkResult })); }); // /api/s1/resources/upload router.post('/api/s1/resources/upload', async (req, res) => { 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'); // 使用 formi dable 解析 multipart/form-data const form = new IncomingForm({ multiples: true, // 支持多文件上传 uploadDir: cacheFilePath, // 上传文件存储目录 allowEmptyFiles: true, // 允许空 minFileSize: 0, // 最小文件大小 createDirsFromUploads: false, // 根据上传的文件夹结构创建目录 keepExtensions: true, // 保留文件拓展名 hashAlgorithm: 'md5', // 文件哈希算法 }); form.on('progress', (bytesReceived, bytesExpected) => { 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); }); // 解析上传的文件 form.parse(req, async (err, fields, files) => { 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 (err) { logger.error(`Upload error: ${err.message}`); res.end(error(`Upload error: ${err.message}`)); clearFiles(); return; } let { appKey, version, username, directory } = getKey(fields, ['appKey', 'version', 'username', 'directory']); 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 = Array.isArray(files.file) ? files.file : [files.file]; 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 r = await app.call({ path: 'app', key: 'uploadFiles', payload: { token: token, data: { appKey, version, username, files: uploadResults, }, }, }); 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, }, }), ); } }); });