feat: 上传资源和下载资源更新

This commit is contained in:
2025-03-20 02:29:26 +08:00
parent 9b1045d456
commit 0179fe73a3
19 changed files with 747 additions and 225 deletions

View File

@@ -8,6 +8,7 @@ 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';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
@@ -36,6 +37,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
console.log('progress-upload', data);
writeEvents(req, data);
});
// 解析上传的文件
@@ -51,7 +53,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
clearFiles();
return;
}
let { appKey, version, username } = getKey(fields, ['appKey', 'version', 'username']);
let { appKey, version, username, directory } = getKey(fields, ['appKey', 'version', 'username', 'directory']);
let uid = tokenUser.id;
if (username) {
const user = await User.getUserByToken(token);
@@ -76,7 +78,12 @@ router.post('/api/s1/resources/upload', async (req, res) => {
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];
const uploadResults = [];
@@ -86,7 +93,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `${username || tokenUser.username}/${appKey}/${version}/${relativePath}`;
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
@@ -99,7 +106,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
path: minioPath,
});
fs.unlinkSync(tempPath); // 删除临时文件
} // 受控
} // 受控
const r = await app.call({
path: 'app',
key: 'uploadFiles',
@@ -120,6 +127,8 @@ router.post('/api/s1/resources/upload', async (req, res) => {
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
});
});