feat: add publish app
This commit is contained in:
69
src/routes/page/publish.ts
Normal file
69
src/routes/page/publish.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { app } from '../../app.ts';
|
||||
import { PageModel } from './models/index.ts';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { ContainerModel } from '../container/models/index.ts';
|
||||
import { Op } from 'sequelize';
|
||||
import { AppListModel, AppModel } from '../app-manager/index.ts';
|
||||
import { cachePage } from './module/cache-file.ts';
|
||||
import _ from 'lodash';
|
||||
import semver from 'semver';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'page',
|
||||
key: 'publish',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { data, id, publish, ...rest } = ctx.query.data;
|
||||
let needUpdate = { ...rest };
|
||||
if (data) {
|
||||
needUpdate = { ...needUpdate, data };
|
||||
}
|
||||
if (publish) {
|
||||
needUpdate = { ...needUpdate, publish };
|
||||
}
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const page = await PageModel.findByPk(id);
|
||||
if (!page) {
|
||||
throw new CustomError('page not found');
|
||||
}
|
||||
await page.update(needUpdate);
|
||||
|
||||
const _publish = page.publish || {};
|
||||
if (!_publish.key) {
|
||||
throw new CustomError('publish key is required');
|
||||
}
|
||||
try {
|
||||
const { key, description } = _publish;
|
||||
const version = _publish.version || '0.0.0';
|
||||
let app = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
|
||||
if (!app) {
|
||||
app = await AppModel.create({ title: key, key, uid: tokenUser.id, description, user: tokenUser.username });
|
||||
}
|
||||
const _version = semver.inc(version, 'patch');
|
||||
let appList = await AppListModel.findOne({ where: { key, version: _version, uid: tokenUser.id } });
|
||||
if (!appList) {
|
||||
appList = await AppListModel.create({ key, version: _version, uid: tokenUser.id, data: {} });
|
||||
}
|
||||
// 上传文件
|
||||
const res = await cachePage(page, { tokenUser, key, version: _version });
|
||||
const appFiles = appList?.data?.files || [];
|
||||
const newFiles = _.uniqBy([...appFiles, ...res], 'name');
|
||||
appList.data = {
|
||||
...appList?.data,
|
||||
files: newFiles,
|
||||
};
|
||||
await appList.save();
|
||||
await page.update({ publish: { ..._publish, id: app.id, version: _version } });
|
||||
ctx.body = page;
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
throw new CustomError(e.message || 'publish error');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
Reference in New Issue
Block a user