248 lines
6.1 KiB
TypeScript
248 lines
6.1 KiB
TypeScript
import { App, CustomError } from '@abearxiong/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 { setExpire } from './revoke.ts';
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'list',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const data = ctx.query.data || {};
|
|
if (!data.key) {
|
|
throw new CustomError('key is required');
|
|
}
|
|
const list = await AppListModel.findAll({
|
|
order: [['updatedAt', 'DESC']],
|
|
where: {
|
|
uid: tokenUser.id,
|
|
key: data.key,
|
|
},
|
|
logging: false,
|
|
});
|
|
ctx.body = list.map((item) => prefixFix(item, tokenUser.username));
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'get',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
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 = prefixFix(am, tokenUser.username);
|
|
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;
|
|
}
|
|
|
|
if (!rest.key) {
|
|
throw new CustomError('key is required');
|
|
}
|
|
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');
|
|
}
|
|
const am = await AppModel.findOne({ where: { key: app.key, uid: app.uid } });
|
|
if (!am) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
if (am.version === app.version) {
|
|
throw new CustomError('app is published');
|
|
}
|
|
const files = app.data.files || [];
|
|
if (files.length > 0) {
|
|
await deleteFiles(files.map((item) => item.path));
|
|
}
|
|
await app.destroy({
|
|
force: true,
|
|
});
|
|
ctx.body = 'success';
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'canUploadFiles',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const { appKey, version } = ctx.query.data;
|
|
if (!appKey) {
|
|
throw new CustomError('appKey is required');
|
|
}
|
|
const app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
|
|
if (!app) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
ctx.body = app;
|
|
})
|
|
.addTo(app);
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'uploadFiles',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
try {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const { appKey, files, version } = ctx.query.data;
|
|
if (!appKey) {
|
|
throw new CustomError('appKey is required');
|
|
}
|
|
if (!files || !files.length) {
|
|
throw new CustomError('files is required');
|
|
}
|
|
let am = await AppModel.findOne({ where: { key: appKey, uid: tokenUser.id } });
|
|
if (!am) {
|
|
am = await AppModel.create({
|
|
user: tokenUser.username,
|
|
key: appKey,
|
|
uid: tokenUser.id,
|
|
version: '0.0.0',
|
|
title: appKey,
|
|
data: {
|
|
files: [],
|
|
},
|
|
});
|
|
}
|
|
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
|
|
if (!app) {
|
|
// throw new CustomError('app not found');
|
|
app = await AppListModel.create({
|
|
key: appKey,
|
|
version,
|
|
uid: tokenUser.id,
|
|
data: {
|
|
files: [],
|
|
},
|
|
});
|
|
}
|
|
const dataFiles = app.data.files || [];
|
|
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
|
|
const res = await app.update({ data: { ...app.data, files: newFiles } });
|
|
|
|
ctx.body = prefixFix(res, tokenUser.username);
|
|
} catch (e) {
|
|
console.log('update error', e);
|
|
throw new CustomError(e.message);
|
|
}
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'publish',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const { id } = ctx.query.data;
|
|
if (!id) {
|
|
throw new CustomError('id is required');
|
|
}
|
|
const app = await AppListModel.findByPk(id);
|
|
if (!app) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
const files = app.data.files || [];
|
|
const am = await AppModel.findOne({ where: { key: app.key, uid: tokenUser.id } });
|
|
if (!am) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
await am.update({ data: { ...am.data, files }, version: app.version });
|
|
setExpire(app.key, am.user);
|
|
ctx.body = 'success';
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'getApp',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { user, key } = ctx.query.data;
|
|
const app = await AppModel.findOne({ where: { user, key } });
|
|
if (!app) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
ctx.body = app;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'app',
|
|
key: 'getDomainApp',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { domain } = ctx.query.data;
|
|
// const query = {
|
|
// }
|
|
const app = await AppModel.findOne({ where: { domain } });
|
|
if (!app) {
|
|
throw new CustomError('app not found');
|
|
}
|
|
ctx.body = app;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|