feat: add download micro-app for cli

This commit is contained in:
2025-03-11 11:04:10 +08:00
parent 7b25dbdf08
commit 1c820c3083
8 changed files with 186 additions and 155 deletions

View File

@@ -129,18 +129,27 @@ router.get('/api/micro-app/download/:id', async (req, res) => {
res.end(error('Key parameter is required'));
return;
}
const query = new URL(req.url || '', 'http://localhost');
const notNeedToken = query.searchParams.get('notNeedToken') || '';
const fileTitle = query.searchParams.get('title') || '';
if (res.headersSent) return; // 如果响应已发送,不再处理
let tokenUser;
if (!DEV_SERVER) {
if (!DEV_SERVER && !notNeedToken) {
const auth = await checkAuth(req, res);
tokenUser = auth.tokenUser;
if (!tokenUser) return;
}
const file = await MicroAppUploadModel.findByPk(id);
let file: MicroAppUploadModel | null = null;
if (!DEV_SERVER) {
file.uid !== tokenUser.id && res.end(error('No permission', 403));
return;
// file.uid !== tokenUser.id && res.end(error('No permission', 403));
// return;
}
if (fileTitle) {
file = await MicroAppUploadModel.findOne({
where: { title: fileTitle },
});
} else if (id) {
file = await MicroAppUploadModel.findByPk(id);
}
if (!file) {

View File

@@ -5,13 +5,22 @@ export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};
export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
let token = '';
const authroization = req.headers?.['authorization'] as string;
if (!authroization) {
const url = new URL(req.url || '', 'http://localhost');
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (authroization) {
// return resNoPermission();
token = authroization.split(' ')[1];
} else if (url.searchParams.get('token')) {
token = url.searchParams.get('token') || '';
} else {
return resNoPermission();
}
const token = authroization.split(' ')[1];
let tokenUser;
try {
tokenUser = await User.verifyToken(token);

View File

@@ -159,7 +159,7 @@ router.post('/api/app/upload', async (req, res) => {
}
if (username) {
const user = await User.getUserByToken(token);
const has = user.hasUser(username);
const has = await user.hasUser(username, true);
if (!has) {
res.end(error('username is not found'));
clearFiles();

View File

@@ -241,8 +241,15 @@ app
key: 'getApp',
})
.define(async (ctx) => {
const { user, key } = ctx.query.data;
const app = await AppModel.findOne({ where: { user, key } });
const { user, key, id } = ctx.query.data;
let app;
if (id) {
app = await AppModel.findByPk(id);
} else if (user && key) {
app = await AppModel.findOne({ where: { user, key } });
} else {
throw new CustomError('user or key is required');
}
if (!app) {
throw new CustomError('app not found');
}

View File

@@ -27,10 +27,15 @@ app
description: 'Get a single micro app upload',
})
.define(async (ctx) => {
const { id } = ctx.query;
const upload = await MicroAppUploadModel.findOne({
where: { id },
});
const { id, title } = ctx.query;
let upload: MicroAppUploadModel | null = null;
if (id) {
upload = await MicroAppUploadModel.findByPk(id);
} else if (title) {
upload = await MicroAppUploadModel.findOne({
where: { title },
});
}
if (upload) {
ctx.body = upload;
} else {

View File