temp: add eco

This commit is contained in:
2024-12-03 23:22:39 +08:00
parent eba5e4988e
commit a5d0e66b3c
12 changed files with 149 additions and 93 deletions

View File

@@ -9,6 +9,7 @@ import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { hash } from 'crypto';
import { MicroAppModel } from '@/routes/micro-app/models.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.post('/api/micro-app/upload', async (req, res) => {
@@ -119,6 +120,61 @@ router.post('/api/micro-app/upload', async (req, res) => {
res.end(JSON.stringify(data));
});
});
router.get('/api/micro-app/download/:id', async (req, res) => {
const { id } = req.params;
if (!id) {
res.writeHead(200, { 'Content-Type': 'application/javascript; charset=utf-8' });
res.end(error('Key parameter is required'));
return;
}
if (res.headersSent) return; // 如果响应已发送,不再处理
let tokenUser;
if (!DEV_SERVER) {
const auth = await checkAuth(req, res);
tokenUser = auth.tokenUser;
if (!tokenUser) return;
}
const file = await MicroAppModel.findByPk(id);
if (!DEV_SERVER) {
file.uid !== tokenUser.id && res.end(error('No permission', 403));
return;
}
if (!file) {
res.end(error('File not found'));
return;
}
const objectName = file.data?.file?.path;
const fileName = file.data?.file?.name;
if (!objectName) {
res.end(error('File not found'));
return;
}
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(fileName)}"`);
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
try {
const stream = await minioClient.getObject(bucketName, objectName);
// 捕获流的错误,防止崩溃
stream.on('error', (err) => {
console.error('Error while streaming file:', err);
if (!res.headersSent) {
res.end(error('Error downloading file'));
}
});
stream.pipe(res).on('finish', () => {
console.log(`File download completed: ${id}`);
});
} catch (err) {
console.error('Error during download:', err);
if (!res.headersSent) {
res.end(error('Error downloading file'));
}
}
});
function parseIfJson(collection: any): any {
try {
return JSON.parse(collection);

View File

@@ -18,11 +18,15 @@ app
const file = files[0];
console.log('File', files);
const { path, name, hash, size } = file;
const tags = [];
if (collection?.tags) {
tags.push(...collection.tags);
}
const microApp = await MicroAppModel.create({
title: name,
description: '',
description: collection?.readme || '',
type: 'micro-app',
tags: [],
tags: tags,
data: {
file: {
path,
@@ -52,7 +56,7 @@ app
description: 'Deploy micro app',
})
.define(async (ctx) => {
const { id, key} = ctx.query?.data;
const { id, key } = ctx.query?.data;
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
// const key = 'envision-cli';
// const id = '7c54a6de-9171-4093-926d-67a035042c6c';

View File

@@ -8,9 +8,10 @@ type MicroAppData = {
path: string;
size: number;
hash: string;
name: string;
};
data?: any;
collection?: any;
collection?: any; // 上传的信息汇总
};
export class MicroAppModel extends Model {
declare id: string;

View File

@@ -36,6 +36,11 @@ export const deleteFileAppInfo = async (key: string) => {
fs.rmSync(directory, { recursive: true });
};
/**
* @deprecated
* @param key
* @returns
*/
export const loadApp = async (key: string) => {
const { mainEntry, app } = await loadFileAppInfo(key);
// 1. 查询数据库获取app信息查看是否运行中

View File

@@ -1,4 +1,4 @@
import { getSession } from '@/app.ts';
import { getSession } from '@/modules/neo4j.ts';
export async function fetchData() {
const session = getSession();
try {