diff --git a/src/routes-simple/resources/chunk.ts b/src/routes-simple/resources/chunk.ts index c3db78c..5d1f7b2 100644 --- a/src/routes-simple/resources/chunk.ts +++ b/src/routes-simple/resources/chunk.ts @@ -147,7 +147,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => { // Notify the app const r = await app.call({ path: 'app', - key: 'detect-version-list', + key: 'detectVersionList', payload: { token: token, data: { diff --git a/src/routes/app-manager/admin/mv-user-app.ts b/src/routes/app-manager/admin/mv-user-app.ts new file mode 100644 index 0000000..0d3ee0c --- /dev/null +++ b/src/routes/app-manager/admin/mv-user-app.ts @@ -0,0 +1,13 @@ +import { app } from '@/app.ts'; +import { AppModel, AppListModel } from '../module/index.ts'; +export const mvAppFromUserAToUserB = async (userA: string, userB: string) => { + const appList = await AppModel.findAll({ + where: { + user: userA, + }, + }); + for (const app of appList) { + app.user = userB; + await app.save(); + } +}; diff --git a/src/routes/app-manager/export.ts b/src/routes/app-manager/export.ts index 5c8e428..3afcbc3 100644 --- a/src/routes/app-manager/export.ts +++ b/src/routes/app-manager/export.ts @@ -2,7 +2,7 @@ import { app } from '@/app.ts'; export const callDetectAppVersion = async ({ appKey, version, username }: { appKey: string; version: string; username: string }, token: string) => { const res = await app.call({ path: 'app', - key: 'detect-version-list', + key: 'detectVersionList', payload: { token: token, data: { appKey, version, username }, diff --git a/src/routes/app-manager/list.ts b/src/routes/app-manager/list.ts index e2d5409..58c4b6c 100644 --- a/src/routes/app-manager/list.ts +++ b/src/routes/app-manager/list.ts @@ -299,7 +299,7 @@ app app .route({ path: 'app', - key: 'detect-version-list', + key: 'detectVersionList', description: '检测版本列表,minio中的数据自己上传后,根据版本信息,进行替换', middleware: ['auth'], }) @@ -332,7 +332,7 @@ app let appListFiles = appList.data?.files || []; const needAddFiles = newFiles.map((item) => { const findFile = appListFiles.find((appListFile) => appListFile.name === item.name); - if (findFile && findFile.path === item.path) { + if (findFile && findFile.name === item.name) { return { ...findFile, ...item }; } return item; diff --git a/src/routes/app-manager/user-app.ts b/src/routes/app-manager/user-app.ts index 1748394..b73b57a 100644 --- a/src/routes/app-manager/user-app.ts +++ b/src/routes/app-manager/user-app.ts @@ -39,20 +39,21 @@ app if (!id && !key) { ctx.throw(500, 'id is required'); } + let am: AppModel; if (id) { - const am = await AppModel.findByPk(id); + am = await AppModel.findByPk(id); if (!am) { ctx.throw(500, 'app not found'); } - ctx.body = am; } else { - const am = await AppModel.findOne({ where: { key, uid: tokenUser.id } }); + am = await AppModel.findOne({ where: { key, uid: tokenUser.id } }); if (!am) { ctx.throw(500, 'app not found'); } - ctx.body = am; } + ctx.body = am; + return ctx; }) .addTo(app); @@ -66,11 +67,25 @@ app .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; - const { data, id, ...rest } = ctx.query.data; + const { data, id, user, ...rest } = ctx.query.data; if (id) { const app = await AppModel.findByPk(id); if (app) { const newData = { ...app.data, ...data }; + if (app.user !== tokenUser.username) { + rest.user = tokenUser.username; + let files = newData?.files || []; + if (files.length > 0) { + files = files.map((item) => { + const paths = item.path.split('/'); + return { + ...item, + path: newData.user + '/' + paths.slice(1).join('/'), + }; + }); + } + newData.files = files; + } const newApp = await app.update({ data: newData, ...rest }); ctx.body = newApp; if (app.status !== 'running') { diff --git a/src/routes/app-manager/util.ts b/src/routes/app-manager/util.ts index dcdda04..b8f4596 100644 --- a/src/routes/app-manager/util.ts +++ b/src/routes/app-manager/util.ts @@ -3,13 +3,29 @@ import { App } from '@kevisual/router'; type Opts = { prefix: string; }; +/** + * fix path + * @param data + * @param prefix + * @param opts + * @returns + */ export const prefixFix = (data: any, prefix: string, opts?: Opts) => { const len = prefix.length || 0; + console.log('prefixFix', prefix, opts?.prefix); + const wrapperPrefix = (path: string, prefix: string) => { + if (prefix) { + return prefix + '/' + path; + } + return path; + }; if (data.data.files) { data.data.files = data.data.files.map((item) => { + const paths = item.path.split('/').filter((item) => item); return { ...item, - path: item.path.slice(len + 1), + path: wrapperPrefix(paths.slice(1).join('/'), ''), + origin: item.path, }; }); } diff --git a/src/routes/user/admin/user.ts b/src/routes/user/admin/user.ts index c05b20f..42dd260 100644 --- a/src/routes/user/admin/user.ts +++ b/src/routes/user/admin/user.ts @@ -3,6 +3,8 @@ import { User } from '@/models/user.ts'; import { nanoid } from 'nanoid'; import { CustomError } from '@kevisual/router'; import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts'; +// import { mvAppFromUserAToUserB } from '@/routes/app-manager/admin/mv-user-app.ts'; + export const checkUsername = (username: string) => { if (username.length > 30) { throw new CustomError(400, 'Username cannot be too long'); @@ -44,6 +46,7 @@ app // 迁移文件数据 await backupUserA(oldName, user.id); // 备份文件数据 await mvUserAToUserB(oldName, newName, true); // 迁移文件数据 + // await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据 } catch (error) { console.error('迁移文件数据失败', error); ctx.throw(500, 'Failed to change username');