feat: add dynamic app

This commit is contained in:
2024-11-22 02:13:12 +08:00
parent d71a574613
commit 40f42ca89b
12 changed files with 358 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ import { useFileStore } from '@abearxiong/use-file-store';
import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { hash } from 'crypto';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.post('/api/micro-app/upload', async (req, res) => {
@@ -18,10 +19,11 @@ router.post('/api/micro-app/upload', async (req, res) => {
//
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({
multiples: true, // 支持多文件上传
multiples: false, // 支持多文件上传
uploadDir: cacheFilePath, // 上传文件存储目录
allowEmptyFiles: true, // 允许空
minFileSize: 0, // 最小文件大小
maxFiles: 1, // 最大文件数量
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
keepExtensions: true, // 保留文件
hashAlgorithm: 'md5', // 文件哈希算法
@@ -52,31 +54,23 @@ router.post('/api/micro-app/upload', async (req, res) => {
fs.unlinkSync(file.filepath);
});
};
let appKey, version;
const { appKey: _appKey, version: _version } = fields;
let appKey, collection;
const { appKey: _appKey, collection: _collecion } = fields;
if (Array.isArray(_appKey)) {
appKey = _appKey?.[0];
} else {
appKey = _appKey;
}
if (Array.isArray(_version)) {
version = _version?.[0];
if (Array.isArray(_collecion)) {
collection = _collecion?.[0];
} else {
version = _version;
collection = _collecion;
}
appKey = appKey || 'micro-app';
// if (!appKey) {
// res.end(error('appKey is required'));
// clearFiles();
// return;
// }
// if (!version) {
// res.end(error('version is required'));
// clearFiles();
// return;
// }
console.log('Appkey', appKey, version);
collection = parseIfJson(collection);
appKey = appKey || 'micro-app';
console.log('Appkey', appKey);
console.log('collection', collection);
// 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
const uploadResults = [];
@@ -86,7 +80,7 @@ router.post('/api/micro-app/upload', async (req, res) => {
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `/private/${tokenUser.username}/${appKey}/${relativePath}`;
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
@@ -97,17 +91,20 @@ router.post('/api/micro-app/upload', async (req, res) => {
uploadResults.push({
name: relativePath,
path: minioPath,
hash: file.hash,
size: file.size,
});
fs.unlinkSync(tempPath); // 删除临时文件
}
// 受控
const r = await app.call({
path: 'micro-app',
key: 'publish',
key: 'upload',
payload: {
token: token,
data: {
appKey,
collection,
files: uploadResults,
},
},
@@ -122,3 +119,10 @@ router.post('/api/micro-app/upload', async (req, res) => {
res.end(JSON.stringify(data));
});
});
function parseIfJson(collection: any): any {
try {
return JSON.parse(collection);
} catch (e) {
return collection;
}
}