feat: add publish app

This commit is contained in:
2024-10-07 22:28:21 +08:00
parent 8beb65e637
commit 0fc580aa38
17 changed files with 1393 additions and 722 deletions

View File

@@ -1,2 +1,4 @@
import './list.ts';
import './user-app.ts';
export * from './module/index.ts';

View File

@@ -1,6 +1,9 @@
import { CustomError } from '@abearxiong/router';
import { App, CustomError } from '@abearxiong/router';
import { AppModel, AppListModel } from './module/index.ts';
import { app } from '@/app.ts';
import { app, redis } from '@/app.ts';
import _ from 'lodash';
import { prefixFix } from './util.ts';
import { deleteFiles } from '../file/index.ts';
app
.route({
@@ -21,7 +24,7 @@ app
key: data.key,
},
});
ctx.body = list;
ctx.body = list.map((item) => prefixFix(item, tokenUser.username));
return ctx;
})
.addTo(app);
@@ -33,6 +36,7 @@ app
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
@@ -41,7 +45,7 @@ app
if (!am) {
throw new CustomError('app not found');
}
ctx.body = am;
ctx.body = prefixFix(am, tokenUser.username);
return ctx;
})
.addTo(app);
@@ -91,6 +95,17 @@ app
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,
});
@@ -98,3 +113,106 @@ app
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) => {
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 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);
})
.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 });
//
const keys = await redis.keys('user:app:exist:*');
console.log('keys', keys);
const expireKey = 'user:app:exist:' + `${app.key}:${am.user}`;
console.log('expireKey', expireKey);
await redis.set(expireKey, 'v', 'EX', 2);
await new Promise((resolve) => setTimeout(resolve, 2100));
const keys2 = await redis.keys('user:app:exist:*');
console.log('keys2', keys2);
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);

View File

@@ -28,15 +28,26 @@ app
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
const { key } = ctx.query.data || {};
if (!id && !key) {
throw new CustomError('id is required');
}
const am = await AppModel.findByPk(id);
if (!am) {
throw new CustomError('app not found');
if (id) {
const am = await AppModel.findByPk(id);
if (!am) {
throw new CustomError('app not found');
}
ctx.body = am;
} else {
const am = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
if (!am) {
throw new CustomError('app not found');
}
ctx.body = am;
}
ctx.body = am;
return ctx;
})
.addTo(app);
@@ -87,6 +98,7 @@ app
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
@@ -95,7 +107,9 @@ app
if (!am) {
throw new CustomError('app not found');
}
await am.destroy();
const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } });
await am.destroy({ force: true });
await Promise.all(list.map((item) => item.destroy({ force: true })));
return ctx;
})
.addTo(app);

View File

@@ -0,0 +1,15 @@
type Opts = {
prefix: string;
};
export const prefixFix = (data: any, prefix: string, opts?: Opts) => {
const len = prefix.length || 0;
if (data.data.files) {
data.data.files = data.data.files.map((item) => {
return {
...item,
path: item.path.slice(len + 1),
};
});
}
return data;
};