feat: add s1/resources/upload

This commit is contained in:
2025-03-15 11:38:57 +08:00
parent d947043a16
commit 9b1045d456
4 changed files with 154 additions and 3 deletions

View File

@@ -0,0 +1,125 @@
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';
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');
});
// /api/s1/resources/upload
router.post('/api/s1/resources/upload', async (req, res) => {
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return;
// 使用 formidable 解析 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)}%`,
};
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) => {
fs.unlinkSync(file.filepath);
});
};
if (err) {
res.end(error(`Upload error: ${err.message}`));
clearFiles();
return;
}
let { appKey, version, username } = getKey(fields, ['appKey', 'version', 'username']);
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 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 = `${username || tokenUser.username}/${appKey}/${version}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
});
uploadResults.push({
name: relativePath,
path: minioPath,
});
fs.unlinkSync(tempPath); // 删除临时文件
} // 受控
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: r.body,
};
if (r.message) {
data.message = r.message;
}
res.end(JSON.stringify(data));
});
});