diff --git a/src/routes/app-manager/public/index.ts b/src/routes/app-manager/public/index.ts index 83ec5cd..43ff7f1 100644 --- a/src/routes/app-manager/public/index.ts +++ b/src/routes/app-manager/public/index.ts @@ -1 +1,3 @@ import './list.ts'; + +import './post.ts' \ No newline at end of file diff --git a/src/routes/app-manager/public/post.ts b/src/routes/app-manager/public/post.ts new file mode 100644 index 0000000..e4d39dd --- /dev/null +++ b/src/routes/app-manager/public/post.ts @@ -0,0 +1,110 @@ +import { app } from '@/app.ts'; +import { AppModel } from '../module/index.ts'; +import { AppListModel } from '../module/index.ts'; +import { oss } from '@/app.ts'; +import { User } from '@/models/user.ts'; +import { permission } from 'process'; +import { customAlphabet } from 'nanoid'; +import dayjs from 'dayjs'; + +const letter = 'abcdefghijklmnopqrstuvwxyz'; +const number = '0123456789'; +const randomId = customAlphabet(letter + number, 16); +const getShareUser = async () => { + const shareUser = await User.findOne({ + where: { + username: 'share', + }, + }); + return shareUser?.id || ''; +}; +app + .route({ + path: 'app', + key: 'public-upload-html', + middleware: ['auth-can'], + }) + .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser || {}; + + let uid = tokenUser?.id; + let username = tokenUser?.username; + if (!uid) { + uid = await getShareUser(); + username = 'share'; + } + if (!uid) { + ctx.throw(403, 'No permission to upload'); + } + let { title, description, version = '1.0.0', key, content } = ctx.query.data || {}; + if (!content) { + ctx.throw(400, 'Content is required'); + } + if (!key) { + key = randomId(16); + } + if (!title) { + const day = dayjs().format('YYYY-MM-DD HH:mm'); + const time = dayjs().format('YYYY-MM-DD HH:mm:ss'); + title = `分享应用 - ${day}`; + + description = `创建于 ${time},分享应用,key: ${key},用户: ${username}`; + if (!tokenUser) { + description = description + `,会自动删除,过期时间为30天`; + } + } + const urlPath = `${username}/${key}/${version}/index.html`; + await oss.putObject(urlPath, content, { + 'Content-Type': 'text/html; charset=utf-8', + 'app-source': 'user-app', + 'Cache-Control': 'no-cache', + }); + const files = [ + { + name: 'index.html', + path: urlPath, + }, + ]; + const appModel = await AppModel.create({ + title, + description, + version, + key, + user: username, + uid, + proxy: true, + data: { + delete: 'share', + permission: { + share: 'public', + }, + files: files, + }, + }); + const appVersionModel = await AppListModel.create({ + data: { + files: files, + }, + version: appModel.version, + key: appModel.key, + uid: appModel.uid, + }); + + ctx.body = { + url: `/share/${key}/`, + appModel: { + id: appModel.id, + title: appModel.title, + description: appModel.description, + version: appModel.version, + data: appModel.data, + }, + appVersionModel: { + id: appVersionModel.id, + version: appVersionModel.version, + key: appVersionModel.key, + data: appVersionModel.data, + }, + }; + }) + .addTo(app);