feat: 更新安装应用逻辑,添加用户 ID 处理,优化应用路径检查

This commit is contained in:
xiongxiao
2026-03-27 00:22:19 +08:00
committed by cnb
parent 867dcce83d
commit bb1141aea4
4 changed files with 21 additions and 15 deletions

View File

@@ -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 = () => {