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

@ -57,7 +57,7 @@
"zod": "^3.24.2"
},
"devDependencies": {
"@kevisual/code-center-module": "0.0.12",
"@kevisual/code-center-module": "0.0.13",
"@kevisual/types": "^0.0.6",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.2",

10
pnpm-lock.yaml generated
View File

@ -96,8 +96,8 @@ importers:
version: 3.24.2
devDependencies:
'@kevisual/code-center-module':
specifier: 0.0.12
version: 0.0.12(@kevisual/auth@1.0.5)(@kevisual/router@0.0.9)(@kevisual/use-config@1.0.9)(ioredis@5.6.0)(pg@8.13.3)(sequelize@6.37.6(pg@8.13.3))
specifier: 0.0.13
version: 0.0.13(@kevisual/auth@1.0.5)(@kevisual/router@0.0.9)(@kevisual/use-config@1.0.9)(ioredis@5.6.0)(pg@8.13.3)(sequelize@6.37.6(pg@8.13.3))
'@kevisual/types':
specifier: ^0.0.6
version: 0.0.6
@ -351,8 +351,8 @@ packages:
'@kevisual/auth@1.0.5':
resolution: {integrity: sha512-GwsLj7unKXi7lmMiIIgdig4LwwLiDJnOy15HHZR5gMbyK6s5/uJiMY5RXPB2+onGzTNDqFo/hXjsD2wkerHPVg==}
'@kevisual/code-center-module@0.0.12':
resolution: {integrity: sha512-klQWlsifSeW7ej33B6/0ipB1UorEce5XHTvwNnX1QLGfUNCWm43j5xk+t71NuaVCmBO9OIRri88aU/GQc4G4fw==}
'@kevisual/code-center-module@0.0.13':
resolution: {integrity: sha512-A82sX8rdG2igyVLIF+0dagcUsGfk2b0JAga1BDDr9mrChrG1HbG1uYN7JJdjJbGE6zGYqGxRZwxKZmzB/+KMnw==}
peerDependencies:
'@kevisual/auth': ^1.0.5
'@kevisual/router': ^0.0.7
@ -2568,7 +2568,7 @@ snapshots:
'@kevisual/auth@1.0.5': {}
'@kevisual/code-center-module@0.0.12(@kevisual/auth@1.0.5)(@kevisual/router@0.0.9)(@kevisual/use-config@1.0.9)(ioredis@5.6.0)(pg@8.13.3)(sequelize@6.37.6(pg@8.13.3))':
'@kevisual/code-center-module@0.0.13(@kevisual/auth@1.0.5)(@kevisual/router@0.0.9)(@kevisual/use-config@1.0.9)(ioredis@5.6.0)(pg@8.13.3)(sequelize@6.37.6(pg@8.13.3))':
dependencies:
'@kevisual/auth': 1.0.5
'@kevisual/router': 0.0.9

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);