feat: 新增app管理和文件管理
This commit is contained in:
2
src/routes/app-manager/index.ts
Normal file
2
src/routes/app-manager/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import './list.ts';
|
||||
import './user-app.ts';
|
||||
89
src/routes/app-manager/list.ts
Normal file
89
src/routes/app-manager/list.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { AppModel, AppListModel } from './module/index.ts';
|
||||
import { app } from '@/app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const list = await AppListModel.findAll({
|
||||
order: [['updatedAt', 'DESC']],
|
||||
where: {
|
||||
uid: tokenUser.id,
|
||||
},
|
||||
});
|
||||
ctx.body = list;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const am = await AppListModel.findByPk(id);
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = am;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { data, id, ...rest } = ctx.query.data;
|
||||
if (id) {
|
||||
const app = await AppListModel.findByPk(id);
|
||||
if (app) {
|
||||
const newData = { ...app.data, ...data };
|
||||
const newApp = await app.update({ data: newData, ...rest });
|
||||
ctx.body = newApp;
|
||||
} else {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
return;
|
||||
}
|
||||
const app = await AppListModel.create({ data, ...rest, uid: tokenUser.id });
|
||||
ctx.body = app;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const app = await AppListModel.findByPk(id);
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
await app.destroy();
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
57
src/routes/app-manager/module/app-list.ts
Normal file
57
src/routes/app-manager/module/app-list.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { sequelize } from '../../../modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
import { AppData, AppType } from './app.ts';
|
||||
|
||||
export type AppList = Partial<InstanceType<typeof AppListModel>>;
|
||||
|
||||
/**
|
||||
* APP List 管理
|
||||
*/
|
||||
export class AppListModel extends Model {
|
||||
declare id: string;
|
||||
declare data: AppData;
|
||||
declare version: string;
|
||||
declare appType: AppType;
|
||||
declare type: string;
|
||||
declare uid: string;
|
||||
}
|
||||
|
||||
AppListModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: 'id',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
version: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
appType: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'kv_app_list',
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
AppListModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('AppListModel sync', e);
|
||||
});
|
||||
71
src/routes/app-manager/module/app.ts
Normal file
71
src/routes/app-manager/module/app.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { sequelize } from '../../../modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
|
||||
export interface AppData {
|
||||
files: { name: string; path: string }[];
|
||||
}
|
||||
export type AppType = 'web-single' | 'web-module';
|
||||
|
||||
export type App = Partial<InstanceType<typeof AppModel>>;
|
||||
|
||||
/**
|
||||
* APP 管理
|
||||
*/
|
||||
export class AppModel extends Model {
|
||||
declare id: string;
|
||||
declare data: AppData;
|
||||
declare version: string;
|
||||
declare domain: string;
|
||||
declare appType: string;
|
||||
declare key: string;
|
||||
declare type: string;
|
||||
declare uid: string;
|
||||
declare user: string;
|
||||
}
|
||||
AppModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: 'id',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
version: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
domain: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
appType: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'kv_app',
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
AppModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('AppModel sync', e);
|
||||
});
|
||||
2
src/routes/app-manager/module/index.ts
Normal file
2
src/routes/app-manager/module/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './app-list.ts';
|
||||
export * from './app.ts';
|
||||
88
src/routes/app-manager/user-app.ts
Normal file
88
src/routes/app-manager/user-app.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { AppModel, AppListModel } from './module/index.ts';
|
||||
import { app } from '@/app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user-app',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const list = await AppModel.findAll({
|
||||
order: [['updatedAt', 'DESC']],
|
||||
where: {
|
||||
uid: tokenUser.id,
|
||||
},
|
||||
});
|
||||
ctx.body = list;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user-app',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const am = await AppModel.findByPk(id);
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = am;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user-app',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { data, id, ...rest } = ctx.query.data;
|
||||
if (id) {
|
||||
const app = await AppModel.findByPk(id);
|
||||
if (app) {
|
||||
const newData = { ...app.data, ...data };
|
||||
const newApp = await app.update({ data: newData, ...rest });
|
||||
ctx.body = newApp;
|
||||
} else {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
return;
|
||||
}
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const app = await AppModel.create({ data, ...rest, uid: tokenUser.id });
|
||||
ctx.body = app;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user-app',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const am = await AppModel.findByPk(id);
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
await am.destroy();
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
1
src/routes/file/index.ts
Normal file
1
src/routes/file/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
30
src/routes/file/list.ts
Normal file
30
src/routes/file/list.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { getMinioList } from './module/get-minio-list.ts';
|
||||
import path from 'path';
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'file',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data || {};
|
||||
const prefixBase = '/' + tokenUser.username;
|
||||
const handlePrefix = (prefix: string) => {
|
||||
// 清理所有的 '..'
|
||||
if (prefix.includes('..')) {
|
||||
throw new CustomError('invalid prefix');
|
||||
}
|
||||
return prefix;
|
||||
};
|
||||
const _prefix = handlePrefix(data.prefix);
|
||||
const prefix = path.join(prefixBase, './', _prefix);
|
||||
const recursive = data.recursive;
|
||||
const list = await getMinioList({ prefix: prefix.slice(1), recursive: recursive });
|
||||
ctx.body = list;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
43
src/routes/file/module/get-minio-list.ts
Normal file
43
src/routes/file/module/get-minio-list.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
|
||||
type MinioListOpt = {
|
||||
prefix: string;
|
||||
recursive?: boolean;
|
||||
};
|
||||
type MinioFile = {
|
||||
name: string;
|
||||
size: number;
|
||||
lastModified: Date;
|
||||
etag: string;
|
||||
};
|
||||
type MinioDirectory = {
|
||||
prefix: string;
|
||||
size: number;
|
||||
};
|
||||
type MinioList = (MinioFile | MinioDirectory)[];
|
||||
export const getMinioList = async (opts: MinioListOpt): Promise<MinioList> => {
|
||||
const prefix = opts.prefix;
|
||||
const recursive = opts.recursive ?? false;
|
||||
return await new Promise((resolve, reject) => {
|
||||
let res: any[] = [];
|
||||
let hasError = false;
|
||||
minioClient
|
||||
.listObjectsV2(bucketName, prefix, recursive)
|
||||
.on('data', (data) => {
|
||||
res.push(data);
|
||||
})
|
||||
.on('error', (err) => {
|
||||
console.error('minio error', opts.prefix, err);
|
||||
hasError = true;
|
||||
})
|
||||
.on('end', () => {
|
||||
if (hasError) {
|
||||
reject();
|
||||
return;
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -15,3 +15,7 @@ import './chat-prompt/index.ts';
|
||||
import './chat-history/index.ts';
|
||||
|
||||
import './github/index.ts';
|
||||
|
||||
import './app-manager/index.ts';
|
||||
|
||||
import './file/index.ts';
|
||||
|
||||
Reference in New Issue
Block a user