This commit is contained in:
2025-03-06 19:20:32 +08:00
parent b5fc5d279e
commit 1ae123e63a
6 changed files with 56 additions and 23 deletions

View File

@@ -287,8 +287,8 @@
// useContextKey('UserModel', () => UserServices);
import { User, UserInit } from '@kevisual/code-center-module';
export { User, UserInit };
import { User, UserInit, UserServices } from '@kevisual/code-center-module/models';
export { User, UserInit, UserServices };
UserInit(null, null, {
alter: true,
logging: true,

View File

@@ -138,8 +138,10 @@ router.post('/api/app/upload', async (req, res) => {
fs.unlinkSync(file.filepath);
});
};
let appKey, version;
const { appKey: _appKey, version: _version } = fields;
let appKey,
version,
username = '';
const { appKey: _appKey, version: _version, username: _username } = fields;
if (Array.isArray(_appKey)) {
appKey = _appKey?.[0];
} else {
@@ -150,6 +152,20 @@ router.post('/api/app/upload', async (req, res) => {
} else {
version = _version;
}
if (Array.isArray(_username)) {
username = _username?.[0];
} else if (_username) {
username = _username;
}
if (username) {
const user = await User.getUserByToken(token);
const has = user.hasUser(username);
if (!has) {
res.end(error('username is not found'));
clearFiles();
return;
}
}
if (!appKey) {
res.end(error('appKey is required'));
clearFiles();
@@ -171,7 +187,7 @@ router.post('/api/app/upload', async (req, res) => {
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `${tokenUser.username}/${appKey}/${version}/${relativePath}`;
const minioPath = `${username || tokenUser.username}/${appKey}/${version}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
@@ -194,6 +210,7 @@ router.post('/api/app/upload', async (req, res) => {
data: {
appKey,
version,
username,
files: uploadResults,
},
},

View File

@@ -5,6 +5,7 @@ import _ from 'lodash';
import { prefixFix } from './util.ts';
import { deleteFiles } from '../file/index.ts';
import { setExpire } from './revoke.ts';
import { User } from '@/models/user.ts';
app
.route({
path: 'app',
@@ -143,19 +144,34 @@ app
.define(async (ctx) => {
try {
const tokenUser = ctx.state.tokenUser;
const { appKey, files, version } = ctx.query.data;
const { appKey, files, version, username } = ctx.query.data;
if (!appKey) {
throw new CustomError('appKey is required');
}
if (!files || !files.length) {
throw new CustomError('files is required');
}
let am = await AppModel.findOne({ where: { key: appKey, uid: tokenUser.id } });
let uid = tokenUser.id;
let userPrefix = tokenUser.username;
if (username) {
try {
const _user = await User.getUserByToken(ctx.query.token);
if (_user.hasUser(username)) {
const upUser = await User.findOne({ where: { username } });
uid = upUser.id;
userPrefix = username;
}
} catch (e) {
console.log('getUserByToken error', e);
throw new CustomError('user not found');
}
}
let am = await AppModel.findOne({ where: { key: appKey, uid } });
if (!am) {
am = await AppModel.create({
user: tokenUser.username,
user: userPrefix,
key: appKey,
uid: tokenUser.id,
uid,
version: '0.0.0',
title: appKey,
data: {
@@ -163,13 +179,13 @@ app
},
});
}
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: uid } });
if (!app) {
// throw new CustomError('app not found');
app = await AppListModel.create({
key: appKey,
version,
uid: tokenUser.id,
uid: uid,
data: {
files: [],
},
@@ -178,8 +194,8 @@ app
const dataFiles = app.data.files || [];
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
const res = await app.update({ data: { ...app.data, files: newFiles } });
setExpire(app.id, 'test');
ctx.body = prefixFix(res, tokenUser.username);
setExpire(app.id, userPrefix);
ctx.body = prefixFix(res, userPrefix);
} catch (e) {
console.log('update error', e);
throw new CustomError(e.message);
@@ -214,7 +230,7 @@ app
key: app.key,
version: app.version,
appManager: am,
user: am.user
user: am.user,
};
})
.addTo(app);

View File

@@ -1,4 +1,4 @@
import { User } from '@/models/user.ts';
import { UserServices } from '@/models/user.ts';
import { app } from '@/app.ts';
@@ -12,7 +12,7 @@ app
if (!password) {
ctx.throw(500, 'root password are required');
}
const res = await User.initializeUser(password);
const res = await UserServices.initializeUser(password);
ctx.body = res;
})
.addTo(app);
@@ -28,7 +28,7 @@ app
if (!username && username.startsWith('demo')) {
ctx.throw(500, 'username are required, and must start with demo');
}
const res = await User.createDemoUser(username, password);
const res = await UserServices.createDemoUser(username, password);
ctx.body = res;
})
.addTo(app);