feat: add minio list detect

This commit is contained in:
2025-03-13 02:32:28 +08:00
parent 1c820c3083
commit efef48a1b0
8 changed files with 208 additions and 15 deletions

View File

@@ -2,8 +2,8 @@ import { App, CustomError } from '@kevisual/router';
import { AppModel, AppListModel } from './module/index.ts';
import { app, redis } from '@/app.ts';
import _ from 'lodash';
import { prefixFix } from './util.ts';
import { deleteFiles } from '../file/index.ts';
import { getUidByUsername, prefixFix } from './util.ts';
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
import { setExpire } from './revoke.ts';
import { User } from '@/models/user.ts';
app
@@ -67,7 +67,7 @@ app
const newData = { ...app.data, ...data };
const newApp = await app.update({ data: newData, ...rest });
ctx.body = newApp;
setExpire(newApp.id, tokenUser.username);
setExpire(newApp.id, 'test');
} else {
throw new CustomError('app not found');
}
@@ -194,7 +194,7 @@ app
const dataFiles = app.data.files || [];
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
const res = await app.update({ data: { ...app.data, files: newFiles } });
setExpire(app.id, userPrefix);
setExpire(app.id, 'test');
ctx.body = prefixFix(res, userPrefix);
} catch (e) {
console.log('update error', e);
@@ -211,24 +211,26 @@ app
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data;
const { id, username } = ctx.query.data;
if (!id) {
throw new CustomError('id is required');
}
const app = await AppListModel.findByPk(id);
if (!app) {
const uid = await getUidByUsername(app, ctx, username);
const appList = await AppListModel.findByPk(id);
if (!appList) {
throw new CustomError('app not found');
}
const files = app.data.files || [];
const am = await AppModel.findOne({ where: { key: app.key, uid: tokenUser.id } });
const files = appList.data.files || [];
const am = await AppModel.findOne({ where: { key: appList.key, uid: uid } });
if (!am) {
throw new CustomError('app not found');
}
await am.update({ data: { ...am.data, files }, version: app.version });
setExpire(app.key, am.user);
await am.update({ data: { ...am.data, files }, version: appList.version });
setExpire(appList.key, am.user);
ctx.body = {
key: app.key,
version: app.version,
key: appList.key,
version: appList.version,
appManager: am,
user: am.user,
};
@@ -274,3 +276,66 @@ app
return ctx;
})
.addTo(app);
app
.route({
path: 'app',
key: 'get-minio-list',
description: '获取minio列表',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { key, version } = ctx.query?.data || {};
if (!key || !version) {
throw new CustomError('key and version are required');
}
const files = await getMinioListAndSetToAppList({ username: tokenUser.username, appKey: key, version });
ctx.body = files;
})
.addTo(app);
app
.route({
path: 'app',
key: 'detect-version-list',
description: '检测版本列表minio中的数据自己上传后根据版本信息进行替换',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
let { key, version, username } = ctx.query?.data || {};
if (!key || !version) {
throw new CustomError('key and version are required');
}
const uid = await getUidByUsername(app, ctx, username);
const appList = await AppListModel.findOne({ where: { key, version, uid: uid } });
if (!appList) {
throw new CustomError('app not found');
}
const checkUsername = username || tokenUser.username;
const files = await getMinioListAndSetToAppList({ username: checkUsername, appKey: key, version });
const newFiles = files.map((item) => {
return {
name: item.name.replace(`${checkUsername}/${key}/${version}/`, ''),
path: item.name,
};
});
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) {
return findFile;
}
return item;
});
await appList.update({ data: { files: needAddFiles } });
setExpire(appList.id, 'test');
const appModel = await AppModel.findOne({ where: { key, version, uid: uid } });
if (appModel) {
await appModel.update({ data: { files: needAddFiles } });
setExpire(appModel.key, appModel.user);
}
ctx.body = appList;
})
.addTo(app);