add share public
This commit is contained in:
parent
633eee4bee
commit
3cf26e3eed
@ -1 +1,3 @@
|
||||
import './list.ts';
|
||||
|
||||
import './post.ts'
|
110
src/routes/app-manager/public/post.ts
Normal file
110
src/routes/app-manager/public/post.ts
Normal file
@ -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);
|
Loading…
x
Reference in New Issue
Block a user