fix: change version name

This commit is contained in:
xion 2025-04-03 21:23:17 +08:00
parent d97053a443
commit 4aaf791801
7 changed files with 57 additions and 10 deletions

View File

@ -147,7 +147,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
// Notify the app // Notify the app
const r = await app.call({ const r = await app.call({
path: 'app', path: 'app',
key: 'detect-version-list', key: 'detectVersionList',
payload: { payload: {
token: token, token: token,
data: { data: {

View File

@ -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();
}
};

View File

@ -2,7 +2,7 @@ import { app } from '@/app.ts';
export const callDetectAppVersion = async ({ appKey, version, username }: { appKey: string; version: string; username: string }, token: string) => { export const callDetectAppVersion = async ({ appKey, version, username }: { appKey: string; version: string; username: string }, token: string) => {
const res = await app.call({ const res = await app.call({
path: 'app', path: 'app',
key: 'detect-version-list', key: 'detectVersionList',
payload: { payload: {
token: token, token: token,
data: { appKey, version, username }, data: { appKey, version, username },

View File

@ -299,7 +299,7 @@ app
app app
.route({ .route({
path: 'app', path: 'app',
key: 'detect-version-list', key: 'detectVersionList',
description: '检测版本列表minio中的数据自己上传后根据版本信息进行替换', description: '检测版本列表minio中的数据自己上传后根据版本信息进行替换',
middleware: ['auth'], middleware: ['auth'],
}) })
@ -332,7 +332,7 @@ app
let appListFiles = appList.data?.files || []; let appListFiles = appList.data?.files || [];
const needAddFiles = newFiles.map((item) => { const needAddFiles = newFiles.map((item) => {
const findFile = appListFiles.find((appListFile) => appListFile.name === item.name); 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 { ...findFile, ...item };
} }
return item; return item;

View File

@ -39,20 +39,21 @@ app
if (!id && !key) { if (!id && !key) {
ctx.throw(500, 'id is required'); ctx.throw(500, 'id is required');
} }
let am: AppModel;
if (id) { if (id) {
const am = await AppModel.findByPk(id); am = await AppModel.findByPk(id);
if (!am) { if (!am) {
ctx.throw(500, 'app not found'); ctx.throw(500, 'app not found');
} }
ctx.body = am;
} else { } else {
const am = await AppModel.findOne({ where: { key, uid: tokenUser.id } }); am = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
if (!am) { if (!am) {
ctx.throw(500, 'app not found'); ctx.throw(500, 'app not found');
} }
ctx.body = am;
} }
ctx.body = am;
return ctx; return ctx;
}) })
.addTo(app); .addTo(app);
@ -66,11 +67,25 @@ app
.define(async (ctx) => { .define(async (ctx) => {
const tokenUser = ctx.state.tokenUser; const tokenUser = ctx.state.tokenUser;
const { data, id, ...rest } = ctx.query.data; const { data, id, user, ...rest } = ctx.query.data;
if (id) { if (id) {
const app = await AppModel.findByPk(id); const app = await AppModel.findByPk(id);
if (app) { if (app) {
const newData = { ...app.data, ...data }; 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 }); const newApp = await app.update({ data: newData, ...rest });
ctx.body = newApp; ctx.body = newApp;
if (app.status !== 'running') { if (app.status !== 'running') {

View File

@ -3,13 +3,29 @@ import { App } from '@kevisual/router';
type Opts = { type Opts = {
prefix: string; prefix: string;
}; };
/**
* fix path
* @param data
* @param prefix
* @param opts
* @returns
*/
export const prefixFix = (data: any, prefix: string, opts?: Opts) => { export const prefixFix = (data: any, prefix: string, opts?: Opts) => {
const len = prefix.length || 0; 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) { if (data.data.files) {
data.data.files = data.data.files.map((item) => { data.data.files = data.data.files.map((item) => {
const paths = item.path.split('/').filter((item) => item);
return { return {
...item, ...item,
path: item.path.slice(len + 1), path: wrapperPrefix(paths.slice(1).join('/'), ''),
origin: item.path,
}; };
}); });
} }

View File

@ -3,6 +3,8 @@ import { User } from '@/models/user.ts';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { CustomError } from '@kevisual/router'; import { CustomError } from '@kevisual/router';
import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts'; 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) => { export const checkUsername = (username: string) => {
if (username.length > 30) { if (username.length > 30) {
throw new CustomError(400, 'Username cannot be too long'); throw new CustomError(400, 'Username cannot be too long');
@ -44,6 +46,7 @@ app
// 迁移文件数据 // 迁移文件数据
await backupUserA(oldName, user.id); // 备份文件数据 await backupUserA(oldName, user.id); // 备份文件数据
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据 await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
} catch (error) { } catch (error) {
console.error('迁移文件数据失败', error); console.error('迁移文件数据失败', error);
ctx.throw(500, 'Failed to change username'); ctx.throw(500, 'Failed to change username');