diff --git a/src/routes/micro-app/list.ts b/src/routes/micro-app/list.ts index 04e6d48..468feae 100644 --- a/src/routes/micro-app/list.ts +++ b/src/routes/micro-app/list.ts @@ -4,6 +4,7 @@ import { manager } from './manager-app.ts'; import { selfRestart } from '@/modules/self-restart.ts'; import { AppList } from '../app-manager/module/index.ts'; import { eq, and } from 'drizzle-orm'; +import { UserId } from '../user/modules/user-id.ts'; // curl http://localhost:4002/api/router?path=micro-app&key=deploy // 把对应的应用安装到系统的apps目录下,并解压,然后把配置项写入数据库配置 // key 是应用的唯一标识,和package.json中的key一致,绑定关系 @@ -48,20 +49,21 @@ app } } const { version } = microApp; - const appKey = username + '/' + microApp.key; - const check = await appPathCheck({ appKey }); + const userAppKey = username + '/' + microApp.key; + const uid = await UserId.getUserIdByName(username); + const check = await appPathCheck({ appKey: userAppKey }); let appType: string; if (check) { if (!force) { ctx.throw(400, 'App already exists, please remove it first'); } else { - const app = manager.getAppShowInfo(appKey); + const app = manager.getAppShowInfo(userAppKey); appType = app?.type; - await manager.removeApp(appKey); + await manager.removeApp(userAppKey); } } - const installAppData = await installApp({ appKey, version, needInstallDeps: !!install }); + const installAppData = await installApp({ uid, username, appKey: microApp.key, version, needInstallDeps: !!install }); await manager.add(installAppData.showAppInfo); ctx.body = installAppData; if (appType === 'system-app') { diff --git a/src/routes/micro-app/module/install-app.ts b/src/routes/micro-app/module/install-app.ts index e1f7349..3209eb3 100644 --- a/src/routes/micro-app/module/install-app.ts +++ b/src/routes/micro-app/module/install-app.ts @@ -1,18 +1,21 @@ import { oss } from '@/app.ts'; import { fileIsExist } from '@kevisual/use-config'; import { spawn, spawnSync } from 'child_process'; -import { getFileStat, getMinioList, MinioFile } from '@/routes/file/index.ts'; +import { getMinioList, MinioFile } from '@/routes/file/index.ts'; import fs from 'fs'; import path from 'path'; import { appsPath } from '../lib/index.ts'; import { installAppFromKey } from './manager.ts'; import { Readable } from 'stream'; +import { CustomError } from '@kevisual/router'; export type InstallAppOpts = { needInstallDeps?: boolean; // minio中 appKey?: string; version?: string; + uid?: string; + username?: string; }; /** * 检测路径是否存在 @@ -28,12 +31,13 @@ export const appPathCheck = async (opts: InstallAppOpts) => { return false; }; export const installApp = async (opts: InstallAppOpts) => { - const { needInstallDeps, appKey, version } = opts; - const prefix = `${appKey}/${version}`; + const { needInstallDeps, appKey, version, uid, username } = opts; + const userAppKey = `${username}/${appKey}`; + const prefix = `data/${uid}/${appKey}/${version}`; const pkgPrefix = prefix + '/package.json'; - const stat = await getFileStat(pkgPrefix); + const stat = await oss.statObject(pkgPrefix); if (!stat) { - throw new Error('App not found'); + throw new CustomError({ code: 400, message: 'App not found' }); } const fileList = await getMinioList({ prefix, @@ -41,7 +45,7 @@ export const installApp = async (opts: InstallAppOpts) => { }); for (const file of fileList) { const { name } = file as MinioFile; - const outputPath = path.join(appsPath, appKey, name.replace(prefix, '')); + const outputPath = path.join(appsPath, userAppKey, name.replace(prefix, '')); const dir = path.dirname(outputPath); if (!fileIsExist(dir)) { fs.mkdirSync(dir, { recursive: true }); @@ -55,7 +59,7 @@ export const installApp = async (opts: InstallAppOpts) => { writeStream.on('error', reject); }); } - const directory = path.join(appsPath, appKey); + const directory = path.join(appsPath, userAppKey); if (!fileIsExist(directory)) { fs.mkdirSync(directory, { recursive: true }); } @@ -66,7 +70,7 @@ export const installApp = async (opts: InstallAppOpts) => { console.log('installDeps error, [need manual install deps]', e); } } - return installAppFromKey(appKey); + return installAppFromKey(userAppKey); }; export const checkPnpm = () => { diff --git a/src/routes/micro-app/module/manager.ts b/src/routes/micro-app/module/manager.ts index bccf50c..7f3058d 100644 --- a/src/routes/micro-app/module/manager.ts +++ b/src/routes/micro-app/module/manager.ts @@ -6,7 +6,7 @@ import { appsPath } from '../lib/index.ts'; export const installAppFromKey = async (key: string) => { const directory = path.join(appsPath, key); if (!fileIsExist(directory)) { - throw new Error('App not found'); + throw new Error('FileIsNotExist: App not found'); } const pkgs = path.join(directory, 'package.json'); if (!fileIsExist(pkgs)) { diff --git a/src/test/s3-stat.ts b/src/test/s3-stat.ts index d534fd9..60c6871 100644 --- a/src/test/s3-stat.ts +++ b/src/test/s3-stat.ts @@ -1,4 +1,4 @@ import { oss } from '@/modules/s3.ts' -const stat = await oss.statObject('root/codepod/0.0.3/index.html'); +const stat = await oss.statObject('data/0e700dc8-90dd-41b7-91dd-336ea51de3d2/cnb/0.0.61/package.json'); console.log('Object Stat:', stat); \ No newline at end of file