Compare commits
42 Commits
7d93f0eef3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c2038b8421 | |||
| daf0b6b31d | |||
| 450fbc7167 | |||
| 544b1defff | |||
| b3993f654c | |||
| c7ddaf88f6 | |||
| b0bd771e3d | |||
| 958ac3f009 | |||
| 3cf26e3eed | |||
| 633eee4bee | |||
| d29f69452c | |||
| 391c43c6b7 | |||
| 9ac04821f5 | |||
| 7f91070906 | |||
| 7b415f5ca8 | |||
| 9d3336e1c2 | |||
| 90729df51a | |||
| 41783728c8 | |||
| cd30e8af78 | |||
| 262ef1d118 | |||
| b934687314 | |||
| 6ef9e1218c | |||
| 1f4404fa5c | |||
| 3de5754f24 | |||
| c3b24ec29c | |||
| aa4d2b5451 | |||
| 9e5340066f | |||
| 2ae49eb4c8 | |||
| b4c1ddd57d | |||
| 4aaf791801 | |||
| d97053a443 | |||
| 8fafe74fa3 | |||
| 230bc6cd5d | |||
| 466ac1bcf0 | |||
| e0ac1b7d27 | |||
| b8fa48e331 | |||
| 83a018c183 | |||
| 80d16b5f76 | |||
| d8d78d184b | |||
| 12aa9022c4 | |||
| a20c082b91 | |||
| 530f594433 |
9
.env.example
Normal file
9
.env.example
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
POSTGRES_HOST=localhost
|
||||||
|
POSTGRES_PORT=5432
|
||||||
|
POSTGRES_USER=postgres
|
||||||
|
POSTGRES_PASSWORD=
|
||||||
|
POSTGRES_DB=postgres
|
||||||
|
|
||||||
|
REDIS_HOST=localhost
|
||||||
|
REDIS_PORT=6379
|
||||||
|
REDIS_PASSWORD=
|
||||||
12
.gitmodules
vendored
12
.gitmodules
vendored
@@ -1,12 +0,0 @@
|
|||||||
[submodule "submodules/code-center-module"]
|
|
||||||
path = submodules/code-center-module
|
|
||||||
url = git@git.xiongxiao.me:kevisual/code-center-module.git
|
|
||||||
[submodule "submodules/permission"]
|
|
||||||
path = submodules/permission
|
|
||||||
url = git@git.xiongxiao.me:kevisual/kevsiual-permission.git
|
|
||||||
[submodule "submodules/oss"]
|
|
||||||
path = submodules/oss
|
|
||||||
url = git@git.xiongxiao.me:kevisual/kevisual-oss.git
|
|
||||||
[submodule "submodules/pay-center-code"]
|
|
||||||
path = submodules/pay-center-code
|
|
||||||
url = git@git.xiongxiao.me:kevisual/pay-center-code.git
|
|
||||||
37
bun.config.mjs
Normal file
37
bun.config.mjs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// @ts-check
|
||||||
|
import { resolvePath } from '@kevisual/use-config';
|
||||||
|
import { execSync } from 'node:child_process';
|
||||||
|
|
||||||
|
const entry = 'src/index.ts';
|
||||||
|
const naming = 'app';
|
||||||
|
const external = ['sequelize', 'pg', 'sqlite3', 'ioredis', 'pm2'];
|
||||||
|
/**
|
||||||
|
* @type {import('bun').BuildConfig}
|
||||||
|
*/
|
||||||
|
await Bun.build({
|
||||||
|
target: 'node',
|
||||||
|
format: 'esm',
|
||||||
|
entrypoints: [resolvePath(entry, { meta: import.meta })],
|
||||||
|
outdir: resolvePath('./dist', { meta: import.meta }),
|
||||||
|
naming: {
|
||||||
|
entry: `${naming}.js`,
|
||||||
|
},
|
||||||
|
external,
|
||||||
|
env: 'KEVISUAL_*',
|
||||||
|
});
|
||||||
|
|
||||||
|
// const cmd = `dts -i src/index.ts -o app.d.ts`;
|
||||||
|
// const cmd = `dts -i ${entry} -o ${naming}.d.ts`;
|
||||||
|
// execSync(cmd, { stdio: 'inherit' });
|
||||||
|
|
||||||
|
await Bun.build({
|
||||||
|
target: 'node',
|
||||||
|
format: 'esm',
|
||||||
|
entrypoints: [resolvePath('./src/run.ts', { meta: import.meta })],
|
||||||
|
outdir: resolvePath('./dist', { meta: import.meta }),
|
||||||
|
naming: {
|
||||||
|
entry: `${'run'}.js`,
|
||||||
|
},
|
||||||
|
external,
|
||||||
|
env: 'KEVISUAL_*',
|
||||||
|
});
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
{
|
{
|
||||||
"name": "codecenter",
|
"name": "codecenter",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
"author": "abearxiong",
|
||||||
|
"basename": "/root/code-center",
|
||||||
|
"app": {
|
||||||
|
"type": "pm2-system-app",
|
||||||
|
"key": "code-center",
|
||||||
|
"entry": "./dist/app.mjs"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "pm2 start dist/app.mjs --name codecenter"
|
"start": "pm2 start apps/code-center/dist/app.mjs --name code-center"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@kevisual/router": "^0.0.10-beta.1",
|
"@kevisual/router": "^0.0.20",
|
||||||
"@kevisual/use-config": "^1.0.10",
|
"@kevisual/use-config": "^1.0.17",
|
||||||
"ioredis": "^5.6.0",
|
"ioredis": "^5.6.1",
|
||||||
"minio": "^8.0.5",
|
"minio": "^8.0.5",
|
||||||
"pg": "^8.14.1",
|
"pg": "^8.16.0",
|
||||||
"sequelize": "^6.37.6",
|
"sequelize": "^6.37.7",
|
||||||
"sqlite3": "^5.1.7",
|
"sqlite3": "^5.1.7",
|
||||||
"socket.io": "^4.8.1",
|
"socket.io": "^4.8.1",
|
||||||
"@msgpack/msgpack": "3.1.1",
|
"pm2": "^6.0.6",
|
||||||
"pino": "^9.6.0",
|
"dotenv": "^16.5.0"
|
||||||
"pino-pretty": "^13.0.0",
|
|
||||||
"pm2": "^6.0.5",
|
|
||||||
"dotenv": "^16.4.7"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import fs from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
|
|
||||||
const currentPath = process.cwd();
|
|
||||||
const packagePath = path.join(currentPath, 'script/package/package.json');
|
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
packagePath,
|
|
||||||
JSON.stringify(
|
|
||||||
{
|
|
||||||
name: 'codecenter',
|
|
||||||
version: '1.0.0',
|
|
||||||
scripts: {
|
|
||||||
start: 'pm2 start dist/app.mjs --name codecenter',
|
|
||||||
},
|
|
||||||
dependencies: {
|
|
||||||
'@kevisual/router': '^0.0.6-alpha-5',
|
|
||||||
'@kevisual/use-config': '^1.0.7',
|
|
||||||
ioredis: '^5.5.0',
|
|
||||||
minio: '^8.0.4',
|
|
||||||
pg: '^8.13.3',
|
|
||||||
sequelize: '^6.37.5',
|
|
||||||
sqlite3: '^5.1.7',
|
|
||||||
'socket.io': '^4.8.1',
|
|
||||||
'@msgpack/msgpack': '3.0.1',
|
|
||||||
pino: '^9.6.0',
|
|
||||||
'pino-pretty': '^13.0.0',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
'utf-8',
|
|
||||||
);
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "codecenter",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"scripts": {
|
|
||||||
"start": "pm2 start dist/app.mjs --name codecenter"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@kevisual/router": "^0.0.6-alpha-5",
|
|
||||||
"@kevisual/use-config": "^1.0.7",
|
|
||||||
"ioredis": "^5.5.0",
|
|
||||||
"minio": "^8.0.4",
|
|
||||||
"pg": "^8.13.3",
|
|
||||||
"sequelize": "^6.37.5",
|
|
||||||
"sqlite3": "^5.1.7",
|
|
||||||
"socket.io": "^4.8.1",
|
|
||||||
"@msgpack/msgpack": "3.0.1",
|
|
||||||
"pino": "^9.6.0",
|
|
||||||
"pino-pretty": "^13.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
dockerfile
27
dockerfile
@@ -1,27 +0,0 @@
|
|||||||
# 使用官方 Node.js 运行时镜像作为基础镜像
|
|
||||||
FROM node:22-alpine
|
|
||||||
|
|
||||||
# 设置工作目录
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY script/package/package.json ./
|
|
||||||
# 复制 package.json 和 package-lock.json
|
|
||||||
# COPY package*.json ./
|
|
||||||
|
|
||||||
# 复制 dist 文件夹
|
|
||||||
COPY dist ./dist
|
|
||||||
COPY app.config.json5 ./app.config.json5
|
|
||||||
COPY .npmrc .
|
|
||||||
|
|
||||||
# 安装依赖
|
|
||||||
RUN npm install --production --registry=https://registry.npmmirror.com/
|
|
||||||
|
|
||||||
# 如果有其他静态资源文件夹,也可以一并复制
|
|
||||||
# COPY public ./public
|
|
||||||
|
|
||||||
# 暴露应用运行的端口(假设应用运行在 3000 端口)
|
|
||||||
EXPOSE 4000
|
|
||||||
|
|
||||||
# 启动应用
|
|
||||||
CMD ["node", "dist/app.cjs"]
|
|
||||||
# CMD ["tail", "-f", "/dev/null"]
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
apps: [
|
|
||||||
{
|
|
||||||
name: 'codecenter', // 应用名称
|
|
||||||
script: './dist/app.mjs', // 入口文件
|
|
||||||
// cwd: '.', // 设置当前工作目录
|
|
||||||
output: './logs/codflow.log',
|
|
||||||
error: './logs/codflow.log',
|
|
||||||
log_date_format: 'YYYY-MM-DD HH:mm:ss',
|
|
||||||
// watch: true, // 自动监控文件变化
|
|
||||||
watch: ['dist'], // 监控的文件夹
|
|
||||||
ignore_watch: ['node_modules', 'logs'], // 忽略的文件夹
|
|
||||||
env: {
|
|
||||||
NODE_ENV: 'development'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
153
package.json
153
package.json
@@ -1,35 +1,38 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/code-center",
|
"name": "@kevisual/code-center",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"description": "code center",
|
"description": "code center",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "abearxiong",
|
"author": "abearxiong",
|
||||||
"basename": "/root/code-center",
|
"basename": "/root/code-center",
|
||||||
|
"app": {
|
||||||
|
"type": "pm2-system-app",
|
||||||
|
"key": "code-center",
|
||||||
|
"entry": "./dist/app.js",
|
||||||
|
"engine": "bun",
|
||||||
|
"runtime": [
|
||||||
|
"client"
|
||||||
|
]
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"watch": "rollup -c rollup.config.mjs -w",
|
|
||||||
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.mjs",
|
|
||||||
"test": "tsx test/**/*.ts",
|
"test": "tsx test/**/*.ts",
|
||||||
"dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
|
"dev": "bun run --watch --hot src/index.ts",
|
||||||
"build": "rimraf dist && rollup -c rollup.config.mjs",
|
"dev:inspect": "bun run --watch --hot --inspect src/index.ts",
|
||||||
"deploy": "rsync -avz --delete ./dist/ --exclude='app.config.json5' light:~/apps/codecenter/dist",
|
"cmd": "bun run src/run.ts ",
|
||||||
"deploy:sky": "rsync -avz --delete ./dist/ --exclude='app.config.json5' sky:~/kevisual/dist",
|
"prebuild": "rimraf dist",
|
||||||
"deploy:envision": "rsync -avz --delete ./dist/ --exclude='app.config.json5' envision:~/kevisual/dist",
|
"build": "NODE_ENV=production bun bun.config.mjs",
|
||||||
|
"deploy": "rsync -avz --delete ./dist/ light:/root/kevisual/assistant-app/apps/code-center/dist",
|
||||||
|
"deploy:envision": "rsync -avz --delete ./dist/ envision:~/kevisual/assistant-app/apps/code-center/dist",
|
||||||
"clean": "rm -rf dist",
|
"clean": "rm -rf dist",
|
||||||
"reload": "ssh light pm2 restart codecenter",
|
"reload": "ssh light pm2 restart code-center",
|
||||||
"reload:sky": "ssh sky pm2 restart codecenter",
|
"reload:envision": "ssh envision pm2 restart code-center",
|
||||||
"reload:envision": "ssh envision pm2 restart codecenter",
|
|
||||||
"pub:me": "npm run build && npm run deploy && npm run reload",
|
"pub:me": "npm run build && npm run deploy && npm run reload",
|
||||||
"pub:sky": "npm run build && npm run deploy:sky && npm run reload:sky",
|
|
||||||
"pub:envision": "npm run build && npm run deploy:envision && npm run reload:envision",
|
"pub:envision": "npm run build && npm run deploy:envision && npm run reload:envision",
|
||||||
"start": "pm2 start dist/app.mjs --name codecenter",
|
"start": "pm2 start dist/app.js --name code-center",
|
||||||
"release": "node ./config/release/index.mjs",
|
"client:start": "pm2 start apps/code-center/dist/app.js --name code-center",
|
||||||
"pub": "envision pack -p -u",
|
"ssl": "ssh -L 5432:localhost:5432 light",
|
||||||
"ssh": "ssh -L 6379:localhost:6379 -L 5432:localhost:5432 light ",
|
"pub": "envision pack -p -u -c"
|
||||||
"ssh:sky": "ssh -L 6379:172.21.32.13:6379 sky",
|
|
||||||
"dev:lib": "turbo run dev:lib",
|
|
||||||
"build:lib": "turbo run build",
|
|
||||||
"dev:oss": "turbo run dev:lib --filter=@kevisual/oss"
|
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"types": "types/index.d.ts",
|
"types": "types/index.d.ts",
|
||||||
@@ -38,70 +41,72 @@
|
|||||||
],
|
],
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@kevisual/local-app-manager": "0.1.10",
|
"commander": "^14.0.1",
|
||||||
"@kevisual/router": "0.0.9",
|
"cookie": "^1.0.2",
|
||||||
"@kevisual/use-config": "^1.0.10",
|
"ioredis": "^5.8.1",
|
||||||
"@types/semver": "^7.5.8",
|
"minio": "^8.0.6",
|
||||||
"archiver": "^7.0.1",
|
"pg": "^8.16.3",
|
||||||
"crypto-js": "^4.2.0",
|
"pm2": "^6.0.13",
|
||||||
"dayjs": "^1.11.13",
|
"sequelize": "^6.37.7"
|
||||||
"dotenv": "^16.4.7",
|
|
||||||
"formidable": "^3.5.2",
|
|
||||||
"ioredis": "^5.6.0",
|
|
||||||
"json5": "^2.2.3",
|
|
||||||
"jsonwebtoken": "^9.0.2",
|
|
||||||
"lodash-es": "^4.17.21",
|
|
||||||
"minio": "^8.0.5",
|
|
||||||
"nanoid": "^5.1.5",
|
|
||||||
"node-fetch": "^3.3.2",
|
|
||||||
"p-queue": "^8.1.0",
|
|
||||||
"pg": "^8.14.1",
|
|
||||||
"pm2": "^6.0.5",
|
|
||||||
"rollup-plugin-esbuild": "^6.2.1",
|
|
||||||
"semver": "^7.7.1",
|
|
||||||
"sequelize": "^6.37.6",
|
|
||||||
"socket.io": "^4.8.1",
|
|
||||||
"strip-ansi": "^7.1.0",
|
|
||||||
"tar": "^7.4.3",
|
|
||||||
"uuid": "^11.1.0",
|
|
||||||
"zod": "^3.24.2"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@kevisual/code-center-module": "workspace:*",
|
"@kevisual/code-center-module": "0.0.24",
|
||||||
"@kevisual/oss": "workspace:*",
|
"@kevisual/context": "^0.0.4",
|
||||||
"@kevisual/permission": "workspace:*",
|
"@kevisual/file-listener": "^0.0.2",
|
||||||
"@kevisual/types": "^0.0.6",
|
"@kevisual/local-app-manager": "0.1.22",
|
||||||
"@rollup/plugin-alias": "^5.1.1",
|
"@kevisual/logger": "^0.0.4",
|
||||||
"@rollup/plugin-commonjs": "^28.0.3",
|
"@kevisual/oss": "0.0.12",
|
||||||
"@rollup/plugin-json": "^6.1.0",
|
"@kevisual/permission": "^0.0.3",
|
||||||
"@rollup/plugin-node-resolve": "^16.0.1",
|
"@kevisual/router": "0.0.28",
|
||||||
"@rollup/plugin-replace": "^6.0.2",
|
"@kevisual/types": "^0.0.10",
|
||||||
"@rollup/plugin-typescript": "^12.1.2",
|
"@kevisual/use-config": "^1.0.19",
|
||||||
"@types/archiver": "^6.0.3",
|
"@types/archiver": "^6.0.3",
|
||||||
"@types/crypto-js": "^4.2.2",
|
"@types/crypto-js": "^4.2.2",
|
||||||
"@types/formidable": "^3.4.5",
|
"@types/formidable": "^3.4.6",
|
||||||
"@types/jsonwebtoken": "^9.0.9",
|
"@types/jsonwebtoken": "^9.0.10",
|
||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/node": "^22.13.13",
|
"@types/node": "^24.7.2",
|
||||||
"@types/react": "^19.0.12",
|
"@types/react": "^19.2.2",
|
||||||
"@types/uuid": "^10.0.0",
|
"@types/semver": "^7.7.1",
|
||||||
"concurrently": "^9.1.2",
|
"@types/uuid": "^11.0.0",
|
||||||
"cross-env": "^7.0.3",
|
"archiver": "^7.0.1",
|
||||||
"nodemon": "^3.1.9",
|
"cross-env": "^10.1.0",
|
||||||
|
"crypto-js": "^4.2.0",
|
||||||
|
"dayjs": "^1.11.18",
|
||||||
|
"dotenv": "^17.2.3",
|
||||||
|
"formidable": "3.5.4",
|
||||||
|
"ioredis": "^5.8.1",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"lodash-es": "^4.17.21",
|
||||||
|
"minio": "^8.0.6",
|
||||||
|
"nanoid": "^5.1.6",
|
||||||
|
"node-fetch": "^3.3.2",
|
||||||
|
"nodemon": "^3.1.10",
|
||||||
|
"p-queue": "^9.0.0",
|
||||||
|
"pg": "^8.16.3",
|
||||||
|
"pm2": "^6.0.13",
|
||||||
"rimraf": "^6.0.1",
|
"rimraf": "^6.0.1",
|
||||||
"rollup": "^4.37.0",
|
"semver": "^7.7.3",
|
||||||
"rollup-plugin-copy": "^3.5.0",
|
"sequelize": "^6.37.7",
|
||||||
"rollup-plugin-dts": "^6.2.1",
|
"socket.io": "^4.8.1",
|
||||||
|
"strip-ansi": "^7.1.2",
|
||||||
"tape": "^5.9.0",
|
"tape": "^5.9.0",
|
||||||
"tsx": "^4.19.3",
|
"tar": "^7.5.1",
|
||||||
"turbo": "^2.4.4",
|
"tsx": "^4.20.6",
|
||||||
"typescript": "^5.8.2"
|
"turbo": "^2.5.8",
|
||||||
|
"typescript": "^5.9.3",
|
||||||
|
"uuid": "^13.0.0",
|
||||||
|
"zod": "^4.1.12"
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"inflight": "latest",
|
"inflight": "latest",
|
||||||
"rimraf": "latest",
|
|
||||||
"picomatch": "^4.0.2"
|
"picomatch": "^4.0.2"
|
||||||
},
|
},
|
||||||
"pnpm": {},
|
"pnpm": {
|
||||||
"packageManager": "pnpm@10.6.5"
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"sqlite3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"packageManager": "pnpm@10.18.3"
|
||||||
}
|
}
|
||||||
2489
pnpm-lock.yaml
generated
2489
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,84 +0,0 @@
|
|||||||
import resolve from '@rollup/plugin-node-resolve';
|
|
||||||
import commonjs from '@rollup/plugin-commonjs';
|
|
||||||
import json from '@rollup/plugin-json';
|
|
||||||
import * as glob from 'fast-glob';
|
|
||||||
import path from 'path';
|
|
||||||
import esbuild from 'rollup-plugin-esbuild';
|
|
||||||
import alias from '@rollup/plugin-alias';
|
|
||||||
import replace from '@rollup/plugin-replace';
|
|
||||||
import pkgs from './package.json' with { type: 'json' };
|
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV === 'development';
|
|
||||||
const version = pkgs.version|| '1.0.0';
|
|
||||||
/**
|
|
||||||
* @type {import('rollup').RollupOptions}
|
|
||||||
*/
|
|
||||||
const config = {
|
|
||||||
input: './src/index.ts',
|
|
||||||
output: {
|
|
||||||
dir: './dist',
|
|
||||||
entryFileNames: 'app.mjs',
|
|
||||||
chunkFileNames: '[name]-[hash].mjs',
|
|
||||||
format: 'esm',
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
replace({
|
|
||||||
preventAssignment: true, // 防止意外赋值
|
|
||||||
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
|
|
||||||
VERSION: JSON.stringify(version), // 替换版本号
|
|
||||||
}),
|
|
||||||
alias({
|
|
||||||
// only esbuild needs to be configured
|
|
||||||
entries: [
|
|
||||||
{ find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录
|
|
||||||
{ find: 'http', replacement: 'node:http' },
|
|
||||||
{ find: 'https', replacement: 'node:https' },
|
|
||||||
{ find: 'fs', replacement: 'node:fs' },
|
|
||||||
{ find: 'path', replacement: 'node:path' },
|
|
||||||
{ find: 'crypto', replacement: 'node:crypto' },
|
|
||||||
{ find: 'zlib', replacement: 'node:zlib' },
|
|
||||||
{ find: 'stream', replacement: 'node:stream' },
|
|
||||||
{ find: 'net', replacement: 'node:net' },
|
|
||||||
{ find: 'tty', replacement: 'node:tty' },
|
|
||||||
{ find: 'tls', replacement: 'node:tls' },
|
|
||||||
{ find: 'buffer', replacement: 'node:buffer' },
|
|
||||||
{ find: 'timers', replacement: 'node:timers' },
|
|
||||||
// { find: 'string_decoder', replacement: 'node:string_decoder' },
|
|
||||||
{ find: 'dns', replacement: 'node:dns' },
|
|
||||||
{ find: 'domain', replacement: 'node:domain' },
|
|
||||||
{ find: 'os', replacement: 'node:os' },
|
|
||||||
{ find: 'events', replacement: 'node:events' },
|
|
||||||
{ find: 'url', replacement: 'node:url' },
|
|
||||||
{ find: 'assert', replacement: 'node:assert' },
|
|
||||||
{ find: 'util', replacement: 'node:util' },
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
resolve({
|
|
||||||
preferBuiltins: true, // 强制优先使用内置模块
|
|
||||||
}),
|
|
||||||
commonjs(),
|
|
||||||
esbuild({
|
|
||||||
target: 'node22', // 目标为 Node.js 14
|
|
||||||
minify: false, // 启用代码压缩
|
|
||||||
tsconfig: 'tsconfig.json',
|
|
||||||
}),
|
|
||||||
json(),
|
|
||||||
],
|
|
||||||
external: [
|
|
||||||
/@kevisual\/router(\/.*)?/, //, // 路由
|
|
||||||
/@kevisual\/use-config(\/.*)?/, //
|
|
||||||
|
|
||||||
'sequelize', // 数据库 orm
|
|
||||||
'ioredis', // redis
|
|
||||||
'socket.io', // socket.io
|
|
||||||
'minio', // minio
|
|
||||||
|
|
||||||
'pm2',
|
|
||||||
|
|
||||||
'pg', // pg
|
|
||||||
'pino', // pino
|
|
||||||
'pino-pretty', // pino-pretty
|
|
||||||
'@msgpack/msgpack', // msgpack
|
|
||||||
],
|
|
||||||
};
|
|
||||||
export default config;
|
|
||||||
@@ -2,4 +2,6 @@
|
|||||||
|
|
||||||
pnpm i -g npm-check-updates
|
pnpm i -g npm-check-updates
|
||||||
ncu -u
|
ncu -u
|
||||||
pnpm install
|
pnpm install
|
||||||
|
|
||||||
|
# /home/ubuntu/.nvm/versions/node/v22.14.0/bin/ncu -u
|
||||||
119
src/app-demo/index.ts
Normal file
119
src/app-demo/index.ts
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
import { Op } from 'sequelize';
|
||||||
|
import { AppDemoModel } from './models/index.ts';
|
||||||
|
import { app } from '@/app.ts';
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'app-demo',
|
||||||
|
key: 'list',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query;
|
||||||
|
const searchWhere = search
|
||||||
|
? {
|
||||||
|
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { summary: { [Op.like]: `%${search}%` } }],
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
|
||||||
|
const { rows: appDemo, count } = await AppDemoModel.findAndCountAll({
|
||||||
|
where: {
|
||||||
|
uid: tokenUser.id,
|
||||||
|
...searchWhere,
|
||||||
|
},
|
||||||
|
offset: (page - 1) * pageSize,
|
||||||
|
limit: pageSize,
|
||||||
|
order: [['updatedAt', sort]],
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
list: appDemo,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
current: page,
|
||||||
|
pageSize,
|
||||||
|
total: count,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'app-demo',
|
||||||
|
key: 'update',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id, data, updatedAt: _clear, createdAt: _clear2, ...rest } = ctx.query.data;
|
||||||
|
let appDemo: AppDemoModel;
|
||||||
|
let isNew = false;
|
||||||
|
if (id) {
|
||||||
|
const appDemo = await AppDemoModel.findByPk(id);
|
||||||
|
if (appDemo.uid !== tokenUser.uid) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
appDemo = await AppDemoModel.create({
|
||||||
|
data: data,
|
||||||
|
...rest,
|
||||||
|
uid: tokenUser.uid,
|
||||||
|
});
|
||||||
|
isNew = true;
|
||||||
|
}
|
||||||
|
if (!appDemo) {
|
||||||
|
ctx.throw(404, 'AppDemo not found');
|
||||||
|
}
|
||||||
|
if (!isNew) {
|
||||||
|
appDemo = await appDemo.update({
|
||||||
|
data: { ...appDemo.data, ...data },
|
||||||
|
...rest,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = appDemo;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'app-demo',
|
||||||
|
key: 'delete',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id, force = false } = ctx.query.data || {};
|
||||||
|
if (!id) {
|
||||||
|
ctx.throw(400, 'id is required');
|
||||||
|
}
|
||||||
|
const appDemo = await AppDemoModel.findByPk(id);
|
||||||
|
if (appDemo.uid !== tokenUser.id) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
await appDemo.destroy({ force });
|
||||||
|
ctx.body = appDemo;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'app-demo',
|
||||||
|
key: 'get',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id } = ctx.query.data || {};
|
||||||
|
if (!id) {
|
||||||
|
ctx.throw(400, 'id is required');
|
||||||
|
}
|
||||||
|
const appDemo = await AppDemoModel.findByPk(id);
|
||||||
|
if (appDemo.uid !== tokenUser.id) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
ctx.body = appDemo;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
71
src/app-demo/models/index.ts
Normal file
71
src/app-demo/models/index.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { sequelize } from '@/modules/sequelize.ts';
|
||||||
|
import { DataTypes, Model } from 'sequelize';
|
||||||
|
|
||||||
|
export interface AppDemoData {
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AppDemo = Partial<InstanceType<typeof AppDemoModel>>;
|
||||||
|
|
||||||
|
export class AppDemoModel extends Model {
|
||||||
|
declare id: string;
|
||||||
|
declare title: string;
|
||||||
|
declare description: string;
|
||||||
|
declare summary: string;
|
||||||
|
|
||||||
|
declare data: AppDemoData;
|
||||||
|
declare tags: string[];
|
||||||
|
declare version: string;
|
||||||
|
|
||||||
|
declare uid: string;
|
||||||
|
|
||||||
|
declare createdAt: Date;
|
||||||
|
declare updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
AppDemoModel.init(
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
primaryKey: true,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
summary: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
tags: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: [],
|
||||||
|
},
|
||||||
|
version: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
defaultValue: 0,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: {},
|
||||||
|
},
|
||||||
|
uid: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'kv_app_demo',
|
||||||
|
paranoid: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
AppDemoModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||||
|
console.error('AppDemoModel sync', e);
|
||||||
|
});
|
||||||
@@ -2,10 +2,16 @@ import { App } from '@kevisual/router';
|
|||||||
import * as redisLib from './modules/redis.ts';
|
import * as redisLib from './modules/redis.ts';
|
||||||
import * as minioLib from './modules/minio.ts';
|
import * as minioLib from './modules/minio.ts';
|
||||||
import * as sequelizeLib from './modules/sequelize.ts';
|
import * as sequelizeLib from './modules/sequelize.ts';
|
||||||
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/context';
|
||||||
import { SimpleRouter } from '@kevisual/router/simple';
|
import { SimpleRouter } from '@kevisual/router/simple';
|
||||||
import { OssBase } from '@kevisual/oss/services';
|
import { OssBase } from '@kevisual/oss/services';
|
||||||
export const router = useContextKey('router', () => new SimpleRouter());
|
export const router = useContextKey('router', () => new SimpleRouter());
|
||||||
|
export const runtime = useContextKey('runtime', () => {
|
||||||
|
return {
|
||||||
|
env: process.env.NODE_ENV || 'development',
|
||||||
|
type: 'server',
|
||||||
|
};
|
||||||
|
});
|
||||||
export const oss = useContextKey(
|
export const oss = useContextKey(
|
||||||
'oss',
|
'oss',
|
||||||
() =>
|
() =>
|
||||||
|
|||||||
18
src/aura/asr/index.ts
Normal file
18
src/aura/asr/index.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { app } from '@/app.ts'
|
||||||
|
import { asr } from './modules/index.ts'
|
||||||
|
app.route({
|
||||||
|
path: 'asr',
|
||||||
|
key: 'text'
|
||||||
|
}).define(async (ctx) => {
|
||||||
|
const base64Audio = ctx.query.base64Audio as string
|
||||||
|
if (!base64Audio) {
|
||||||
|
ctx.throw('Missing base64Audio parameter')
|
||||||
|
}
|
||||||
|
const result = await asr.getText({
|
||||||
|
audio: {
|
||||||
|
data: base64Audio
|
||||||
|
}
|
||||||
|
})
|
||||||
|
ctx.body = result
|
||||||
|
})
|
||||||
|
.addTo(app)
|
||||||
7
src/aura/asr/modules/index.ts
Normal file
7
src/aura/asr/modules/index.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { Asr } from '../../libs/auc.ts'
|
||||||
|
import { auraConfig } from '../../config.ts'
|
||||||
|
|
||||||
|
export const asr = new Asr({
|
||||||
|
appid: auraConfig.VOLCENGINE_AUC_APPID,
|
||||||
|
token: auraConfig.VOLCENGINE_AUC_TOKEN,
|
||||||
|
})
|
||||||
6
src/aura/config.ts
Normal file
6
src/aura/config.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { config } from '@/modules/config.ts'
|
||||||
|
export type AIConfig = {
|
||||||
|
VOLCENGINE_AUC_APPID: string
|
||||||
|
VOLCENGINE_AUC_TOKEN: string
|
||||||
|
}
|
||||||
|
export const auraConfig: AIConfig = config as unknown as AIConfig;
|
||||||
1
src/aura/index.ts
Normal file
1
src/aura/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import './asr/index.ts'
|
||||||
136
src/aura/libs/auc.ts
Normal file
136
src/aura/libs/auc.ts
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
// https://git.xiongxiao.me/kevisual/video-tools/raw/branch/main/src/asr/provider/volcengine/auc.ts
|
||||||
|
import { nanoid } from "nanoid"
|
||||||
|
|
||||||
|
export const FlashURL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/recognize/flash"
|
||||||
|
export const AsrBaseURL = 'https://openspeech.bytedance.com/api/v3/auc/bigmodel/submit'
|
||||||
|
export const AsrBase = 'volc.bigasr.auc'
|
||||||
|
export const AsrTurbo = 'volc.bigasr.auc_turbo'
|
||||||
|
|
||||||
|
const uuid = () => nanoid()
|
||||||
|
|
||||||
|
type AsrOptions = {
|
||||||
|
url?: string
|
||||||
|
appid?: string
|
||||||
|
token?: string
|
||||||
|
type?: AsrType
|
||||||
|
}
|
||||||
|
|
||||||
|
type AsrType = 'flash' | 'standard' | 'turbo'
|
||||||
|
export class Asr {
|
||||||
|
url: string = FlashURL
|
||||||
|
appid: string = ""
|
||||||
|
token: string = ""
|
||||||
|
type: AsrType = 'flash'
|
||||||
|
constructor(options: AsrOptions = {}) {
|
||||||
|
this.appid = options.appid || ""
|
||||||
|
this.token = options.token || ""
|
||||||
|
this.type = options.type || 'flash'
|
||||||
|
if (this.type !== 'flash') {
|
||||||
|
this.url = AsrBaseURL
|
||||||
|
}
|
||||||
|
if (!this.appid || !this.token) {
|
||||||
|
throw new Error("VOLCENGINE_Asr_APPID or VOLCENGINE_Asr_TOKEN is not set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
header() {
|
||||||
|
const model = this.type === 'flash' ? AsrTurbo : AsrBase
|
||||||
|
return {
|
||||||
|
"X-Api-App-Key": this.appid,
|
||||||
|
"X-Api-Access-Key": this.token,
|
||||||
|
"X-Api-Resource-Id": model,
|
||||||
|
"X-Api-Request-Id": uuid(),
|
||||||
|
"X-Api-Sequence": "-1",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
submit(body: AsrRequest) {
|
||||||
|
if (!body.audio || (!body.audio.url && !body.audio.data)) {
|
||||||
|
throw new Error("audio.url or audio.data is required")
|
||||||
|
}
|
||||||
|
const data: AsrRequest = {
|
||||||
|
...body,
|
||||||
|
}
|
||||||
|
return fetch(this.url, { method: "POST", headers: this.header(), body: JSON.stringify(data) })
|
||||||
|
}
|
||||||
|
async getText(body: AsrRequest) {
|
||||||
|
const res = await this.submit(body)
|
||||||
|
return res.json()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AsrResponse = {
|
||||||
|
audio_info: {
|
||||||
|
/**
|
||||||
|
* 音频时长,单位为 ms
|
||||||
|
*/
|
||||||
|
duration: number;
|
||||||
|
};
|
||||||
|
result: {
|
||||||
|
additions: {
|
||||||
|
duration: string;
|
||||||
|
};
|
||||||
|
text: string;
|
||||||
|
utterances: Array<{
|
||||||
|
end_time: number;
|
||||||
|
start_time: number;
|
||||||
|
text: string;
|
||||||
|
words: Array<{
|
||||||
|
confidence: number;
|
||||||
|
end_time: number;
|
||||||
|
start_time: number;
|
||||||
|
text: string;
|
||||||
|
}>;
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export interface AsrRequest {
|
||||||
|
user?: {
|
||||||
|
uid: string;
|
||||||
|
};
|
||||||
|
audio: {
|
||||||
|
url?: string;
|
||||||
|
data?: string;
|
||||||
|
format?: 'wav' | 'pcm' | 'mp3' | 'ogg';
|
||||||
|
codec?: 'raw' | 'opus'; // raw / opus,默认为 raw(pcm) 。
|
||||||
|
rate?: 8000 | 16000; // 采样率,支持 8000 或 16000,默认为 16000 。
|
||||||
|
channel?: 1 | 2; // 声道数,支持 1 或 2,默认为 1。
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
request?: {
|
||||||
|
model_name?: string; // 识别模型名称,如 "bigmodel"
|
||||||
|
enable_words?: boolean; // 是否开启词级别时间戳,默认为 false。
|
||||||
|
enable_sentence_info?: boolean; // 是否开启句子级别时间戳,默认为 false。
|
||||||
|
enable_utterance_info?: boolean; // 是否开启语句级别时间戳,默认为 true。
|
||||||
|
enable_punctuation_prediction?: boolean; // 是否开启标点符号预测,默认为 true。
|
||||||
|
enable_inverse_text_normalization?: boolean; // 是否开启文本规范化,默认为 true。
|
||||||
|
enable_separate_recognition_per_channel?: boolean; // 是否开启声道分离识别,默认为 false。
|
||||||
|
audio_channel_count?: 1 | 2; // 音频声道数,仅在 enable_separate_recognition_per_channel 开启时有效,支持 1 或 2,默认为 1。
|
||||||
|
max_sentence_silence?: number; // 句子最大静音时间,仅在 enable_sentence_info 开启时有效,单位为 ms,默认为 800。
|
||||||
|
custom_words?: string[];
|
||||||
|
enable_channel_split?: boolean; // 是否开启声道分离
|
||||||
|
enable_ddc?: boolean; // 是否开启 DDC(双通道降噪)
|
||||||
|
enable_speaker_info?: boolean; // 是否开启说话人分离
|
||||||
|
enable_punc?: boolean; // 是否开启标点符号预测(简写)
|
||||||
|
enable_itn?: boolean; // 是否开启文本规范化(简写)
|
||||||
|
vad_segment?: boolean; // 是否开启 VAD 断句
|
||||||
|
show_utterances?: boolean; // 是否返回语句级别结果
|
||||||
|
corpus?: {
|
||||||
|
boosting_table_name?: string;
|
||||||
|
correct_table_name?: string;
|
||||||
|
context?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// const main = async () => {
|
||||||
|
// const base64Audio = wavToBase64(audioPath);
|
||||||
|
// const auc = new Asr({
|
||||||
|
// appid: config.VOLCENGINE_AUC_APPID,
|
||||||
|
// token: config.VOLCENGINE_AUC_TOKEN,
|
||||||
|
// });
|
||||||
|
// const result = await auc.getText({ audio: { data: base64Audio } });
|
||||||
|
// console.log(util.inspect(result, { showHidden: false, depth: null, colors: true }))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// main();
|
||||||
20
src/logger/index.ts
Normal file
20
src/logger/index.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { useConfig } from '@kevisual/use-config/env';
|
||||||
|
import { Logger } from '@kevisual/logger';
|
||||||
|
const config = useConfig();
|
||||||
|
|
||||||
|
export const logger = new Logger({
|
||||||
|
level: config.LOG_LEVEL || 'info',
|
||||||
|
showTime: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const logError = (message: string, data?: any) => logger.error({ data }, message);
|
||||||
|
export const logWarning = (message: string, data?: any) => logger.warn({ data }, message);
|
||||||
|
export const logInfo = (message: string, data?: any) => logger.info({ data }, message);
|
||||||
|
export const logDebug = (message: string, data?: any) => logger.debug({ data }, message);
|
||||||
|
|
||||||
|
export const log = {
|
||||||
|
error: logError,
|
||||||
|
warn: logWarning,
|
||||||
|
info: logInfo,
|
||||||
|
debug: logDebug,
|
||||||
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// import { DataTypes, Model, Sequelize } from 'sequelize';
|
// import { DataTypes, Model, Sequelize } from 'sequelize';
|
||||||
// import { useContextKey } from '@kevisual/use-config/context';
|
// import { useContextKey } from '@kevisual/context';
|
||||||
// const sequelize = useContextKey<Sequelize>('sequelize');
|
// const sequelize = useContextKey<Sequelize>('sequelize');
|
||||||
// export class Org extends Model {
|
// export class Org extends Model {
|
||||||
// declare id: string;
|
// declare id: string;
|
||||||
@@ -42,7 +42,5 @@
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
// useContextKey('OrgModel', () => Org);
|
// useContextKey('OrgModel', () => Org);
|
||||||
import { Org, OrgInit } from '@kevisual/code-center-module/models';
|
import { Org } from '@kevisual/code-center-module/models';
|
||||||
export { Org };
|
export { Org };
|
||||||
|
|
||||||
OrgInit();
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { User, UserInit, UserServices } from '@kevisual/code-center-module/models';
|
import { User, UserInit, UserServices } from '@kevisual/code-center-module/models';
|
||||||
export { User, UserInit, UserServices };
|
import { UserSecretInit, UserSecret } from '@kevisual/code-center-module/models';
|
||||||
import { OrgInit } from '@kevisual/code-center-module/models';
|
import { OrgInit } from '@kevisual/code-center-module/models';
|
||||||
|
export { User, UserInit, UserServices, UserSecret };
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
await OrgInit(null, null, {
|
await OrgInit(null, null, {
|
||||||
alter: true,
|
alter: true,
|
||||||
@@ -14,5 +15,11 @@ const init = async () => {
|
|||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.error('User sync', e);
|
console.error('User sync', e);
|
||||||
});
|
});
|
||||||
|
await UserSecretInit(null, null, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
}).catch((e) => {
|
||||||
|
console.error('UserSecret sync', e);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
init();
|
init();
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
|
// import { useConfig } from '@kevisual/use-config/env';
|
||||||
|
|
||||||
const envFiles = [
|
export const envFiles = [
|
||||||
path.resolve(process.cwd(), '.env.dev'),
|
path.resolve(process.cwd(), process.env.NODE_ENV === 'development' ? '.env.dev' : '.env'),
|
||||||
path.resolve(process.cwd(), '.env'),
|
// path.resolve(process.cwd(), '.env'), //
|
||||||
];
|
];
|
||||||
dotenv.config({
|
console.log('envFiles', envFiles);
|
||||||
|
export const config = dotenv.config({
|
||||||
path: envFiles,
|
path: envFiles,
|
||||||
});
|
override: true,
|
||||||
|
}).parsed;
|
||||||
export const config = process.env;
|
// const config = useConfig();
|
||||||
|
// export const config = process.env;
|
||||||
|
// console.log('config', config);
|
||||||
export const port = config.PORT || 4005;
|
export const port = config.PORT || 4005;
|
||||||
|
|||||||
20
src/modules/logger.ts
Normal file
20
src/modules/logger.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { useConfig } from '@kevisual/use-config/env';
|
||||||
|
import { Logger } from '@kevisual/logger';
|
||||||
|
const config = useConfig();
|
||||||
|
|
||||||
|
export const logger = new Logger({
|
||||||
|
level: config.LOG_LEVEL || 'info',
|
||||||
|
showTime: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const logError = (message: string, data?: any) => logger.error({ data }, message);
|
||||||
|
export const logWarning = (message: string, data?: any) => logger.warn({ data }, message);
|
||||||
|
export const logInfo = (message: string, data?: any) => logger.info({ data }, message);
|
||||||
|
export const logDebug = (message: string, data?: any) => logger.debug({ data }, message);
|
||||||
|
|
||||||
|
export const log = {
|
||||||
|
error: logError,
|
||||||
|
warn: logWarning,
|
||||||
|
info: logInfo,
|
||||||
|
debug: logDebug,
|
||||||
|
};
|
||||||
@@ -1,25 +1,33 @@
|
|||||||
import { Client, ClientOptions } from 'minio';
|
import { Client, ClientOptions } from 'minio';
|
||||||
|
import { config } from './config.ts';
|
||||||
|
import { OssBase } from '@kevisual/oss/services';
|
||||||
const minioConfig = {
|
const minioConfig = {
|
||||||
endPoint: process.env.MINIO_ENDPOINT || 'localhost',
|
endPoint: config.MINIO_ENDPOINT || 'localhost',
|
||||||
port: parseInt(process.env.MINIO_PORT || '9000'),
|
port: parseInt(config.MINIO_PORT || '9000'),
|
||||||
useSSL: process.env.MINIO_USE_SSL === 'true',
|
useSSL: config.MINIO_USE_SSL === 'true',
|
||||||
accessKey: process.env.MINIO_ACCESS_KEY,
|
accessKey: config.MINIO_ACCESS_KEY,
|
||||||
secretKey: process.env.MINIO_SECRET_KEY,
|
secretKey: config.MINIO_SECRET_KEY,
|
||||||
};
|
};
|
||||||
|
// console.log('minioConfig', minioConfig);
|
||||||
export const minioClient = new Client(minioConfig);
|
export const minioClient = new Client(minioConfig);
|
||||||
|
|
||||||
export const bucketName = process.env.MINIO_BUCKET_NAME || 'resources';
|
export const bucketName = config.MINIO_BUCKET_NAME || 'resources';
|
||||||
if (!minioClient) {
|
if (!minioClient) {
|
||||||
throw new Error('Minio client not initialized');
|
throw new Error('Minio client not initialized');
|
||||||
}
|
}
|
||||||
// 验证权限
|
// 验证权限
|
||||||
// (async () => {
|
(async () => {
|
||||||
// const bucketExists = await minioClient.bucketExists(bucketName);
|
const bucketExists = await minioClient.bucketExists(bucketName);
|
||||||
// if (!bucketExists) {
|
if (!bucketExists) {
|
||||||
// await minioClient.makeBucket(bucketName);
|
await minioClient.makeBucket(bucketName);
|
||||||
// }
|
}
|
||||||
// const res = await minioClient.putObject(bucketName, 'private/test/a.b', 'test');
|
console.log('bucketExists', bucketExists);
|
||||||
// console.log('minio putObject', res);
|
// const res = await minioClient.putObject(bucketName, 'root/test/0.0.1/a.txt', 'test');
|
||||||
|
// console.log('minio putObject', res);
|
||||||
|
})();
|
||||||
|
|
||||||
// })();
|
export const oss = new OssBase({
|
||||||
|
client: minioClient,
|
||||||
|
bucketName: bucketName,
|
||||||
|
prefix: '',
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,18 +1,29 @@
|
|||||||
import { Redis } from 'ioredis';
|
import { Redis } from 'ioredis';
|
||||||
|
import { config } from './config.ts';
|
||||||
|
const redisConfig = {
|
||||||
|
host: config.REDIS_HOST || 'localhost',
|
||||||
|
port: parseInt(config.REDIS_PORT || '6379'),
|
||||||
|
password: config.REDIS_PASSWORD,
|
||||||
|
};
|
||||||
|
export const createRedisClient = (options = {}) => {
|
||||||
|
const redisClient = new Redis({
|
||||||
|
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||||
|
port: 6379, // Redis 服务器的端口号
|
||||||
|
// password: 'your_password', // Redis 的密码 (如果有)
|
||||||
|
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||||
|
keyPrefix: '', // key 前缀
|
||||||
|
retryStrategy(times) {
|
||||||
|
// 连接重试策略
|
||||||
|
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||||
|
},
|
||||||
|
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||||
|
...redisConfig,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
return redisClient;
|
||||||
|
};
|
||||||
// 配置 Redis 连接
|
// 配置 Redis 连接
|
||||||
export const redis = new Redis({
|
export const redis = createRedisClient();
|
||||||
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
|
||||||
port: 6379, // Redis 服务器的端口号
|
|
||||||
// password: 'your_password', // Redis 的密码 (如果有)
|
|
||||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
|
||||||
keyPrefix: '', // key 前缀
|
|
||||||
retryStrategy(times) {
|
|
||||||
// 连接重试策略
|
|
||||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
|
||||||
},
|
|
||||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
|
||||||
});
|
|
||||||
|
|
||||||
// 监听连接事件
|
// 监听连接事件
|
||||||
redis.on('connect', () => {
|
redis.on('connect', () => {
|
||||||
@@ -24,5 +35,5 @@ redis.on('error', (err) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 初始化 Redis 客户端
|
// 初始化 Redis 客户端
|
||||||
export const redisPublisher = new Redis(); // 用于发布消息
|
export const redisPublisher = createRedisClient(); // 用于发布消息
|
||||||
export const redisSubscriber = new Redis(); // 用于订阅消息
|
export const redisSubscriber = createRedisClient(); // 用于订阅消息
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import childProcess from 'child_process';
|
import childProcess from 'child_process';
|
||||||
|
|
||||||
export const selfRestart = async () => {
|
export const selfRestart = async () => {
|
||||||
const appName = 'codecenter';
|
const appName = 'code-center';
|
||||||
// 检测 pm2 是否安装和是否有 appName 这个应用
|
// 检测 pm2 是否安装和是否有 appName 这个应用
|
||||||
try {
|
try {
|
||||||
const res = childProcess.execSync(`pm2 list`);
|
const res = childProcess.execSync(`pm2 list`);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Sequelize } from 'sequelize';
|
import { Sequelize } from 'sequelize';
|
||||||
import { config } from './config.ts';
|
import { config } from './config.ts';
|
||||||
|
import { log } from './logger.ts';
|
||||||
export type PostgresConfig = {
|
export type PostgresConfig = {
|
||||||
postgres: {
|
postgres: {
|
||||||
username: string;
|
username: string;
|
||||||
@@ -10,7 +11,8 @@ export type PostgresConfig = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
|
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
|
||||||
console.error('postgres config is required password and user');
|
log.error('postgres config is required password and user');
|
||||||
|
log.error('config', config);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const postgresConfig = {
|
const postgresConfig = {
|
||||||
@@ -26,3 +28,13 @@ export const sequelize = new Sequelize({
|
|||||||
...postgresConfig,
|
...postgresConfig,
|
||||||
// logging: false,
|
// logging: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
sequelize
|
||||||
|
.authenticate({ logging: false })
|
||||||
|
.then(() => {
|
||||||
|
log.info('Database connected');
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
log.error('Database connection failed', { err, config: postgresConfig });
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|||||||
16
src/program.ts
Normal file
16
src/program.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { program, Command } from 'commander';
|
||||||
|
// import { useContextKey } from '@kevisual/context';
|
||||||
|
// import * as redisLib from './modules/redis.ts';
|
||||||
|
// import * as sequelizeLib from './modules/sequelize.ts';
|
||||||
|
// import * as minioLib from './modules/minio.ts';
|
||||||
|
|
||||||
|
// export const redis = useContextKey('redis', () => redisLib.redis);
|
||||||
|
// export const redisPublisher = useContextKey('redisPublisher', () => redisLib.redisPublisher);
|
||||||
|
// export const redisSubscriber = useContextKey('redisSubscriber', () => redisLib.redisSubscriber);
|
||||||
|
// export const minioClient = useContextKey('minioClient', () => minioLib.minioClient);
|
||||||
|
// export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
|
||||||
|
|
||||||
|
export { program, Command };
|
||||||
|
|
||||||
|
program.description('code-center的一部分工具');
|
||||||
|
program.version('1.0.0', '-v, --version');
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
import './routes/index.ts';
|
import './routes/index.ts';
|
||||||
|
import './aura/index.ts';
|
||||||
import { app } from './app.ts';
|
import { app } from './app.ts';
|
||||||
import type { App } from '@kevisual/router';
|
import type { App } from '@kevisual/router';
|
||||||
import { User } from './models/user.ts';
|
import { User } from './models/user.ts';
|
||||||
// import { addAuth } from '@kevisual/code-center-module/models';
|
|
||||||
// addAuth(app);
|
|
||||||
import { createCookie, getSomeInfoFromReq } from './routes/user/me.ts';
|
import { createCookie, getSomeInfoFromReq } from './routes/user/me.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,7 +17,6 @@ export const addAuth = (app: App) => {
|
|||||||
.route({
|
.route({
|
||||||
path: 'auth',
|
path: 'auth',
|
||||||
id: 'auth',
|
id: 'auth',
|
||||||
isDebug: true,
|
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const token = ctx.query.token;
|
const token = ctx.query.token;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export const getLoginUser = async (req: http.IncomingMessage) => {
|
|||||||
if (!token) {
|
if (!token) {
|
||||||
const parsedCookies = cookie.parse(req.headers.cookie || '');
|
const parsedCookies = cookie.parse(req.headers.cookie || '');
|
||||||
token = parsedCookies.token || '';
|
token = parsedCookies.token || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
token = token.replace('Bearer ', '');
|
token = token.replace('Bearer ', '');
|
||||||
|
|||||||
@@ -97,7 +97,6 @@ export const authMinio = async (req: IncomingMessage, res: ServerResponse, objec
|
|||||||
etag,
|
etag,
|
||||||
'last-modified': lastModified,
|
'last-modified': lastModified,
|
||||||
'Content-Disposition': contentDisposition,
|
'Content-Disposition': contentDisposition,
|
||||||
'file-name': filename,
|
|
||||||
...filteredMetaData,
|
...filteredMetaData,
|
||||||
});
|
});
|
||||||
const objectStream = await minioClient.getObject(bucketName, objectName);
|
const objectStream = await minioClient.getObject(bucketName, objectName);
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { useFileStore } from '@kevisual/use-config/file-store';
|
import { useFileStore } from '@kevisual/use-config/file-store';
|
||||||
import { checkAuth, error, router, writeEvents, getKey, getTaskId } from '../router.ts';
|
import { checkAuth, error, router, writeEvents, getKey, getTaskId } from '../router.ts';
|
||||||
import { IncomingForm } from 'formidable';
|
import { IncomingForm } from 'formidable';
|
||||||
import { app, minioClient } from '@/app.ts';
|
import { app, oss } from '@/app.ts';
|
||||||
|
|
||||||
import { bucketName } from '@/modules/minio.ts';
|
|
||||||
import { getContentType } from '@/utils/get-content-type.ts';
|
import { getContentType } from '@/utils/get-content-type.ts';
|
||||||
import { User } from '@/models/user.ts';
|
import { User } from '@/models/user.ts';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
@@ -23,6 +22,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
|||||||
if (!tokenUser) return;
|
if (!tokenUser) return;
|
||||||
const url = new URL(req.url || '', 'http://localhost');
|
const url = new URL(req.url || '', 'http://localhost');
|
||||||
const share = !!url.searchParams.get('public');
|
const share = !!url.searchParams.get('public');
|
||||||
|
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
|
||||||
// 使用 formidable 解析 multipart/form-data
|
// 使用 formidable 解析 multipart/form-data
|
||||||
const form = new IncomingForm({
|
const form = new IncomingForm({
|
||||||
multiples: false, // 改为单文件上传
|
multiples: false, // 改为单文件上传
|
||||||
@@ -123,8 +123,9 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
|||||||
if (share) {
|
if (share) {
|
||||||
metadata.share = 'public';
|
metadata.share = 'public';
|
||||||
}
|
}
|
||||||
|
const bucketName = oss.bucketName;
|
||||||
// All chunks uploaded, now upload to MinIO
|
// All chunks uploaded, now upload to MinIO
|
||||||
await minioClient.fPutObject(bucketName, minioPath, finalFilePath, {
|
await oss.client.fPutObject(bucketName, minioPath, finalFilePath, {
|
||||||
'Content-Type': getContentType(relativePath),
|
'Content-Type': getContentType(relativePath),
|
||||||
'app-source': 'user-app',
|
'app-source': 'user-app',
|
||||||
'Cache-Control': relativePath.endsWith('.html') ? 'no-cache' : 'max-age=31536000, immutable',
|
'Cache-Control': relativePath.endsWith('.html') ? 'no-cache' : 'max-age=31536000, immutable',
|
||||||
@@ -133,38 +134,57 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
|||||||
|
|
||||||
// Clean up the final file
|
// Clean up the final file
|
||||||
fs.unlinkSync(finalFilePath);
|
fs.unlinkSync(finalFilePath);
|
||||||
|
|
||||||
// Notify the app
|
|
||||||
const r = await app.call({
|
|
||||||
path: 'app',
|
|
||||||
key: 'detect-version-list',
|
|
||||||
payload: {
|
|
||||||
token: token,
|
|
||||||
data: {
|
|
||||||
appKey,
|
|
||||||
version,
|
|
||||||
username,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const downloadBase = '/api/s1/share';
|
const downloadBase = '/api/s1/share';
|
||||||
const data: any = {
|
|
||||||
code: r.code,
|
const uploadResult = {
|
||||||
data: {
|
name: relativePath,
|
||||||
app: r.body,
|
path: `${downloadBase}/${minioPath}`,
|
||||||
resource: `${downloadBase}/${minioPath}`,
|
appKey,
|
||||||
},
|
version,
|
||||||
|
username,
|
||||||
};
|
};
|
||||||
if (r.message) {
|
if (!noCheckAppFiles) {
|
||||||
data.message = r.message;
|
// Notify the app
|
||||||
|
const r = await app.call({
|
||||||
|
path: 'app',
|
||||||
|
key: 'detectVersionList',
|
||||||
|
payload: {
|
||||||
|
token: token,
|
||||||
|
data: {
|
||||||
|
appKey,
|
||||||
|
version,
|
||||||
|
username,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const data: any = {
|
||||||
|
code: r.code,
|
||||||
|
data: {
|
||||||
|
app: r.body,
|
||||||
|
upload: [uploadResult],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (r.message) {
|
||||||
|
data.message = r.message;
|
||||||
|
}
|
||||||
|
console.log('upload data', data);
|
||||||
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
res.end(JSON.stringify(data));
|
||||||
|
} else {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
res.end(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 200,
|
||||||
|
message: 'Chunk uploaded successfully',
|
||||||
|
data: { chunkIndex, totalChunks, upload: [uploadResult] },
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
console.log('upload data', data);
|
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
||||||
res.end(JSON.stringify(data));
|
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
res.end(
|
res.end(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
code: 200,
|
||||||
message: 'Chunk uploaded successfully',
|
message: 'Chunk uploaded successfully',
|
||||||
data: {
|
data: {
|
||||||
chunkIndex,
|
chunkIndex,
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ router.all('/api/s1/share/*splat', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const url = req.url;
|
const url = req.url;
|
||||||
const _url = new URL(url || '', 'http://localhost');
|
const _url = new URL(url || '', 'http://localhost');
|
||||||
const objectName = _url.pathname.replace('/api/s1/share/', '');
|
let objectName = _url.pathname.replace('/api/s1/share/', '');
|
||||||
|
objectName = decodeURIComponent(objectName);
|
||||||
await authMinio(req, res, objectName);
|
await authMinio(req, res, objectName);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('get share resource error url', req.url);
|
console.log('get share resource error url', req.url);
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import { User } from '@/models/user.ts';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { ConfigModel } from '@/routes/config/models/model.ts';
|
import { ConfigModel } from '@/routes/config/models/model.ts';
|
||||||
import { validateDirectory } from './util.ts';
|
import { validateDirectory } from './util.ts';
|
||||||
|
import { pick } from 'lodash-es';
|
||||||
|
import { getFileStat } from '@/routes/file/index.ts';
|
||||||
|
import { logger } from '@/logger/index.ts';
|
||||||
|
|
||||||
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||||
|
|
||||||
@@ -16,12 +19,91 @@ router.get('/api/s1/resources/upload', async (req, res) => {
|
|||||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
res.end('Upload API is ready');
|
res.end('Upload API is ready');
|
||||||
});
|
});
|
||||||
|
export const parseIfJson = (data = '{}') => {
|
||||||
|
try {
|
||||||
|
const _data = JSON.parse(data);
|
||||||
|
if (typeof _data === 'object') return _data;
|
||||||
|
return {};
|
||||||
|
} catch (error) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
router.post('/api/s1/resources/upload/check', async (req, res) => {
|
||||||
|
const { tokenUser, token } = await checkAuth(req, res);
|
||||||
|
if (!tokenUser) {
|
||||||
|
res.end(error('Token is invalid.'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('data', req.url);
|
||||||
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
const data = await router.getBody(req);
|
||||||
|
type Data = {
|
||||||
|
appKey: string;
|
||||||
|
version: string;
|
||||||
|
username: string;
|
||||||
|
directory: string;
|
||||||
|
files: { path: string; hash: string }[];
|
||||||
|
};
|
||||||
|
let { appKey, version, username, directory, files } = pick(data, ['appKey', 'version', 'username', 'directory', 'files']) as Data;
|
||||||
|
let uid = tokenUser.id;
|
||||||
|
if (username) {
|
||||||
|
const user = await User.getUserByToken(token);
|
||||||
|
const has = await user.hasUser(username, true);
|
||||||
|
if (!has) {
|
||||||
|
res.end(error('username is not found'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const _user = await User.findOne({ where: { username } });
|
||||||
|
uid = _user?.id || '';
|
||||||
|
}
|
||||||
|
if (!appKey || !version) {
|
||||||
|
res.end(error('appKey and version is required'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const { code, message } = validateDirectory(directory);
|
||||||
|
if (code !== 200) {
|
||||||
|
res.end(error(message));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
type CheckResult = {
|
||||||
|
path: string;
|
||||||
|
stat: any;
|
||||||
|
resourcePath: string;
|
||||||
|
hash: string;
|
||||||
|
uploadHash: string;
|
||||||
|
isUpload?: boolean;
|
||||||
|
};
|
||||||
|
const checkResult: CheckResult[] = [];
|
||||||
|
for (let i = 0; i < files.length; i++) {
|
||||||
|
const file = files[i];
|
||||||
|
const relativePath = file.path;
|
||||||
|
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
|
||||||
|
let stat = await getFileStat(minioPath, true);
|
||||||
|
const statHash = stat?.etag || '';
|
||||||
|
checkResult.push({
|
||||||
|
path: relativePath,
|
||||||
|
uploadHash: file.hash,
|
||||||
|
resourcePath: minioPath,
|
||||||
|
isUpload: statHash === file.hash,
|
||||||
|
stat,
|
||||||
|
hash: statHash,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.end(JSON.stringify({ code: 200, data: checkResult }));
|
||||||
|
});
|
||||||
|
|
||||||
// /api/s1/resources/upload
|
// /api/s1/resources/upload
|
||||||
router.post('/api/s1/resources/upload', async (req, res) => {
|
router.post('/api/s1/resources/upload', async (req, res) => {
|
||||||
const { tokenUser, token } = await checkAuth(req, res);
|
const { tokenUser, token } = await checkAuth(req, res);
|
||||||
if (!tokenUser) return;
|
if (!tokenUser) {
|
||||||
// 使用 formidable 解析 multipart/form-data
|
res.end(error('Token is invalid.'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const url = new URL(req.url || '', 'http://localhost');
|
||||||
|
const share = !!url.searchParams.get('public');
|
||||||
|
const meta = parseIfJson(url.searchParams.get('meta'));
|
||||||
|
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
|
||||||
|
// 使用 formi dable 解析 multipart/form-data
|
||||||
const form = new IncomingForm({
|
const form = new IncomingForm({
|
||||||
multiples: true, // 支持多文件上传
|
multiples: true, // 支持多文件上传
|
||||||
uploadDir: cacheFilePath, // 上传文件存储目录
|
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||||
@@ -45,10 +127,13 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
|||||||
const clearFiles = () => {
|
const clearFiles = () => {
|
||||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||||
uploadedFiles.forEach((file) => {
|
uploadedFiles.forEach((file) => {
|
||||||
fs.unlinkSync(file.filepath);
|
if (file?.filepath && fs.existsSync(file.filepath)) {
|
||||||
|
fs.unlinkSync(file.filepath);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (err) {
|
if (err) {
|
||||||
|
logger.error(`Upload error: ${err.message}`);
|
||||||
res.end(error(`Upload error: ${err.message}`));
|
res.end(error(`Upload error: ${err.message}`));
|
||||||
clearFiles();
|
clearFiles();
|
||||||
return;
|
return;
|
||||||
@@ -86,6 +171,12 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
|||||||
}
|
}
|
||||||
// 逐个处理每个上传的文件
|
// 逐个处理每个上传的文件
|
||||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||||
|
logger.info(
|
||||||
|
'upload files',
|
||||||
|
uploadedFiles.map((item) => {
|
||||||
|
return pick(item, ['filepath', 'originalFilename']);
|
||||||
|
}),
|
||||||
|
);
|
||||||
const uploadResults = [];
|
const uploadResults = [];
|
||||||
for (let i = 0; i < uploadedFiles.length; i++) {
|
for (let i = 0; i < uploadedFiles.length; i++) {
|
||||||
const file = uploadedFiles[i];
|
const file = uploadedFiles[i];
|
||||||
@@ -96,39 +187,62 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
|||||||
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
|
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
|
||||||
// 上传到 MinIO 并保留文件夹结构
|
// 上传到 MinIO 并保留文件夹结构
|
||||||
const isHTML = relativePath.endsWith('.html');
|
const isHTML = relativePath.endsWith('.html');
|
||||||
|
const metadata: any = {};
|
||||||
|
if (share) {
|
||||||
|
metadata.share = 'public';
|
||||||
|
}
|
||||||
|
Object.assign(metadata, meta);
|
||||||
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
|
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
|
||||||
'Content-Type': getContentType(relativePath),
|
'Content-Type': getContentType(relativePath),
|
||||||
'app-source': 'user-app',
|
'app-source': 'user-app',
|
||||||
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
|
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
|
||||||
|
...metadata,
|
||||||
});
|
});
|
||||||
uploadResults.push({
|
uploadResults.push({
|
||||||
name: relativePath,
|
name: relativePath,
|
||||||
path: minioPath,
|
path: minioPath,
|
||||||
});
|
});
|
||||||
fs.unlinkSync(tempPath); // 删除临时文件
|
fs.unlinkSync(tempPath); // 删除临时文件
|
||||||
} // 受控
|
|
||||||
const r = await app.call({
|
|
||||||
path: 'app',
|
|
||||||
key: 'uploadFiles',
|
|
||||||
payload: {
|
|
||||||
token: token,
|
|
||||||
data: {
|
|
||||||
appKey,
|
|
||||||
version,
|
|
||||||
username,
|
|
||||||
files: uploadResults,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const data: any = {
|
|
||||||
code: r.code,
|
|
||||||
data: r.body,
|
|
||||||
};
|
|
||||||
if (r.message) {
|
|
||||||
data.message = r.message;
|
|
||||||
}
|
}
|
||||||
console.log('upload data', data);
|
if (!noCheckAppFiles) {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
// 受控
|
||||||
res.end(JSON.stringify(data));
|
const r = await app.call({
|
||||||
|
path: 'app',
|
||||||
|
key: 'uploadFiles',
|
||||||
|
payload: {
|
||||||
|
token: token,
|
||||||
|
data: {
|
||||||
|
appKey,
|
||||||
|
version,
|
||||||
|
username,
|
||||||
|
files: uploadResults,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const data: any = {
|
||||||
|
code: r.code,
|
||||||
|
data: {
|
||||||
|
app: r.body,
|
||||||
|
upload: uploadResults,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (r.message) {
|
||||||
|
data.message = r.message;
|
||||||
|
}
|
||||||
|
console.log('upload data', data);
|
||||||
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
res.end(JSON.stringify(data));
|
||||||
|
} else {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
res.end(
|
||||||
|
JSON.stringify({
|
||||||
|
code: 200,
|
||||||
|
data: {
|
||||||
|
detect: [],
|
||||||
|
upload: uploadResults,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { router } from '@/app.ts';
|
import { router } from '@/app.ts';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import { useContextKey } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/context';
|
||||||
import { checkAuth, error } from './middleware/auth.ts';
|
import { checkAuth, error } from './middleware/auth.ts';
|
||||||
import formidable from 'formidable';
|
import formidable from 'formidable';
|
||||||
export { router, checkAuth, error };
|
export { router, checkAuth, error };
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import { getContainerById } from '@/routes/container/module/get-container-file.t
|
|||||||
import { router, error, checkAuth, clients, writeEvents } from './router.ts';
|
import { router, error, checkAuth, clients, writeEvents } from './router.ts';
|
||||||
import './index.ts';
|
import './index.ts';
|
||||||
|
|
||||||
const filePath = useFileStore('upload', { needExists: true });
|
|
||||||
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
const cacheFilePath = useFileStore('cache-file', { needExists: true });
|
||||||
// curl -X POST http://localhost:4000/api/upload -F "file=@readme.md"
|
// curl -X POST http://localhost:4000/api/upload -F "file=@readme.md"
|
||||||
// curl -X POST http://localhost:4000/api/upload \
|
// curl -X POST http://localhost:4000/api/upload \
|
||||||
@@ -178,13 +177,13 @@ router.get('/api/container/file/:id', async (req, res) => {
|
|||||||
res.end(JSON.stringify(container));
|
res.end(JSON.stringify(container));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/api/code/version', async (req, res) => {
|
// router.get('/api/code/version', async (req, res) => {
|
||||||
const version = VERSION;
|
// const version = VERSION;
|
||||||
res.writeHead(200, {
|
// res.writeHead(200, {
|
||||||
'Content-Type': 'application/json',
|
// 'Content-Type': 'application/json',
|
||||||
});
|
// });
|
||||||
res.end(JSON.stringify({ code: 200, data: { version } }));
|
// res.end(JSON.stringify({ code: 200, data: { version } }));
|
||||||
});
|
// });
|
||||||
|
|
||||||
export const uploadMiddleware = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
export const uploadMiddleware = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||||
if (req.url?.startsWith('/api/router')) {
|
if (req.url?.startsWith('/api/router')) {
|
||||||
|
|||||||
13
src/routes/app-manager/admin/mv-user-app.ts
Normal file
13
src/routes/app-manager/admin/mv-user-app.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { app } from '@/app.ts';
|
||||||
|
import { AppModel, AppListModel } from '../module/index.ts';
|
||||||
|
export const mvAppFromUserAToUserB = async (userA: string, userB: string) => {
|
||||||
|
const appList = await AppModel.findAll({
|
||||||
|
where: {
|
||||||
|
user: userA,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
for (const app of appList) {
|
||||||
|
app.user = userB;
|
||||||
|
await app.save();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -2,7 +2,7 @@ import { app } from '@/app.ts';
|
|||||||
export const callDetectAppVersion = async ({ appKey, version, username }: { appKey: string; version: string; username: string }, token: string) => {
|
export const callDetectAppVersion = async ({ appKey, version, username }: { appKey: string; version: string; username: string }, token: string) => {
|
||||||
const res = await app.call({
|
const res = await app.call({
|
||||||
path: 'app',
|
path: 'app',
|
||||||
key: 'detect-version-list',
|
key: 'detectVersionList',
|
||||||
payload: {
|
payload: {
|
||||||
token: token,
|
token: token,
|
||||||
data: { appKey, version, username },
|
data: { appKey, version, username },
|
||||||
|
|||||||
@@ -4,4 +4,6 @@ import './user-app.ts';
|
|||||||
import './public/index.ts';
|
import './public/index.ts';
|
||||||
import './domain/index.ts';
|
import './domain/index.ts';
|
||||||
|
|
||||||
|
import './proxy/index.ts';
|
||||||
|
|
||||||
export * from './module/index.ts';
|
export * from './module/index.ts';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { App, CustomError } from '@kevisual/router';
|
import { App, CustomError } from '@kevisual/router';
|
||||||
import { AppModel, AppListModel } from './module/index.ts';
|
import { AppModel, AppListModel } from './module/index.ts';
|
||||||
import { app, redis } from '@/app.ts';
|
import { app, redis } from '@/app.ts';
|
||||||
import _ from 'lodash';
|
import { uniqBy } from 'lodash-es';
|
||||||
import { getUidByUsername, prefixFix } from './util.ts';
|
import { getUidByUsername, prefixFix } from './util.ts';
|
||||||
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
|
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
|
||||||
import { setExpire } from './revoke.ts';
|
import { setExpire } from './revoke.ts';
|
||||||
@@ -40,15 +40,26 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const id = ctx.query.id;
|
const id = ctx.query.id;
|
||||||
if (!id) {
|
const { key, version } = ctx.query?.data || {};
|
||||||
|
if (!id && (!key || !version)) {
|
||||||
throw new CustomError('id is required');
|
throw new CustomError('id is required');
|
||||||
}
|
}
|
||||||
const am = await AppListModel.findByPk(id);
|
let am: AppListModel;
|
||||||
|
if (id) {
|
||||||
|
am = await AppListModel.findByPk(id);
|
||||||
|
} else if (key && version) {
|
||||||
|
am = await AppListModel.findOne({
|
||||||
|
where: {
|
||||||
|
key,
|
||||||
|
version,
|
||||||
|
uid: tokenUser.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!am) {
|
if (!am) {
|
||||||
throw new CustomError('app not found');
|
throw new CustomError('app not found');
|
||||||
}
|
}
|
||||||
ctx.body = prefixFix(am, tokenUser.username);
|
ctx.body = prefixFix(am, tokenUser.username);
|
||||||
return ctx;
|
|
||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
|
||||||
@@ -91,6 +102,7 @@ app
|
|||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const id = ctx.query.id;
|
const id = ctx.query.id;
|
||||||
|
const deleteFile = !!ctx.query.deleteFile; // 是否删除文件, 默认不删除
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new CustomError('id is required');
|
throw new CustomError('id is required');
|
||||||
}
|
}
|
||||||
@@ -106,7 +118,7 @@ app
|
|||||||
throw new CustomError('app is published');
|
throw new CustomError('app is published');
|
||||||
}
|
}
|
||||||
const files = app.data.files || [];
|
const files = app.data.files || [];
|
||||||
if (files.length > 0) {
|
if (deleteFile && files.length > 0) {
|
||||||
await deleteFiles(files.map((item) => item.path));
|
await deleteFiles(files.map((item) => item.path));
|
||||||
}
|
}
|
||||||
await app.destroy({
|
await app.destroy({
|
||||||
@@ -195,7 +207,7 @@ app
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
const dataFiles = app.data.files || [];
|
const dataFiles = app.data.files || [];
|
||||||
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
|
const newFiles = uniqBy([...dataFiles, ...files], 'name');
|
||||||
const res = await app.update({ data: { ...app.data, files: newFiles } });
|
const res = await app.update({ data: { ...app.data, files: newFiles } });
|
||||||
if (version === am.version && !appIsNew) {
|
if (version === am.version && !appIsNew) {
|
||||||
await am.update({ data: { ...am.data, files: newFiles } });
|
await am.update({ data: { ...am.data, files: newFiles } });
|
||||||
@@ -217,13 +229,25 @@ app
|
|||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { id, username } = ctx.query.data;
|
const { id, username, appKey, version } = ctx.query.data;
|
||||||
if (!id) {
|
if (!id && !appKey) {
|
||||||
throw new CustomError('id is required');
|
throw new CustomError('id or appKey is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
const uid = await getUidByUsername(app, ctx, username);
|
const uid = await getUidByUsername(app, ctx, username);
|
||||||
const appList = await AppListModel.findByPk(id);
|
let appList: AppListModel | null = null;
|
||||||
|
if (id) {
|
||||||
|
appList = await AppListModel.findByPk(id);
|
||||||
|
if (appList?.uid !== uid) {
|
||||||
|
throw new CustomError('no permission');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!appList && appKey) {
|
||||||
|
if (!version) {
|
||||||
|
throw new CustomError('version is required');
|
||||||
|
}
|
||||||
|
appList = await AppListModel.findOne({ where: { key: appKey, version, uid } });
|
||||||
|
}
|
||||||
if (!appList) {
|
if (!appList) {
|
||||||
throw new CustomError('app not found');
|
throw new CustomError('app not found');
|
||||||
}
|
}
|
||||||
@@ -265,7 +289,6 @@ app
|
|||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'app',
|
path: 'app',
|
||||||
@@ -287,7 +310,7 @@ app
|
|||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'app',
|
path: 'app',
|
||||||
key: 'detect-version-list',
|
key: 'detectVersionList',
|
||||||
description: '检测版本列表,minio中的数据自己上传后,根据版本信息,进行替换',
|
description: '检测版本列表,minio中的数据自己上传后,根据版本信息,进行替换',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
})
|
})
|
||||||
@@ -320,7 +343,7 @@ app
|
|||||||
let appListFiles = appList.data?.files || [];
|
let appListFiles = appList.data?.files || [];
|
||||||
const needAddFiles = newFiles.map((item) => {
|
const needAddFiles = newFiles.map((item) => {
|
||||||
const findFile = appListFiles.find((appListFile) => appListFile.name === item.name);
|
const findFile = appListFiles.find((appListFile) => appListFile.name === item.name);
|
||||||
if (findFile && findFile.path === item.path) {
|
if (findFile && findFile.name === item.name) {
|
||||||
return { ...findFile, ...item };
|
return { ...findFile, ...item };
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
|
|||||||
@@ -43,6 +43,55 @@ export class AppModel extends Model {
|
|||||||
declare proxy: boolean;
|
declare proxy: boolean;
|
||||||
declare user: string;
|
declare user: string;
|
||||||
declare status: string;
|
declare status: string;
|
||||||
|
static async moveToNewUser(oldUserName: string, newUserName: string) {
|
||||||
|
const appIds = await AppModel.findAll({
|
||||||
|
where: {
|
||||||
|
user: oldUserName,
|
||||||
|
},
|
||||||
|
attributes: ['id'],
|
||||||
|
});
|
||||||
|
for (const app of appIds) {
|
||||||
|
const appData = await AppModel.findByPk(app.id);
|
||||||
|
appData.user = newUserName;
|
||||||
|
const data = appData.data;
|
||||||
|
data.files = await AppModel.getNewFiles(data.files, {
|
||||||
|
oldUser: oldUserName,
|
||||||
|
newUser: newUserName,
|
||||||
|
});
|
||||||
|
appData.data = { ...data };
|
||||||
|
await appData.save({ fields: ['data', 'user'] });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async getNewFiles(files: { name: string; path: string }[] = [], opts: { oldUser: string; newUser: string } = { oldUser: '', newUser: '' }) {
|
||||||
|
const { oldUser, newUser } = opts;
|
||||||
|
const _ = files.map((item) => {
|
||||||
|
if (item.path.startsWith('http')) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
if (oldUser && item.path.startsWith(oldUser)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
const paths = item.path.split('/');
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
path: newUser + '/' + paths.slice(1).join('/'),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return _;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPublic() {
|
||||||
|
const value = this.toJSON();
|
||||||
|
// 删除不需要的字段
|
||||||
|
const data = value.data;
|
||||||
|
if (data && data.permission) {
|
||||||
|
delete data.permission.usernames;
|
||||||
|
delete data.permission.password;
|
||||||
|
delete data.permission['expiration-time'];
|
||||||
|
}
|
||||||
|
value.data = data;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
AppModel.init(
|
AppModel.init(
|
||||||
{
|
{
|
||||||
|
|||||||
1
src/routes/app-manager/proxy/index.ts
Normal file
1
src/routes/app-manager/proxy/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import './page-proxy.ts';
|
||||||
29
src/routes/app-manager/proxy/page-proxy.ts
Normal file
29
src/routes/app-manager/proxy/page-proxy.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { app, redis } from '@/app.ts';
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'page-proxy-app',
|
||||||
|
key: 'status',
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
//
|
||||||
|
const { user, app } = ctx.query;
|
||||||
|
if (!user || !app) {
|
||||||
|
ctx.body = {
|
||||||
|
code: 400,
|
||||||
|
message: 'user and app are required',
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const key = `user:app:status:${app}:${user}`;
|
||||||
|
const status = await redis.get(key);
|
||||||
|
if (!status) {
|
||||||
|
ctx.throw(404, 'status not found');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const parsedStatus = JSON.parse(status);
|
||||||
|
ctx.body = parsedStatus;
|
||||||
|
} catch (e) {
|
||||||
|
ctx.throw(400, 'status is not a valid json');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
@@ -1 +1,3 @@
|
|||||||
import './list.ts';
|
import './list.ts';
|
||||||
|
|
||||||
|
import './post.ts'
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { app } from '@/app.ts';
|
import { app } from '@/app.ts';
|
||||||
import { AppModel } from '../module/index.ts';
|
import { AppModel } from '../module/index.ts';
|
||||||
|
import { ConfigPermission } from '@kevisual/permission';
|
||||||
|
|
||||||
// curl http://localhost:4005/api/router?path=app&key=public-list
|
// curl http://localhost:4005/api/router?path=app&key=public-list
|
||||||
// TODO:
|
// TODO:
|
||||||
@@ -9,15 +10,28 @@ app
|
|||||||
key: 'public-list',
|
key: 'public-list',
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const list = await AppModel.findAll({
|
const { username = 'root', status = 'running', page = 1, pageSize = 100, order = 'DESC' } = ctx.query.data || {};
|
||||||
|
const { rows, count } = await AppModel.findAndCountAll({
|
||||||
where: {
|
where: {
|
||||||
status: 'running',
|
status,
|
||||||
|
user: username,
|
||||||
},
|
},
|
||||||
// attributes: {
|
attributes: {
|
||||||
// exclude: ['data'],
|
exclude: [],
|
||||||
// },
|
},
|
||||||
|
order: [['updatedAt', order]],
|
||||||
|
limit: pageSize,
|
||||||
|
offset: (page - 1) * pageSize,
|
||||||
|
distinct: true,
|
||||||
logging: false,
|
logging: false,
|
||||||
});
|
});
|
||||||
ctx.body = list;
|
ctx.body = {
|
||||||
|
list: rows.map((item) => {
|
||||||
|
return ConfigPermission.getDataPublicPermission(item.toJSON());
|
||||||
|
}),
|
||||||
|
pagination: {
|
||||||
|
total: count,
|
||||||
|
},
|
||||||
|
};
|
||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
|||||||
111
src/routes/app-manager/public/post.ts
Normal file
111
src/routes/app-manager/public/post.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
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: `/${username}/${key}/`,
|
||||||
|
username: username,
|
||||||
|
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);
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { AppModel, AppListModel } from './module/index.ts';
|
import { AppModel, AppListModel } from './module/index.ts';
|
||||||
import { app } from '@/app.ts';
|
import { app } from '@/app.ts';
|
||||||
import { setExpire } from './revoke.ts';
|
import { setExpire } from './revoke.ts';
|
||||||
|
import { deleteFileByPrefix } from '../file/index.ts';
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
@@ -38,20 +39,21 @@ app
|
|||||||
if (!id && !key) {
|
if (!id && !key) {
|
||||||
ctx.throw(500, 'id is required');
|
ctx.throw(500, 'id is required');
|
||||||
}
|
}
|
||||||
|
let am: AppModel;
|
||||||
if (id) {
|
if (id) {
|
||||||
const am = await AppModel.findByPk(id);
|
am = await AppModel.findByPk(id);
|
||||||
if (!am) {
|
if (!am) {
|
||||||
ctx.throw(500, 'app not found');
|
ctx.throw(500, 'app not found');
|
||||||
}
|
}
|
||||||
ctx.body = am;
|
|
||||||
} else {
|
} else {
|
||||||
const am = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
|
am = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
|
||||||
if (!am) {
|
if (!am) {
|
||||||
ctx.throw(500, 'app not found');
|
ctx.throw(500, 'app not found');
|
||||||
}
|
}
|
||||||
ctx.body = am;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.body = am;
|
||||||
|
|
||||||
return ctx;
|
return ctx;
|
||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
@@ -65,14 +67,22 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
|
||||||
const { data, id, ...rest } = ctx.query.data;
|
const { data, id, user, ...rest } = ctx.query.data;
|
||||||
if (id) {
|
if (id) {
|
||||||
const app = await AppModel.findByPk(id);
|
const app = await AppModel.findByPk(id);
|
||||||
if (app) {
|
if (app) {
|
||||||
const newData = { ...app.data, ...data };
|
const newData = { ...app.data, ...data };
|
||||||
|
if (app.user !== tokenUser.username) {
|
||||||
|
rest.user = tokenUser.username;
|
||||||
|
let files = newData?.files || [];
|
||||||
|
if (files.length > 0) {
|
||||||
|
files = await AppModel.getNewFiles(files, { oldUser: app.user, newUser: tokenUser.username });
|
||||||
|
}
|
||||||
|
newData.files = files;
|
||||||
|
}
|
||||||
const newApp = await app.update({ data: newData, ...rest });
|
const newApp = await app.update({ data: newData, ...rest });
|
||||||
ctx.body = newApp;
|
ctx.body = newApp;
|
||||||
if (app.status !== 'running') {
|
if (app.status !== 'running' || data?.share || rest?.status) {
|
||||||
setExpire(newApp.key, app.user);
|
setExpire(newApp.key, app.user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -107,6 +117,7 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const id = ctx.query.id;
|
const id = ctx.query.id;
|
||||||
|
const deleteFile = !!ctx.query.deleteFile; // 是否删除文件, 默认不删除
|
||||||
if (!id) {
|
if (!id) {
|
||||||
ctx.throw(500, 'id is required');
|
ctx.throw(500, 'id is required');
|
||||||
}
|
}
|
||||||
@@ -120,6 +131,10 @@ app
|
|||||||
const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } });
|
const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } });
|
||||||
await am.destroy({ force: true });
|
await am.destroy({ force: true });
|
||||||
await Promise.all(list.map((item) => item.destroy({ force: true })));
|
await Promise.all(list.map((item) => item.destroy({ force: true })));
|
||||||
|
if (deleteFile) {
|
||||||
|
const username = tokenUser.username;
|
||||||
|
await deleteFileByPrefix(`${username}/${am.key}`);
|
||||||
|
}
|
||||||
ctx.body = 'success';
|
ctx.body = 'success';
|
||||||
return ctx;
|
return ctx;
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,13 +3,29 @@ import { App } from '@kevisual/router';
|
|||||||
type Opts = {
|
type Opts = {
|
||||||
prefix: string;
|
prefix: string;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* fix path
|
||||||
|
* @param data
|
||||||
|
* @param prefix
|
||||||
|
* @param opts
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export const prefixFix = (data: any, prefix: string, opts?: Opts) => {
|
export const prefixFix = (data: any, prefix: string, opts?: Opts) => {
|
||||||
const len = prefix.length || 0;
|
const len = prefix.length || 0;
|
||||||
|
console.log('prefixFix', prefix, opts?.prefix);
|
||||||
|
const wrapperPrefix = (path: string, prefix: string) => {
|
||||||
|
if (prefix) {
|
||||||
|
return prefix + '/' + path;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
};
|
||||||
if (data.data.files) {
|
if (data.data.files) {
|
||||||
data.data.files = data.data.files.map((item) => {
|
data.data.files = data.data.files.map((item) => {
|
||||||
|
const paths = item.path.split('/').filter((item) => item);
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
path: item.path.slice(len + 1),
|
path: wrapperPrefix(paths.slice(1).join('/'), ''),
|
||||||
|
origin: item.path,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { oss } from '@/app.ts';
|
|||||||
import { ConfigOssService } from '@kevisual/oss/services';
|
import { ConfigOssService } from '@kevisual/oss/services';
|
||||||
import { User } from '@/models/user.ts';
|
import { User } from '@/models/user.ts';
|
||||||
import { defaultKeys } from './models/default-keys.ts';
|
import { defaultKeys } from './models/default-keys.ts';
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'config',
|
path: 'config',
|
||||||
@@ -20,7 +21,7 @@ app
|
|||||||
const user = new User();
|
const user = new User();
|
||||||
user.setTokenUser(tokenUser);
|
user.setTokenUser(tokenUser);
|
||||||
const isAdmin = await user.hasUser('admin');
|
const isAdmin = await user.hasUser('admin');
|
||||||
const usersConfig = ['upload.json', 'workspace.json', 'ai.json'];
|
const usersConfig = ['upload.json', 'workspace.json', 'ai.json', 'user.json'];
|
||||||
const adminConfig = ['vip.json'];
|
const adminConfig = ['vip.json'];
|
||||||
const configs = [...usersConfig, ...(isAdmin ? adminConfig : [])];
|
const configs = [...usersConfig, ...(isAdmin ? adminConfig : [])];
|
||||||
if (!configs.includes(configKey)) {
|
if (!configs.includes(configKey)) {
|
||||||
@@ -34,6 +35,8 @@ app
|
|||||||
uid: tokenUser.id,
|
uid: tokenUser.id,
|
||||||
},
|
},
|
||||||
defaults: {
|
defaults: {
|
||||||
|
title: defaultConfig?.key,
|
||||||
|
description: defaultConfig?.description || '',
|
||||||
key: configKey,
|
key: configKey,
|
||||||
uid: tokenUser.id,
|
uid: tokenUser.id,
|
||||||
data: defaultConfig?.data,
|
data: defaultConfig?.data,
|
||||||
|
|||||||
@@ -1,18 +1,33 @@
|
|||||||
export const defaultKeys = [
|
export const defaultKeys = [
|
||||||
{
|
{
|
||||||
key: 'upload.json',
|
key: 'upload.json',
|
||||||
|
description: '上传配置',
|
||||||
data: { key: 'upload', version: '1.0.0' },
|
data: { key: 'upload', version: '1.0.0' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'workspace.json',
|
key: 'workspace.json',
|
||||||
|
description: '工作空间配置',
|
||||||
data: { key: 'workspace', version: '1.0.0' },
|
data: { key: 'workspace', version: '1.0.0' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'ai.json',
|
key: 'ai.json',
|
||||||
data: { key: 'ai', version: '1.0.0' },
|
description: 'AI配置',
|
||||||
|
data: {
|
||||||
|
title: 'AI Secret Config',
|
||||||
|
description: 'AI Secret配置, 请根据需要配置',
|
||||||
|
version: '1.0.0',
|
||||||
|
models: [],
|
||||||
|
secretKeys: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'vip.json',
|
key: 'vip.json',
|
||||||
|
description: 'VIP配置',
|
||||||
data: { key: 'vip', version: '1.0.0' },
|
data: { key: 'vip', version: '1.0.0' },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: 'user.json',
|
||||||
|
description: '用户配置',
|
||||||
|
data: { key: 'user', version: '1.0.0', redirectURL: '/root/center/' },
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useContextKey } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/context';
|
||||||
import { sequelize } from '../../../modules/sequelize.ts';
|
import { sequelize } from '../../../modules/sequelize.ts';
|
||||||
import { DataTypes, Model } from 'sequelize';
|
import { DataTypes, Model } from 'sequelize';
|
||||||
import { Permission } from '@kevisual/permission';
|
import { Permission } from '@kevisual/permission';
|
||||||
|
|||||||
1
src/routes/file-listener/index.ts
Normal file
1
src/routes/file-listener/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import './list.ts';
|
||||||
107
src/routes/file-listener/list.ts
Normal file
107
src/routes/file-listener/list.ts
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
import { Op } from 'sequelize';
|
||||||
|
import { app } from '@/app.ts';
|
||||||
|
import { FileSyncModel } from './model.ts';
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'file-listener',
|
||||||
|
key: 'list',
|
||||||
|
middleware: ['auth'],
|
||||||
|
description: '获取用户的某一个文件夹下的所有的列表的数据',
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const username = tokenUser.username;
|
||||||
|
const { page = 1, pageSize = 20, sort = 'DESC' } = ctx.query;
|
||||||
|
let { prefix } = ctx.query;
|
||||||
|
if (prefix) {
|
||||||
|
if (typeof prefix !== 'string') {
|
||||||
|
ctx.throw(400, 'prefix must be a string');
|
||||||
|
}
|
||||||
|
if (prefix.startsWith('/')) {
|
||||||
|
prefix = prefix.slice(1); // Remove leading slash if present
|
||||||
|
}
|
||||||
|
if (!prefix.startsWith(username + '/')) {
|
||||||
|
ctx.throw(400, 'prefix must start with the your username:', username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const searchWhere = prefix
|
||||||
|
? {
|
||||||
|
[Op.or]: [{ name: { [Op.like]: `${prefix}%` } }],
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
|
||||||
|
const { rows: files, count } = await FileSyncModel.findAndCountAll({
|
||||||
|
where: {
|
||||||
|
...searchWhere,
|
||||||
|
},
|
||||||
|
offset: (page - 1) * pageSize,
|
||||||
|
limit: pageSize,
|
||||||
|
order: [['updatedAt', sort]],
|
||||||
|
});
|
||||||
|
const getPublicFiles = (files: FileSyncModel[]) => {
|
||||||
|
return files.map((file) => {
|
||||||
|
const value = file.toJSON();
|
||||||
|
const stat = value.stat || {};
|
||||||
|
delete stat.password;
|
||||||
|
return {
|
||||||
|
...value,
|
||||||
|
stat: stat,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
list: getPublicFiles(files),
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
current: page,
|
||||||
|
pageSize,
|
||||||
|
total: count,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'file-listener',
|
||||||
|
key: 'get',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const username = tokenUser.username;
|
||||||
|
const { id, name, hash } = ctx.query.data || {};
|
||||||
|
|
||||||
|
if (!id && !name && !hash) {
|
||||||
|
ctx.throw(400, 'id, name or hash is required');
|
||||||
|
}
|
||||||
|
let fileSync: FileSyncModel | null = null;
|
||||||
|
if (id) {
|
||||||
|
fileSync = await FileSyncModel.findByPk(id);
|
||||||
|
}
|
||||||
|
if (name && !fileSync) {
|
||||||
|
fileSync = await FileSyncModel.findOne({
|
||||||
|
where: {
|
||||||
|
name,
|
||||||
|
hash,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!fileSync && hash) {
|
||||||
|
fileSync = await FileSyncModel.findOne({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
[Op.like]: `${username}/%`,
|
||||||
|
},
|
||||||
|
hash,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fileSync || !fileSync.name.startsWith(`${username}/`)) {
|
||||||
|
ctx.throw(404, 'NotFoundFile');
|
||||||
|
}
|
||||||
|
ctx.body = fileSync;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
3
src/routes/file-listener/model.ts
Normal file
3
src/routes/file-listener/model.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { FileSyncModel } from '@kevisual/file-listener/src/file-sync/model.ts';
|
||||||
|
import type { FileSyncModelType } from '@kevisual/file-listener/src/file-sync/model.ts';
|
||||||
|
export { FileSyncModel, FileSyncModelType };
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { minioClient } from '@/app.ts';
|
import dayjs from 'dayjs';
|
||||||
import { bucketName } from '@/modules/minio.ts';
|
import { minioClient } from '../../../modules/minio.ts';
|
||||||
import { CopyDestinationOptions, CopySourceOptions } from 'minio';
|
import { bucketName } from '../../../modules/minio.ts';
|
||||||
|
import { BucketItemStat, CopyDestinationOptions, CopySourceOptions } from 'minio';
|
||||||
type MinioListOpt = {
|
type MinioListOpt = {
|
||||||
prefix: string;
|
prefix: string;
|
||||||
recursive?: boolean;
|
recursive?: boolean;
|
||||||
@@ -42,7 +43,7 @@ export const getMinioList = async <IS_FILE extends boolean>(opts: MinioListOpt):
|
|||||||
});
|
});
|
||||||
return res as IS_FILE extends true ? MinioFile[] : MinioDirectory[];
|
return res as IS_FILE extends true ? MinioFile[] : MinioDirectory[];
|
||||||
};
|
};
|
||||||
export const getFileStat = async (prefix: string, isFile?: boolean): Promise<any> => {
|
export const getFileStat = async (prefix: string, isFile?: boolean): Promise<BucketItemStat | null> => {
|
||||||
try {
|
try {
|
||||||
const obj = await minioClient.statObject(bucketName, prefix);
|
const obj = await minioClient.statObject(bucketName, prefix);
|
||||||
if (isFile && obj.size === 0) {
|
if (isFile && obj.size === 0) {
|
||||||
@@ -95,7 +96,17 @@ export const deleteFiles = async (prefixs: string[]): Promise<any> => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const deleteFileByPrefix = async (prefix: string): Promise<any> => {
|
||||||
|
try {
|
||||||
|
const allFiles = await getMinioList<true>({ prefix, recursive: true });
|
||||||
|
const files = allFiles.filter((item) => item.name.startsWith(prefix));
|
||||||
|
await deleteFiles(files.map((item) => item.name));
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('delete File Error not handle', e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
type GetMinioListAndSetToAppListOpts = {
|
type GetMinioListAndSetToAppListOpts = {
|
||||||
username: string;
|
username: string;
|
||||||
appKey: string;
|
appKey: string;
|
||||||
@@ -148,3 +159,62 @@ export const updateFileStat = async (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将用户A的文件移动到用户B
|
||||||
|
* @param usernameA
|
||||||
|
* @param usernameB
|
||||||
|
* @param clearOldUser 是否清除用户A的文件
|
||||||
|
*/
|
||||||
|
export const mvUserAToUserB = async (usernameA: string, usernameB: string, clearOldUser = false) => {
|
||||||
|
const oldPrefix = `${usernameA}/`;
|
||||||
|
const newPrefix = `${usernameB}/`;
|
||||||
|
const listSource = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
|
||||||
|
for (const item of listSource) {
|
||||||
|
const source = new CopySourceOptions({ Bucket: bucketName, Object: item.name });
|
||||||
|
const stat = await getFileStat(item.name);
|
||||||
|
const newName = item.name.slice(oldPrefix.length);
|
||||||
|
// @ts-ignore
|
||||||
|
const metadata = stat?.userMetadata || stat.metaData;
|
||||||
|
const destination = new CopyDestinationOptions({
|
||||||
|
Bucket: bucketName,
|
||||||
|
Object: `${newPrefix}${newName}`,
|
||||||
|
UserMetadata: metadata,
|
||||||
|
MetadataDirective: 'COPY',
|
||||||
|
});
|
||||||
|
await minioClient.copyObject(source, destination);
|
||||||
|
}
|
||||||
|
if (clearOldUser) {
|
||||||
|
const files = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
|
||||||
|
for (const file of files) {
|
||||||
|
await minioClient.removeObject(bucketName, file.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const backupUserA = async (usernameA: string, id: string, backName?: string) => {
|
||||||
|
const today = backName || dayjs().format('YYYY-MM-DD-HH-mm');
|
||||||
|
const backupAllPrefix = `private/backup/${id}/`;
|
||||||
|
const backupPrefix = `private/backup/${id}/${today}`;
|
||||||
|
const backupList = await getMinioList<false>({ prefix: backupAllPrefix });
|
||||||
|
const backupListSort = backupList.sort((a, b) => -a.prefix.localeCompare(b.prefix));
|
||||||
|
if (backupListSort.length > 2) {
|
||||||
|
const deleteBackup = backupListSort.slice(2);
|
||||||
|
for (const item of deleteBackup) {
|
||||||
|
const files = await getMinioList<true>({ prefix: item.prefix, recursive: true });
|
||||||
|
for (const file of files) {
|
||||||
|
await minioClient.removeObject(bucketName, file.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await mvUserAToUserB(usernameA, backupPrefix, false);
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* 删除用户
|
||||||
|
* @param username
|
||||||
|
*/
|
||||||
|
export const deleteUser = async (username: string) => {
|
||||||
|
const list = await getMinioList<true>({ prefix: `${username}/`, recursive: true });
|
||||||
|
for (const item of list) {
|
||||||
|
await minioClient.removeObject(bucketName, item.name);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -13,3 +13,5 @@ import './micro-app/index.ts';
|
|||||||
import './config/index.ts';
|
import './config/index.ts';
|
||||||
|
|
||||||
import './mark/index.ts';
|
import './mark/index.ts';
|
||||||
|
|
||||||
|
import './file-listener/index.ts';
|
||||||
|
|||||||
@@ -116,10 +116,11 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'update',
|
key: 'update',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
|
isDebug: true,
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { id, ...data } = ctx.query.data || {};
|
const { id, createdAt, updatedAt, uid: _, puid: _2, uname: _3, data, ...rest } = ctx.query.data || {};
|
||||||
let markModel: MarkModel;
|
let markModel: MarkModel;
|
||||||
if (id) {
|
if (id) {
|
||||||
markModel = await MarkModel.findByPk(id);
|
markModel = await MarkModel.findByPk(id);
|
||||||
@@ -130,10 +131,19 @@ app
|
|||||||
ctx.throw(403, 'no permission');
|
ctx.throw(403, 'no permission');
|
||||||
}
|
}
|
||||||
const version = Number(markModel.version) + 1;
|
const version = Number(markModel.version) + 1;
|
||||||
await markModel.update({ ...markModel.data, ...data, version });
|
await markModel.update({
|
||||||
|
...markModel.data,
|
||||||
|
...rest,
|
||||||
|
data: {
|
||||||
|
...markModel.data,
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
version,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
markModel = await MarkModel.create({
|
markModel = await MarkModel.create({
|
||||||
...data,
|
data,
|
||||||
|
...rest,
|
||||||
uname: tokenUser.username,
|
uname: tokenUser.username,
|
||||||
uid: tokenUser.id,
|
uid: tokenUser.id,
|
||||||
puid: tokenUser.uid,
|
puid: tokenUser.uid,
|
||||||
|
|||||||
327
src/routes/mark/mark-model.ts
Normal file
327
src/routes/mark/mark-model.ts
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
import { useContextKey } from '@kevisual/context';
|
||||||
|
import { nanoid, customAlphabet } from 'nanoid';
|
||||||
|
import { DataTypes, Model, ModelAttributes } from 'sequelize';
|
||||||
|
import type { Sequelize } from 'sequelize';
|
||||||
|
export const random = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
|
||||||
|
export type Mark = Partial<InstanceType<typeof MarkModel>>;
|
||||||
|
export type MarkData = {
|
||||||
|
md?: string; // markdown
|
||||||
|
mdList?: string[]; // markdown list
|
||||||
|
type?: string; // 类型 markdown | json | html | image | video | audio | code | link | file
|
||||||
|
data?: any;
|
||||||
|
key?: string; // 文件的名称, 唯一
|
||||||
|
push?: boolean; // 是否推送到elasticsearch
|
||||||
|
pushTime?: Date; // 推送时间
|
||||||
|
summary?: string; // 摘要
|
||||||
|
nodes?: MarkDataNode[]; // 节点
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
export type MarkFile = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
url: string;
|
||||||
|
size: number;
|
||||||
|
type: 'self' | 'data' | 'generate'; // generate为生成文件
|
||||||
|
query: string; // 'data.nodes[id].content';
|
||||||
|
hash: string;
|
||||||
|
fileKey: string; // 文件的名称, 唯一
|
||||||
|
};
|
||||||
|
export type MarkDataNode = {
|
||||||
|
id?: string;
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
export type MarkConfig = {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
export type MarkAuth = {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* 隐秘内容
|
||||||
|
* auth
|
||||||
|
* config
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export class MarkModel extends Model {
|
||||||
|
declare id: string;
|
||||||
|
declare title: string; // 标题,可以ai生成
|
||||||
|
declare description: string; // 描述,可以ai生成
|
||||||
|
declare cover: string; // 封面,可以ai生成
|
||||||
|
declare thumbnail: string; // 缩略图
|
||||||
|
declare key: string; // 文件路径
|
||||||
|
declare markType: string; // markdown | json | html | image | video | audio | code | link | file
|
||||||
|
declare link: string; // 访问链接
|
||||||
|
declare tags: string[]; // 标签
|
||||||
|
declare summary: string; // 摘要, description的简化版
|
||||||
|
declare data: MarkData; // 数据
|
||||||
|
|
||||||
|
declare uid: string; // 操作用户的id
|
||||||
|
declare puid: string; // 父级用户的id, 真实用户
|
||||||
|
declare config: MarkConfig; // mark属于一定不会暴露的内容。
|
||||||
|
|
||||||
|
declare fileList: MarkFile[]; // 文件管理
|
||||||
|
declare uname: string; // 用户的名称, 或者着别名
|
||||||
|
|
||||||
|
declare markedAt: Date; // 标记时间
|
||||||
|
declare createdAt: Date;
|
||||||
|
declare updatedAt: Date;
|
||||||
|
declare version: number;
|
||||||
|
/**
|
||||||
|
* 加锁更新data中的node的节点,通过node的id
|
||||||
|
* @param param0
|
||||||
|
*/
|
||||||
|
static async updateJsonNode(id: string, node: MarkDataNode, opts?: { operate?: 'update' | 'delete'; Model?: any; sequelize?: Sequelize }) {
|
||||||
|
const sequelize = opts?.sequelize || (await useContextKey('sequelize'));
|
||||||
|
const transaction = await sequelize.transaction(); // 开启事务
|
||||||
|
const operate = opts.operate || 'update';
|
||||||
|
const isUpdate = operate === 'update';
|
||||||
|
const Model = opts.Model || MarkModel;
|
||||||
|
try {
|
||||||
|
// 1. 获取当前的 JSONB 字段值(加锁)
|
||||||
|
const mark = await Model.findByPk(id, {
|
||||||
|
transaction,
|
||||||
|
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
||||||
|
});
|
||||||
|
if (!mark) {
|
||||||
|
throw new Error('Mark not found');
|
||||||
|
}
|
||||||
|
// 2. 修改特定的数组元素
|
||||||
|
const data = mark.data as MarkData;
|
||||||
|
const items = data.nodes;
|
||||||
|
if (!node.id) {
|
||||||
|
node.id = random(12);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 找到要更新的元素
|
||||||
|
const itemIndex = items.findIndex((item) => item.id === node.id);
|
||||||
|
if (itemIndex === -1) {
|
||||||
|
isUpdate && items.push(node);
|
||||||
|
} else {
|
||||||
|
if (isUpdate) {
|
||||||
|
items[itemIndex] = node;
|
||||||
|
} else {
|
||||||
|
items.splice(itemIndex, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const version = Number(mark.version) + 1;
|
||||||
|
// 4. 更新 JSONB 字段
|
||||||
|
const result = await mark.update(
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...data,
|
||||||
|
nodes: items,
|
||||||
|
},
|
||||||
|
version,
|
||||||
|
},
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async updateJsonNodes(id: string, nodes: { node: MarkDataNode; operate?: 'update' | 'delete' }[], opts?: { Model?: any; sequelize?: Sequelize }) {
|
||||||
|
const sequelize = opts?.sequelize || (await useContextKey('sequelize'));
|
||||||
|
const transaction = await sequelize.transaction(); // 开启事务
|
||||||
|
const Model = opts?.Model || MarkModel;
|
||||||
|
try {
|
||||||
|
const mark = await Model.findByPk(id, {
|
||||||
|
transaction,
|
||||||
|
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
||||||
|
});
|
||||||
|
if (!mark) {
|
||||||
|
throw new Error('Mark not found');
|
||||||
|
}
|
||||||
|
const data = mark.data as MarkData;
|
||||||
|
const _nodes = data.nodes || [];
|
||||||
|
// 过滤不在nodes中的节点
|
||||||
|
const blankNodes = nodes.filter((node) => !_nodes.find((n) => n.id === node.node.id)).map((node) => node.node);
|
||||||
|
// 更新或删除节点
|
||||||
|
const newNodes = _nodes
|
||||||
|
.map((node) => {
|
||||||
|
const nodeOperate = nodes.find((n) => n.node.id === node.id);
|
||||||
|
if (nodeOperate) {
|
||||||
|
if (nodeOperate.operate === 'delete') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return nodeOperate.node;
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
})
|
||||||
|
.filter((node) => node !== null);
|
||||||
|
const version = Number(mark.version) + 1;
|
||||||
|
const result = await mark.update(
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...data,
|
||||||
|
nodes: [...blankNodes, ...newNodes],
|
||||||
|
},
|
||||||
|
version,
|
||||||
|
},
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
|
await transaction.commit();
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static async updateData(id: string, data: MarkData, opts: { Model?: any; sequelize?: Sequelize }) {
|
||||||
|
const sequelize = opts.sequelize || (await useContextKey('sequelize'));
|
||||||
|
const transaction = await sequelize.transaction(); // 开启事务
|
||||||
|
const Model = opts.Model || MarkModel;
|
||||||
|
const mark = await Model.findByPk(id, {
|
||||||
|
transaction,
|
||||||
|
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
||||||
|
});
|
||||||
|
if (!mark) {
|
||||||
|
throw new Error('Mark not found');
|
||||||
|
}
|
||||||
|
const version = Number(mark.version) + 1;
|
||||||
|
const result = await mark.update(
|
||||||
|
{
|
||||||
|
...mark.data,
|
||||||
|
...data,
|
||||||
|
data: {
|
||||||
|
...mark.data,
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
version,
|
||||||
|
},
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
|
await transaction.commit();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
static async createNew(data: any, opts: { Model?: any; sequelize?: Sequelize }) {
|
||||||
|
const sequelize = opts.sequelize || (await useContextKey('sequelize'));
|
||||||
|
const transaction = await sequelize.transaction(); // 开启事务
|
||||||
|
const Model = opts.Model || MarkModel;
|
||||||
|
const result = await Model.create({ ...data, version: 1 }, { transaction });
|
||||||
|
await transaction.commit();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export type MarkInitOpts<T = any> = {
|
||||||
|
tableName: string;
|
||||||
|
sequelize?: Sequelize;
|
||||||
|
callInit?: (attribute: ModelAttributes) => ModelAttributes;
|
||||||
|
Model?: T extends typeof MarkModel ? T : typeof MarkModel;
|
||||||
|
};
|
||||||
|
export type Opts = {
|
||||||
|
sync?: boolean;
|
||||||
|
alter?: boolean;
|
||||||
|
logging?: boolean | ((...args: any) => any);
|
||||||
|
force?: boolean;
|
||||||
|
};
|
||||||
|
export const MarkMInit = async <T = any>(opts: MarkInitOpts<T>, sync?: Opts) => {
|
||||||
|
const sequelize = await useContextKey('sequelize');
|
||||||
|
opts.sequelize = opts.sequelize || sequelize;
|
||||||
|
const { callInit, Model, ...optsRest } = opts;
|
||||||
|
const modelAttribute = {
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
primaryKey: true,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
comment: 'id',
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
key: {
|
||||||
|
type: DataTypes.TEXT, // 对应的minio的文件路径
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
markType: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: 'md', // markdown | json | html | image | video | audio | code | link | file
|
||||||
|
comment: '类型',
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
},
|
||||||
|
cover: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
comment: '封面',
|
||||||
|
},
|
||||||
|
thumbnail: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
comment: '缩略图',
|
||||||
|
},
|
||||||
|
link: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
comment: '链接',
|
||||||
|
},
|
||||||
|
tags: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: [],
|
||||||
|
},
|
||||||
|
summary: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
defaultValue: '',
|
||||||
|
comment: '摘要',
|
||||||
|
},
|
||||||
|
config: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: {},
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: {},
|
||||||
|
},
|
||||||
|
fileList: {
|
||||||
|
type: DataTypes.JSONB,
|
||||||
|
defaultValue: [],
|
||||||
|
},
|
||||||
|
uname: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
defaultValue: '',
|
||||||
|
comment: '用户的名称, 更新后的用户的名称',
|
||||||
|
},
|
||||||
|
version: {
|
||||||
|
type: DataTypes.INTEGER, // 更新刷新版本,多人协作
|
||||||
|
defaultValue: 1,
|
||||||
|
},
|
||||||
|
markedAt: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: true,
|
||||||
|
comment: '标记时间',
|
||||||
|
},
|
||||||
|
uid: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
puid: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const InitModel = Model || MarkModel;
|
||||||
|
InitModel.init(callInit ? callInit(modelAttribute) : modelAttribute, {
|
||||||
|
sequelize,
|
||||||
|
paranoid: true,
|
||||||
|
...optsRest,
|
||||||
|
});
|
||||||
|
if (sync && sync.sync) {
|
||||||
|
const { sync: _, ...rest } = sync;
|
||||||
|
MarkModel.sync({ alter: true, logging: false, ...rest }).catch((e) => {
|
||||||
|
console.error('MarkModel sync', e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const markModelInit = MarkMInit;
|
||||||
|
|
||||||
|
export const syncMarkModel = async (sync?: Opts, tableName = 'micro_mark') => {
|
||||||
|
const sequelize = await useContextKey('sequelize');
|
||||||
|
await MarkMInit({ sequelize, tableName }, sync);
|
||||||
|
};
|
||||||
@@ -1,319 +1,5 @@
|
|||||||
import { useContextKey } from '@kevisual/use-config/context';
|
export * from '@kevisual/code-center-module/src/mark/mark-model.ts';
|
||||||
import { nanoid, customAlphabet } from 'nanoid';
|
import { markModelInit, MarkModel, syncMarkModel } from '@kevisual/code-center-module/src/mark/mark-model.ts';
|
||||||
import { DataTypes, Model, ModelAttributes } from 'sequelize';
|
export { markModelInit, MarkModel };
|
||||||
import type { Sequelize } from 'sequelize';
|
|
||||||
export const random = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
|
|
||||||
export type Mark = Partial<InstanceType<typeof MarkModel>>;
|
|
||||||
export type MarkData = {
|
|
||||||
md?: string; // markdown
|
|
||||||
mdList?: string[]; // markdown list
|
|
||||||
type?: string; // 类型 markdown | json | html | image | video | audio | code | link | file
|
|
||||||
data?: any;
|
|
||||||
push?: boolean; // 是否推送到elasticsearch
|
|
||||||
pushTime?: Date; // 推送时间
|
|
||||||
summary?: string; // 摘要
|
|
||||||
nodes?: MarkDataNode[]; // 节点
|
|
||||||
[key: string]: any;
|
|
||||||
};
|
|
||||||
export type MarkFile = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
url: string;
|
|
||||||
size: number;
|
|
||||||
type: 'self' | 'data' | 'generate'; // generate为生成文件
|
|
||||||
query: string; // 'data.nodes[id].content';
|
|
||||||
hash: string;
|
|
||||||
fileKey: string; // 文件的名称, 唯一
|
|
||||||
};
|
|
||||||
export type MarkDataNode = {
|
|
||||||
id?: string;
|
|
||||||
[key: string]: any;
|
|
||||||
};
|
|
||||||
export type MarkConfig = {
|
|
||||||
[key: string]: any;
|
|
||||||
};
|
|
||||||
export type MarkAuth = {
|
|
||||||
[key: string]: any;
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* 隐秘内容
|
|
||||||
* auth
|
|
||||||
* config
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
export class MarkModel extends Model {
|
|
||||||
declare id: string;
|
|
||||||
declare title: string; // 标题,可以ai生成
|
|
||||||
declare description: string; // 描述,可以ai生成
|
|
||||||
declare cover: string; // 封面,可以ai生成
|
|
||||||
declare thumbnail: string; // 缩略图
|
|
||||||
|
|
||||||
declare markType: string; // markdown | json | html | image | video | audio | code | link | file
|
syncMarkModel({ sync: true, alter: true, logging: false });
|
||||||
declare link: string; // 访问链接
|
|
||||||
declare tags: string[]; // 标签
|
|
||||||
declare summary: string; // 摘要, description的简化版
|
|
||||||
declare data: MarkData; // 数据
|
|
||||||
|
|
||||||
declare uid: string; // 操作用户的id
|
|
||||||
declare puid: string; // 父级用户的id, 真实用户
|
|
||||||
declare config: MarkConfig; // mark属于一定不会暴露的内容。
|
|
||||||
|
|
||||||
declare fileList: MarkFile[]; // 文件管理
|
|
||||||
declare uname: string; // 用户的名称, 或者着别名
|
|
||||||
|
|
||||||
declare createdAt: Date;
|
|
||||||
declare updatedAt: Date;
|
|
||||||
declare version: number;
|
|
||||||
/**
|
|
||||||
* 加锁更新data中的node的节点,通过node的id
|
|
||||||
* @param param0
|
|
||||||
*/
|
|
||||||
static async updateJsonNode(id: string, node: MarkDataNode, opts?: { operate?: 'update' | 'delete'; Model?: any; sequelize?: Sequelize }) {
|
|
||||||
const sequelize = opts?.sequelize || (await useContextKey('sequelize'));
|
|
||||||
const transaction = await sequelize.transaction(); // 开启事务
|
|
||||||
const operate = opts.operate || 'update';
|
|
||||||
const isUpdate = operate === 'update';
|
|
||||||
const Model = opts.Model || MarkModel;
|
|
||||||
try {
|
|
||||||
// 1. 获取当前的 JSONB 字段值(加锁)
|
|
||||||
const mark = await Model.findByPk(id, {
|
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
|
||||||
});
|
|
||||||
if (!mark) {
|
|
||||||
throw new Error('Mark not found');
|
|
||||||
}
|
|
||||||
// 2. 修改特定的数组元素
|
|
||||||
const data = mark.data as MarkData;
|
|
||||||
const items = data.nodes;
|
|
||||||
if (!node.id) {
|
|
||||||
node.id = random(12);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 找到要更新的元素
|
|
||||||
const itemIndex = items.findIndex((item) => item.id === node.id);
|
|
||||||
if (itemIndex === -1) {
|
|
||||||
isUpdate && items.push(node);
|
|
||||||
} else {
|
|
||||||
if (isUpdate) {
|
|
||||||
items[itemIndex] = node;
|
|
||||||
} else {
|
|
||||||
items.splice(itemIndex, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const version = Number(mark.version) + 1;
|
|
||||||
// 4. 更新 JSONB 字段
|
|
||||||
const result = await mark.update(
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
...data,
|
|
||||||
nodes: items,
|
|
||||||
},
|
|
||||||
version,
|
|
||||||
},
|
|
||||||
{ transaction },
|
|
||||||
);
|
|
||||||
|
|
||||||
await transaction.commit();
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
await transaction.rollback();
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static async updateJsonNodes(id: string, nodes: { node: MarkDataNode; operate?: 'update' | 'delete' }[], opts?: { Model?: any; sequelize?: Sequelize }) {
|
|
||||||
const sequelize = opts?.sequelize || (await useContextKey('sequelize'));
|
|
||||||
const transaction = await sequelize.transaction(); // 开启事务
|
|
||||||
const Model = opts?.Model || MarkModel;
|
|
||||||
try {
|
|
||||||
const mark = await Model.findByPk(id, {
|
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
|
||||||
});
|
|
||||||
if (!mark) {
|
|
||||||
throw new Error('Mark not found');
|
|
||||||
}
|
|
||||||
const data = mark.data as MarkData;
|
|
||||||
const _nodes = data.nodes || [];
|
|
||||||
// 过滤不在nodes中的节点
|
|
||||||
const blankNodes = nodes.filter((node) => !_nodes.find((n) => n.id === node.node.id)).map((node) => node.node);
|
|
||||||
// 更新或删除节点
|
|
||||||
const newNodes = _nodes
|
|
||||||
.map((node) => {
|
|
||||||
const nodeOperate = nodes.find((n) => n.node.id === node.id);
|
|
||||||
if (nodeOperate) {
|
|
||||||
if (nodeOperate.operate === 'delete') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return nodeOperate.node;
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
})
|
|
||||||
.filter((node) => node !== null);
|
|
||||||
const version = Number(mark.version) + 1;
|
|
||||||
const result = await mark.update(
|
|
||||||
{
|
|
||||||
data: {
|
|
||||||
...data,
|
|
||||||
nodes: [...blankNodes, ...newNodes],
|
|
||||||
},
|
|
||||||
version,
|
|
||||||
},
|
|
||||||
{ transaction },
|
|
||||||
);
|
|
||||||
await transaction.commit();
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
await transaction.rollback();
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static async updateData(id: string, data: MarkData, opts: { Model?: any; sequelize?: Sequelize }) {
|
|
||||||
const sequelize = opts.sequelize || (await useContextKey('sequelize'));
|
|
||||||
const transaction = await sequelize.transaction(); // 开启事务
|
|
||||||
const Model = opts.Model || MarkModel;
|
|
||||||
const mark = await Model.findByPk(id, {
|
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE, // 加锁,防止其他事务同时修改
|
|
||||||
});
|
|
||||||
if (!mark) {
|
|
||||||
throw new Error('Mark not found');
|
|
||||||
}
|
|
||||||
const version = Number(mark.version) + 1;
|
|
||||||
const result = await mark.update(
|
|
||||||
{
|
|
||||||
...mark.data,
|
|
||||||
...data,
|
|
||||||
data: {
|
|
||||||
...mark.data,
|
|
||||||
...data,
|
|
||||||
},
|
|
||||||
version,
|
|
||||||
},
|
|
||||||
{ transaction },
|
|
||||||
);
|
|
||||||
await transaction.commit();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
static async createNew(data: any, opts: { Model?: any; sequelize?: Sequelize }) {
|
|
||||||
const sequelize = opts.sequelize || (await useContextKey('sequelize'));
|
|
||||||
const transaction = await sequelize.transaction(); // 开启事务
|
|
||||||
const Model = opts.Model || MarkModel;
|
|
||||||
const result = await Model.create({ ...data, version: 1 }, { transaction });
|
|
||||||
await transaction.commit();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export type MarkInitOpts<T = any> = {
|
|
||||||
tableName: string;
|
|
||||||
sequelize?: Sequelize;
|
|
||||||
callInit?: (attribute: ModelAttributes) => ModelAttributes;
|
|
||||||
Model?: T;
|
|
||||||
};
|
|
||||||
export type Opts = {
|
|
||||||
sync?: boolean;
|
|
||||||
alter?: boolean;
|
|
||||||
logging?: boolean;
|
|
||||||
force?: boolean;
|
|
||||||
};
|
|
||||||
export const MarkMInit = async <T = any>(opts: MarkInitOpts<T>, sync?: Opts) => {
|
|
||||||
const sequelize = await useContextKey('sequelize');
|
|
||||||
opts.sequelize = opts.sequelize || sequelize;
|
|
||||||
const { callInit, Model, ...optsRest } = opts;
|
|
||||||
const modelAttribute = {
|
|
||||||
id: {
|
|
||||||
type: DataTypes.UUID,
|
|
||||||
primaryKey: true,
|
|
||||||
defaultValue: DataTypes.UUIDV4,
|
|
||||||
comment: 'id',
|
|
||||||
},
|
|
||||||
title: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
},
|
|
||||||
markType: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: 'md', // markdown | json | html | image | video | audio | code | link | file
|
|
||||||
comment: '类型',
|
|
||||||
},
|
|
||||||
description: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
},
|
|
||||||
cover: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
comment: '封面',
|
|
||||||
},
|
|
||||||
thumbnail: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
comment: '缩略图',
|
|
||||||
},
|
|
||||||
link: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
comment: '链接',
|
|
||||||
},
|
|
||||||
tags: {
|
|
||||||
type: DataTypes.JSONB,
|
|
||||||
defaultValue: [],
|
|
||||||
},
|
|
||||||
summary: {
|
|
||||||
type: DataTypes.TEXT,
|
|
||||||
defaultValue: '',
|
|
||||||
comment: '摘要',
|
|
||||||
},
|
|
||||||
config: {
|
|
||||||
type: DataTypes.JSONB,
|
|
||||||
defaultValue: {},
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
type: DataTypes.JSONB,
|
|
||||||
defaultValue: {},
|
|
||||||
},
|
|
||||||
fileList: {
|
|
||||||
type: DataTypes.JSONB,
|
|
||||||
defaultValue: [],
|
|
||||||
},
|
|
||||||
uname: {
|
|
||||||
type: DataTypes.STRING,
|
|
||||||
defaultValue: '',
|
|
||||||
comment: '用户的名称, 更新后的用户的名称',
|
|
||||||
},
|
|
||||||
version: {
|
|
||||||
type: DataTypes.INTEGER, // 更新刷新版本,多人协作
|
|
||||||
defaultValue: 1,
|
|
||||||
},
|
|
||||||
uid: {
|
|
||||||
type: DataTypes.UUID,
|
|
||||||
allowNull: true,
|
|
||||||
},
|
|
||||||
puid: {
|
|
||||||
type: DataTypes.UUID,
|
|
||||||
allowNull: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const InitModel = Model || MarkModel;
|
|
||||||
// @ts-ignore
|
|
||||||
InitModel.init(callInit ? callInit(modelAttribute) : modelAttribute, {
|
|
||||||
sequelize,
|
|
||||||
paranoid: true,
|
|
||||||
...optsRest,
|
|
||||||
});
|
|
||||||
console.log('MarkModel init', optsRest);
|
|
||||||
if (sync && sync.sync) {
|
|
||||||
const { sync: _, ...rest } = sync;
|
|
||||||
console.log('MarkModel sync', rest);
|
|
||||||
MarkModel.sync({ alter: true, logging: false, ...rest }).catch((e) => {
|
|
||||||
console.error('MarkModel sync', e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const markModelInit = MarkMInit;
|
|
||||||
|
|
||||||
export const syncMarkModel = async (sync?: Opts) => {
|
|
||||||
const sequelize = await useContextKey('sequelize');
|
|
||||||
await MarkMInit({ sequelize, tableName: 'micro_mark' }, sync);
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -18,17 +18,33 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const data = ctx.query?.data;
|
const data = ctx.query?.data;
|
||||||
const { id, key, force, install } = data;
|
const { id, key, force, install, appKey: postAppKey, version: postVersion = '1.0.0' } = data;
|
||||||
if (!id) {
|
if (!id && !postAppKey) {
|
||||||
ctx.throw(400, 'Invalid id');
|
ctx.throw(400, 'Invalid id or postAppKey');
|
||||||
}
|
}
|
||||||
let username = tokenUser.username;
|
let username = tokenUser.username;
|
||||||
if (data.username) {
|
if (data.username && username === 'admin') {
|
||||||
// username = data.username;
|
username = data.username;
|
||||||
}
|
}
|
||||||
const microApp = await AppListModel.findByPk(id);
|
let microApp: AppListModel;
|
||||||
|
if (!microApp && id) {
|
||||||
|
microApp = await AppListModel.findByPk(id);
|
||||||
|
}
|
||||||
|
if (!microApp && postAppKey) {
|
||||||
|
microApp = await AppListModel.findOne({
|
||||||
|
where: {
|
||||||
|
key: postAppKey,
|
||||||
|
version: postVersion,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!microApp) {
|
if (!microApp) {
|
||||||
ctx.throw(400, 'Invalid id');
|
if (id) {
|
||||||
|
ctx.throw(400, 'Invalid id');
|
||||||
|
} else {
|
||||||
|
ctx.throw(400, `Invalid appKey , no found app with key: ${postAppKey} and version: ${postVersion}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const { key: appKey, version } = microApp;
|
const { key: appKey, version } = microApp;
|
||||||
const check = await appPathCheck({ key: key });
|
const check = await appPathCheck({ key: key });
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
// import { app } from '@/app.ts';
|
// import { app } from '@/app.ts';
|
||||||
import { manager, loadManager, app as ManagerApp } from '@kevisual/local-app-manager';
|
import { manager, loadManager, app as ManagerApp } from '@kevisual/local-app-manager';
|
||||||
|
import { fileIsExist } from '@kevisual/use-config/env';
|
||||||
|
import path from 'path';
|
||||||
|
const assistantAppsConfig = path.join(process.cwd(), 'assistant-apps-config.json');
|
||||||
|
const isExist = fileIsExist(assistantAppsConfig);
|
||||||
export const existDenpend = [
|
export const existDenpend = [
|
||||||
'sequelize', // commonjs
|
'sequelize', // commonjs
|
||||||
'pg', // commonjs
|
'pg', // commonjs
|
||||||
@@ -16,7 +20,11 @@ export { manager };
|
|||||||
// console.log('app', app, );
|
// console.log('app', app, );
|
||||||
// console.log('app2 context', global.context);
|
// console.log('app2 context', global.context);
|
||||||
// console.log('app equal', app === ManagerApp);
|
// console.log('app equal', app === ManagerApp);
|
||||||
loadManager();
|
if (isExist) {
|
||||||
|
loadManager({ runtime: 'server', configFilename: 'assistant-apps-config.json' });
|
||||||
|
} else {
|
||||||
|
loadManager({ runtime: 'server' });
|
||||||
|
}
|
||||||
|
|
||||||
// middleware: ['auth-admin']
|
// middleware: ['auth-admin']
|
||||||
/*
|
/*
|
||||||
@@ -31,4 +39,4 @@ loadManager();
|
|||||||
|
|
||||||
path: 'local-apps',
|
path: 'local-apps',
|
||||||
key: 'delete',
|
key: 'delete',
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const installAppFromKey = async (key: string) => {
|
|||||||
}
|
}
|
||||||
let showAppInfo = {
|
let showAppInfo = {
|
||||||
key,
|
key,
|
||||||
status: 'inactive',
|
status: 'inactive' as const,
|
||||||
type: app?.type || 'system-app',
|
type: app?.type || 'system-app',
|
||||||
description: readmeDesc || '',
|
description: readmeDesc || '',
|
||||||
version,
|
version,
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { PageModel } from '../models/index.ts';
|
|||||||
import { ContainerModel } from '@/routes/container/models/index.ts';
|
import { ContainerModel } from '@/routes/container/models/index.ts';
|
||||||
import { Op } from 'sequelize';
|
import { Op } from 'sequelize';
|
||||||
import { getContainerData } from './get-container.ts';
|
import { getContainerData } from './get-container.ts';
|
||||||
import path from 'path';
|
import path from 'node:path';
|
||||||
import fs from 'fs';
|
import fs from 'node:fs';
|
||||||
import { getHTML, getDataJs, getOneHTML } from './file-template.ts';
|
import { getHTML, getDataJs, getOneHTML } from './file-template.ts';
|
||||||
import { minioClient } from '@/app.ts';
|
import { minioClient } from '@/app.ts';
|
||||||
import { bucketName } from '@/modules/minio.ts';
|
import { bucketName } from '@/modules/minio.ts';
|
||||||
@@ -174,7 +174,7 @@ export const getZip = async (page: PageModel, opts: { tokenUser: any }) => {
|
|||||||
|
|
||||||
// 添加 JavaScript 字符串作为文件到 zip 中
|
// 添加 JavaScript 字符串作为文件到 zip 中
|
||||||
zip.append(dataJs, { name: 'data.js' });
|
zip.append(dataJs, { name: 'data.js' });
|
||||||
zip.append(JSON.stringify(page), { name: 'app.config.json5' });
|
zip.append(JSON.stringify(page), { name: 'app.config.json' });
|
||||||
// 可以继续添加更多内容,文件或目录等
|
// 可以继续添加更多内容,文件或目录等
|
||||||
// zip.append('Another content', { name: 'other.txt' });
|
// zip.append('Another content', { name: 'other.txt' });
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { ContainerModel } from '../container/models/index.ts';
|
|||||||
import { Op } from 'sequelize';
|
import { Op } from 'sequelize';
|
||||||
import { AppListModel, AppModel } from '../app-manager/index.ts';
|
import { AppListModel, AppModel } from '../app-manager/index.ts';
|
||||||
import { cachePage, getZip } from './module/cache-file.ts';
|
import { cachePage, getZip } from './module/cache-file.ts';
|
||||||
import _ from 'lodash';
|
import { uniqBy } from 'lodash-es';
|
||||||
import semver from 'semver';
|
import semver from 'semver';
|
||||||
|
|
||||||
app
|
app
|
||||||
@@ -53,7 +53,7 @@ app
|
|||||||
// 上传文件
|
// 上传文件
|
||||||
const res = await cachePage(page, { tokenUser, key, version: _version });
|
const res = await cachePage(page, { tokenUser, key, version: _version });
|
||||||
const appFiles = appList?.data?.files || [];
|
const appFiles = appList?.data?.files || [];
|
||||||
const newFiles = _.uniqBy([...appFiles, ...res], 'name');
|
const newFiles = uniqBy([...appFiles, ...res], 'name');
|
||||||
appList.data = {
|
appList.data = {
|
||||||
...appList?.data,
|
...appList?.data,
|
||||||
files: newFiles,
|
files: newFiles,
|
||||||
|
|||||||
159
src/routes/user/admin/user.ts
Normal file
159
src/routes/user/admin/user.ts
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
import { app } from '@/app.ts';
|
||||||
|
import { User } from '@/models/user.ts';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
import { CustomError } from '@kevisual/router';
|
||||||
|
import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts';
|
||||||
|
import { AppModel } from '@/routes/app-manager/index.ts';
|
||||||
|
// import { mvAppFromUserAToUserB } from '@/routes/app-manager/admin/mv-user-app.ts';
|
||||||
|
|
||||||
|
export const checkUsername = (username: string) => {
|
||||||
|
if (username.length > 30) {
|
||||||
|
throw new CustomError(400, 'Username cannot be too long');
|
||||||
|
}
|
||||||
|
if (!/^[a-zA-Z0-9_@]+$/.test(username)) {
|
||||||
|
throw new CustomError(400, 'Username cannot contain special characters');
|
||||||
|
}
|
||||||
|
if (username.includes(' ')) {
|
||||||
|
throw new CustomError(400, 'Username cannot contain spaces');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const checkUsernameShort = (username: string) => {
|
||||||
|
if (username.length < 3) {
|
||||||
|
throw new CustomError(400, 'Username cannot be too short');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'changeName',
|
||||||
|
middleware: ['auth-admin'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { id, newName } = ctx.query.data || {};
|
||||||
|
const user = await User.findByPk(id);
|
||||||
|
if (!user) {
|
||||||
|
ctx.throw(404, 'User not found');
|
||||||
|
}
|
||||||
|
const oldName = user.username;
|
||||||
|
checkUsername(newName);
|
||||||
|
const findUserByUsername = await User.findOne({ where: { username: newName } });
|
||||||
|
if (findUserByUsername) {
|
||||||
|
ctx.throw(400, 'Username already exists');
|
||||||
|
}
|
||||||
|
user.username = newName;
|
||||||
|
try {
|
||||||
|
await user.save();
|
||||||
|
// 迁移文件数据
|
||||||
|
await backupUserA(oldName, user.id); // 备份文件数据
|
||||||
|
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
|
||||||
|
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
|
||||||
|
|
||||||
|
if (['org', 'user'].includes(user.type)) {
|
||||||
|
const type = user.type === 'org' ? 'org' : 'user';
|
||||||
|
await User.clearUserToken(user.id, type); // 清除旧token
|
||||||
|
}
|
||||||
|
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据
|
||||||
|
} catch (error) {
|
||||||
|
console.error('迁移文件数据失败', error);
|
||||||
|
ctx.throw(500, 'Failed to change username');
|
||||||
|
}
|
||||||
|
ctx.body = user;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'checkUserExist',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { username } = ctx.query.data || {};
|
||||||
|
if (!username) {
|
||||||
|
ctx.throw(400, 'Username is required');
|
||||||
|
}
|
||||||
|
checkUsername(username);
|
||||||
|
const user = await User.findOne({ where: { username } });
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
id: user?.id,
|
||||||
|
username: user?.username,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'resetPassword',
|
||||||
|
middleware: ['auth-admin'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { id, password } = ctx.query.data || {};
|
||||||
|
const user = await User.findByPk(id);
|
||||||
|
if (!user) {
|
||||||
|
ctx.throw(404, 'User not found');
|
||||||
|
}
|
||||||
|
let pwd = password || nanoid(6);
|
||||||
|
user.createPassword(pwd);
|
||||||
|
await user.save();
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
password: !password ? pwd : undefined,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'createNewUser',
|
||||||
|
middleware: ['auth-admin'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { username, password, description } = ctx.query.data || {};
|
||||||
|
if (!username) {
|
||||||
|
ctx.throw(400, 'Username is required');
|
||||||
|
}
|
||||||
|
checkUsername(username);
|
||||||
|
const findUserByUsername = await User.findOne({ where: { username } });
|
||||||
|
if (findUserByUsername) {
|
||||||
|
ctx.throw(400, 'Username already exists');
|
||||||
|
}
|
||||||
|
let pwd = password || nanoid(6);
|
||||||
|
const user = await User.createUser(username, pwd, description);
|
||||||
|
ctx.body = {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
description: user.description,
|
||||||
|
password: pwd,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'deleteUser',
|
||||||
|
middleware: ['auth-admin'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { id } = ctx.query.data || {};
|
||||||
|
const user = await User.findByPk(id);
|
||||||
|
if (!user) {
|
||||||
|
ctx.throw(404, 'User not found');
|
||||||
|
}
|
||||||
|
await user.destroy();
|
||||||
|
backupUserA(user.username, user.id);
|
||||||
|
deleteUser(user.username);
|
||||||
|
// TODO: EXPIRE 删除token
|
||||||
|
ctx.body = {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
message: 'User deleted successfully',
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
@@ -3,10 +3,14 @@ import './org.ts';
|
|||||||
|
|
||||||
import './me.ts';
|
import './me.ts';
|
||||||
|
|
||||||
import './update.ts'
|
import './update.ts';
|
||||||
|
|
||||||
import './init.ts'
|
import './init.ts';
|
||||||
|
|
||||||
import './web-login.ts'
|
import './web-login.ts';
|
||||||
|
|
||||||
import './org-user/list.ts'
|
import './org-user/list.ts';
|
||||||
|
|
||||||
|
import './admin/user.ts';
|
||||||
|
|
||||||
|
import './secret-key/list.ts';
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { app } from '@/app.ts';
|
import { app } from '@/app.ts';
|
||||||
import { User } from '@/models/user.ts';
|
import { User } from '@/models/user.ts';
|
||||||
import { CustomError } from '@kevisual/router';
|
import { CustomError } from '@kevisual/router';
|
||||||
|
import { checkUsername } from './admin/user.ts';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
@@ -18,7 +20,6 @@ app
|
|||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'user',
|
path: 'user',
|
||||||
@@ -28,9 +29,12 @@ app
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { id, username, password, description } = ctx.query.data || {};
|
const { id, username, password, description } = ctx.query.data || {};
|
||||||
|
if (!id) {
|
||||||
|
throw new CustomError(400, 'id is required');
|
||||||
|
}
|
||||||
const user = await User.findByPk(id);
|
const user = await User.findByPk(id);
|
||||||
if (user.id !== tokenUser.id) {
|
if (user.id !== tokenUser.id) {
|
||||||
throw new CustomError(401, 'Permission denied');
|
throw new CustomError(403, 'Permission denied');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
@@ -59,21 +63,26 @@ app
|
|||||||
.route({
|
.route({
|
||||||
path: 'user',
|
path: 'user',
|
||||||
key: 'add',
|
key: 'add',
|
||||||
middleware: ['auth'],
|
middleware: ['auth-admin'],
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
|
||||||
const { username, password, description } = ctx.query.data || {};
|
const { username, password, description } = ctx.query.data || {};
|
||||||
if (!username) {
|
if (!username) {
|
||||||
throw new CustomError(400, 'username is required');
|
throw new CustomError(400, 'username is required');
|
||||||
}
|
}
|
||||||
const user = await User.createUser(username, password, description);
|
checkUsername(username);
|
||||||
const token = await user.createToken();
|
const findUserByUsername = await User.findOne({ where: { username } });
|
||||||
|
if (findUserByUsername) {
|
||||||
|
throw new CustomError(400, 'username already exists');
|
||||||
|
}
|
||||||
|
const pwd = password || nanoid(6);
|
||||||
|
const user = await User.createUser(username, pwd, description);
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
description: user.description,
|
description: user.description,
|
||||||
needChangePassword: user.needChangePassword,
|
needChangePassword: user.needChangePassword,
|
||||||
token,
|
password: pwd,
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export const createCookie = (token: any, ctx: any) => {
|
|||||||
ctx.res.cookie('token', token.accessToken || token?.token, {
|
ctx.res.cookie('token', token.accessToken || token?.token, {
|
||||||
maxAge: 7 * 24 * 60 * 60 * 1000, // 过期时间, 设置7天
|
maxAge: 7 * 24 * 60 * 60 * 1000, // 过期时间, 设置7天
|
||||||
domain,
|
domain,
|
||||||
|
path: '/',
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
});
|
});
|
||||||
@@ -79,12 +80,17 @@ export const clearCookie = (ctx: any) => {
|
|||||||
if (!domain) {
|
if (!domain) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ctx.res.cookie('token', '', {
|
const browser = ctx.req.headers['user-agent'];
|
||||||
maxAge: 0,
|
const isBrowser = browser.includes('Mozilla'); // 浏览器
|
||||||
domain,
|
if (isBrowser && ctx.res.cookie) {
|
||||||
sameSite: 'lax',
|
ctx.res.cookie('token', '_', {
|
||||||
httpOnly: true,
|
maxAge: 1,
|
||||||
});
|
domain,
|
||||||
|
path: '/',
|
||||||
|
sameSite: 'lax',
|
||||||
|
httpOnly: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
@@ -158,10 +164,17 @@ app
|
|||||||
key: 'logout',
|
key: 'logout',
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
|
const token = ctx.query?.token;
|
||||||
const { tokens = [] } = ctx.query?.data || {};
|
const { tokens = [] } = ctx.query?.data || {};
|
||||||
clearCookie(ctx);
|
clearCookie(ctx);
|
||||||
for (const token of tokens) {
|
let needDelTokens = Array.from(new Set([...tokens, token].filter(Boolean)));
|
||||||
await User.oauth.delToken(token);
|
for (const token of needDelTokens) {
|
||||||
|
try {
|
||||||
|
await User.oauth.delToken(token);
|
||||||
|
} catch (e) {
|
||||||
|
// console.log('logout error', e);
|
||||||
|
console.log('error token is has been deleted', token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
code: 200,
|
code: 200,
|
||||||
@@ -201,13 +214,13 @@ app
|
|||||||
const { id } = tokenUser;
|
const { id } = tokenUser;
|
||||||
const user = await User.findByPk(id);
|
const user = await User.findByPk(id);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
ctx.throw(500, 'user not found');
|
ctx.throw(404, 'user not found');
|
||||||
}
|
}
|
||||||
user.setTokenUser(tokenUser);
|
user.setTokenUser(tokenUser);
|
||||||
if (username) {
|
if (username) {
|
||||||
user.username = username;
|
user.username = username;
|
||||||
}
|
}
|
||||||
if (password) {
|
if (password && user.type !== 'org') {
|
||||||
user.createPassword(password);
|
user.createPassword(password);
|
||||||
}
|
}
|
||||||
if (description) {
|
if (description) {
|
||||||
|
|||||||
136
src/routes/user/secret-key/list.ts
Normal file
136
src/routes/user/secret-key/list.ts
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import { Op } from 'sequelize';
|
||||||
|
import { User, UserSecret } from '@/models/user.ts';
|
||||||
|
import { app } from '@/app.ts';
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'secret',
|
||||||
|
key: 'list',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { page = 1, pageSize = 20, search, sort = 'DESC', orgId } = ctx.query;
|
||||||
|
const searchWhere: Record<string, any> = search
|
||||||
|
? {
|
||||||
|
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { description: { [Op.like]: `%${search}%` } }],
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
if (orgId) {
|
||||||
|
searchWhere.orgId = orgId;
|
||||||
|
}
|
||||||
|
const { rows: secrets, count } = await UserSecret.findAndCountAll({
|
||||||
|
where: {
|
||||||
|
userId: tokenUser.userId,
|
||||||
|
...searchWhere,
|
||||||
|
},
|
||||||
|
offset: (page - 1) * pageSize,
|
||||||
|
limit: pageSize,
|
||||||
|
attributes: {
|
||||||
|
exclude: ['token'], // Exclude sensitive token field
|
||||||
|
},
|
||||||
|
order: [['updatedAt', sort]],
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
list: secrets,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
current: page,
|
||||||
|
pageSize,
|
||||||
|
total: count,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'secret',
|
||||||
|
key: 'update',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id, updatedAt: _clear, createdAt: _clear2, token, ...rest } = ctx.query.data;
|
||||||
|
let secret: UserSecret;
|
||||||
|
let isNew = false;
|
||||||
|
|
||||||
|
if (id) {
|
||||||
|
secret = await UserSecret.findByPk(id);
|
||||||
|
if (!secret) {
|
||||||
|
ctx.throw(404, 'Secret not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secret.userId !== tokenUser.userId) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
secret = await UserSecret.createSecret(tokenUser);
|
||||||
|
isNew = true;
|
||||||
|
}
|
||||||
|
if (secret) {
|
||||||
|
secret = await secret.update({
|
||||||
|
...rest,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = secret;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'secret',
|
||||||
|
key: 'delete',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id } = ctx.query.data || {};
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
ctx.throw(400, 'id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
const secret = await UserSecret.findByPk(id);
|
||||||
|
|
||||||
|
if (!secret) {
|
||||||
|
ctx.throw(404, 'Secret not found');
|
||||||
|
}
|
||||||
|
if (secret.userId !== tokenUser.userId) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
|
||||||
|
await secret.destroy();
|
||||||
|
ctx.body = secret;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'secret',
|
||||||
|
key: 'get',
|
||||||
|
middleware: ['auth'],
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state.tokenUser;
|
||||||
|
const { id } = ctx.query.data || {};
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
ctx.throw(400, 'id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
const secret = await UserSecret.findByPk(id);
|
||||||
|
|
||||||
|
if (!secret) {
|
||||||
|
ctx.throw(404, 'Secret not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secret.userId !== tokenUser.uid) {
|
||||||
|
ctx.throw(403, 'No permission');
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = secret;
|
||||||
|
})
|
||||||
|
.addTo(app);
|
||||||
@@ -1,56 +1,57 @@
|
|||||||
import { app } from '@/app.ts'
|
import { app } from '@/app.ts';
|
||||||
import { User } from '@/models/user.ts'
|
import { User } from '@/models/user.ts';
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'user',
|
path: 'user',
|
||||||
key: 'getUpdateInfo',
|
key: 'getUpdateInfo',
|
||||||
middleware: ['auth']
|
middleware: ['auth'],
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state?.tokenUser || {}
|
const tokenUser = ctx.state?.tokenUser || {};
|
||||||
const user = await User.findByPk(tokenUser.id)
|
const user = await User.findByPk(tokenUser.id);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
ctx.throw(500, 'user not found')
|
ctx.throw(500, 'user not found');
|
||||||
}
|
}
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
nickname: user.nickname,
|
nickname: user.nickname,
|
||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
data: user.data
|
data: user.data,
|
||||||
}
|
};
|
||||||
})
|
})
|
||||||
.addTo(app)
|
.addTo(app);
|
||||||
app
|
app
|
||||||
.route('user', 'updateInfo', {
|
.route('user', 'updateInfo', {
|
||||||
middleware: ['auth']
|
middleware: ['auth'],
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const { nickname, avatar, data } = ctx.query.data || {}
|
const { nickname, avatar, data } = ctx.query.data || {};
|
||||||
const tokenUser = ctx.state?.tokenUser || {}
|
const tokenUser = ctx.state?.tokenUser || {};
|
||||||
const { id } = tokenUser
|
const { id, uid } = tokenUser;
|
||||||
const user = await User.findByPk(id)
|
const user = await User.findByPk(id);
|
||||||
let updateData: any = {}
|
let updateData: any = {};
|
||||||
if (!user) {
|
if (!user) {
|
||||||
ctx.throw(500, 'user not found')
|
ctx.throw(500, 'user not found');
|
||||||
}
|
}
|
||||||
if (nickname) {
|
if (nickname) {
|
||||||
updateData.nickname = nickname
|
updateData.nickname = nickname;
|
||||||
}
|
}
|
||||||
if (avatar) {
|
if (avatar) {
|
||||||
updateData.avatar = avatar
|
updateData.avatar = avatar;
|
||||||
}
|
}
|
||||||
await user.update(
|
await user.update(
|
||||||
{
|
{
|
||||||
...updateData,
|
...updateData,
|
||||||
data: {
|
data: {
|
||||||
...user.data,
|
...user.data,
|
||||||
...data
|
...data,
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fields: ['nickname', 'avatar', 'data']
|
fields: ['nickname', 'avatar', 'data'],
|
||||||
}
|
},
|
||||||
)
|
);
|
||||||
ctx.body = await user.getInfo()
|
user.setTokenUser(tokenUser);
|
||||||
|
ctx.body = await user.getInfo();
|
||||||
})
|
})
|
||||||
.addTo(app)
|
.addTo(app);
|
||||||
|
|||||||
9
src/run.ts
Normal file
9
src/run.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { program } from './program.ts';
|
||||||
|
|
||||||
|
//
|
||||||
|
import './scripts/change-user-pwd.ts';
|
||||||
|
import './scripts/list-app.ts';
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
program.parse(process.argv);
|
||||||
50
src/scripts/change-user-pwd.ts
Normal file
50
src/scripts/change-user-pwd.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { program, Command } from '../program.ts';
|
||||||
|
import { initUser, logger, close } from './common.ts';
|
||||||
|
const usrCommand = new Command('user').description('用户相关操作');
|
||||||
|
program.addCommand(usrCommand);
|
||||||
|
|
||||||
|
const changePwd = new Command('pwd')
|
||||||
|
.description('修改用户密码')
|
||||||
|
.option('-u, --username <username>', '用户名')
|
||||||
|
.option('-p, --password <password>', '新密码')
|
||||||
|
.action(async (opts) => {
|
||||||
|
const username = opts.username;
|
||||||
|
const password = opts.password;
|
||||||
|
if (!username) {
|
||||||
|
logger.error('用户名不能为空');
|
||||||
|
close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { User } = await initUser();
|
||||||
|
const newPassword = password || 'kevisual';
|
||||||
|
|
||||||
|
logger.info(`用户名: ${username}`);
|
||||||
|
logger.info(`新密码: ${newPassword}`);
|
||||||
|
const user = await User.findOne({ where: { username: username }, logging: false });
|
||||||
|
if (!user) {
|
||||||
|
logger.error('用户不存在');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newP = await user.createPassword(newPassword);
|
||||||
|
logger.info('新密码加密成功', '新密码: ', newPassword);
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
usrCommand.addCommand(changePwd);
|
||||||
|
|
||||||
|
const list = new Command('list').description('列出所有用户').action(async () => {
|
||||||
|
console.log('列出所有用户 start');
|
||||||
|
const { User } = await initUser();
|
||||||
|
console.log('列出所有用户');
|
||||||
|
const users = await User.findAll({ limit: 10, order: [['createdAt', 'DESC']] });
|
||||||
|
if (users.length === 0) {
|
||||||
|
logger.info('没有用户');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
users.forEach((user) => {
|
||||||
|
console.log(`用户名: ${user.username}`);
|
||||||
|
});
|
||||||
|
console.log(`用户数量: ${users.length}`);
|
||||||
|
await close();
|
||||||
|
});
|
||||||
|
|
||||||
|
usrCommand.addCommand(list);
|
||||||
3
src/scripts/common-redis.ts
Normal file
3
src/scripts/common-redis.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import * as redisLib from '../modules/redis.ts';
|
||||||
|
import { useContextKey, useContext } from '@kevisual/context';
|
||||||
|
export const redis = useContextKey('redis', () => redisLib.redis);
|
||||||
35
src/scripts/common.ts
Normal file
35
src/scripts/common.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import { config } from '../modules/config.ts';
|
||||||
|
import { sequelize } from '../modules/sequelize.ts';
|
||||||
|
export { program, Command } from '../program.ts';
|
||||||
|
// import { User, UserInit, OrgInit, Org, UserSecretInit, UserSecret } from '@kevisual/code-center-module/models';
|
||||||
|
import { User, UserInit, OrgInit, Org, UserSecretInit, UserSecret } from '@kevisual/code-center-module/src/core-models.ts';
|
||||||
|
import { Logger } from '@kevisual/logger';
|
||||||
|
export const close = async () => {
|
||||||
|
process.exit(0);
|
||||||
|
};
|
||||||
|
export { sequelize };
|
||||||
|
export const logger = new Logger({
|
||||||
|
level: (config?.LOG_LEVEL || 'info') as any,
|
||||||
|
showTime: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const initUser = async () => {
|
||||||
|
console.log('init user');
|
||||||
|
await UserInit(sequelize, undefined, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
await OrgInit(sequelize, undefined, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
await UserSecretInit(sequelize, undefined, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
User: User,
|
||||||
|
Org: Org,
|
||||||
|
UserSecret: UserSecret,
|
||||||
|
};
|
||||||
|
};
|
||||||
12
src/scripts/list-app.ts
Normal file
12
src/scripts/list-app.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { AppListModel, AppModel } from '../routes/app-manager/module/index.ts';
|
||||||
|
|
||||||
|
import { program, Command, close } from './common.ts';
|
||||||
|
|
||||||
|
const app = program.command('app');
|
||||||
|
|
||||||
|
const appList = new Command('list').action(async () => {
|
||||||
|
const list = await AppListModel.findAll();
|
||||||
|
console.log(list.map((item) => item.toJSON()));
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
app.addCommand(appList);
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import { sequelize } from '../modules/sequelize.ts';
|
|
||||||
import { User, UserInit } from '../models/user.ts';
|
|
||||||
|
|
||||||
// User.sync({ alter: true, logging: true }).then(() => {
|
|
||||||
// console.log('sync user done');
|
|
||||||
// });
|
|
||||||
|
|
||||||
// class UserChange extends User {
|
|
||||||
// static async syncUser() {
|
|
||||||
// await UserChange.sync({ alter: true, logging: false });
|
|
||||||
// console.log('sync user done');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
export const main = async () => {
|
|
||||||
|
|
||||||
await UserInit(null, null, {
|
|
||||||
alter: true,
|
|
||||||
logging: false,
|
|
||||||
});
|
|
||||||
const user = await User.findAll({});
|
|
||||||
for (const u of user) {
|
|
||||||
console.log(u.username, u.type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
main();
|
|
||||||
62
src/test/create-user-secret.ts
Normal file
62
src/test/create-user-secret.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { sequelize } from '../modules/sequelize.ts';
|
||||||
|
import { initUser } from '../scripts/common.ts';
|
||||||
|
import '../scripts/common-redis.ts';
|
||||||
|
import { useContextKey } from '@kevisual/context';
|
||||||
|
|
||||||
|
export const main = async () => {
|
||||||
|
const models = await initUser();
|
||||||
|
|
||||||
|
const username = 'root';
|
||||||
|
const orgname = 'admin';
|
||||||
|
|
||||||
|
const user = await models.User.findOne({ where: { username } });
|
||||||
|
const org = await models.User.findOne({ where: { username: orgname } });
|
||||||
|
|
||||||
|
console.log('user.id', user?.id);
|
||||||
|
console.log('org.id', org?.id);
|
||||||
|
// const userSecret1 = await models.UserSecret.createSecret(user?.id!);
|
||||||
|
// userSecret1.title = 'root secret';
|
||||||
|
// await userSecret1.save();
|
||||||
|
// await models.UserSecret.destroy({
|
||||||
|
// where: {
|
||||||
|
// orgId: '16a496d4-8cd6-4e02-b403-c2adc006a53d',
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
const userSecret2 = await models.UserSecret.createSecret(user?.id!, org?.id!);
|
||||||
|
userSecret2.title = 'root org secret';
|
||||||
|
await userSecret2.save();
|
||||||
|
|
||||||
|
const secretList = await models.UserSecret.findAll();
|
||||||
|
for (const secret of secretList) {
|
||||||
|
console.log(`\nSecret ID: ${secret.id}, User ID: ${secret.userId}, Org ID: ${secret.orgId}, Token: ${secret.token}, Expired Time: ${secret.expiredTime}`);
|
||||||
|
}
|
||||||
|
process.exit(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
||||||
|
export const dropTable = async () => {
|
||||||
|
await sequelize.query('DROP TABLE IF EXISTS "cf_user_secrets"');
|
||||||
|
console.log('UserSecret table dropped');
|
||||||
|
process.exit(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
// dropTable()
|
||||||
|
|
||||||
|
const token1 = 'sk_tvwzgp5lky8iupawh0encvd52vji4o8argvd2x668gn15q83xpgo8fe10ny7wfsq';
|
||||||
|
const orgToken2 = 'sk_x37p8iifh6k18c3f121w49nmfy1sbjqpyol9fcsz0lmc5dz493wrfwvtxc4gi9od';
|
||||||
|
|
||||||
|
export const main2 = async () => {
|
||||||
|
const redis = useContextKey('redis');
|
||||||
|
if (!redis) {
|
||||||
|
console.error('Redis is not initialized');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const models = await initUser();
|
||||||
|
const UserSecret = models.UserSecret;
|
||||||
|
const v = await models.UserSecret.verifyToken(token1);
|
||||||
|
console.log('verifyToken', v);
|
||||||
|
process.exit(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
// main2();
|
||||||
15
src/test/mv-minio.ts
Normal file
15
src/test/mv-minio.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
process.env.NODE_ENV = 'development';
|
||||||
|
// import { mvUserAToUserB, backupUserA } from '../routes/file/module/get-minio-list.ts';
|
||||||
|
|
||||||
|
|
||||||
|
// mvUserAToUserB('demo', 'demo2');
|
||||||
|
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-00');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-01');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-02');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-03');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-04');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-05');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-06');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-07');
|
||||||
|
// backupUserA('demo', '123', '2025-04-02-16-08');
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useContextKey } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/context';
|
||||||
import { sequelize } from '../modules/sequelize.ts';
|
import { sequelize } from '../modules/sequelize.ts';
|
||||||
import { MarkModel, syncMarkModel } from '../routes/mark/model.ts';
|
import { MarkModel, syncMarkModel } from '../routes/mark/model.ts';
|
||||||
export const sequelize2 = useContextKey('sequelize', () => sequelize);
|
export const sequelize2 = useContextKey('sequelize', () => sequelize);
|
||||||
48
src/test/sync-user.ts
Normal file
48
src/test/sync-user.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import { sequelize } from '../modules/sequelize.ts';
|
||||||
|
import { User, UserInit, UserServices, Org, OrgInit } from '@kevisual/code-center-module/models';
|
||||||
|
|
||||||
|
// User.sync({ alter: true, logging: true }).then(() => {
|
||||||
|
// console.log('sync user done');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// class UserChange extends User {
|
||||||
|
// static async syncUser() {
|
||||||
|
// await UserChange.sync({ alter: true, logging: false });
|
||||||
|
// console.log('sync user done');
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
export const main = async () => {
|
||||||
|
await UserInit(sequelize, null, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
await OrgInit(sequelize, null, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
const user = await User.findAll({});
|
||||||
|
for (const u of user) {
|
||||||
|
console.log(u.username, u.type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
|
export const changeRootPassword = async () => {
|
||||||
|
await OrgInit(sequelize, null, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
await UserInit(sequelize, null, {
|
||||||
|
alter: true,
|
||||||
|
logging: false,
|
||||||
|
});
|
||||||
|
const user = await User.findOne({ where: { username: 'root' } });
|
||||||
|
if (user) {
|
||||||
|
await user.createPassword('');
|
||||||
|
await user.save();
|
||||||
|
console.log('change root password done');
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// changeRootPassword();
|
||||||
10
src/test/test-sql.ts
Normal file
10
src/test/test-sql.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { sequelize } from '../modules/sequelize.ts';
|
||||||
|
|
||||||
|
console.log('sequelize');
|
||||||
|
|
||||||
|
// 获取所有表名
|
||||||
|
const [tables] = await sequelize.query(
|
||||||
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('tables', tables);
|
||||||
@@ -5,6 +5,7 @@ export const getContentType = (filePath: string) => {
|
|||||||
const contentType = {
|
const contentType = {
|
||||||
'.html': 'text/html; charset=utf-8',
|
'.html': 'text/html; charset=utf-8',
|
||||||
'.js': 'text/javascript; charset=utf-8',
|
'.js': 'text/javascript; charset=utf-8',
|
||||||
|
'.mjs': 'text/javascript; charset=utf-8',
|
||||||
'.css': 'text/css; charset=utf-8',
|
'.css': 'text/css; charset=utf-8',
|
||||||
'.txt': 'text/plain; charset=utf-8',
|
'.txt': 'text/plain; charset=utf-8',
|
||||||
'.json': 'application/json; charset=utf-8',
|
'.json': 'application/json; charset=utf-8',
|
||||||
|
|||||||
Submodule submodules/oss deleted from 68332c9c8d
Submodule submodules/pay-center-code deleted from 0db28aecde
Submodule submodules/permission deleted from f8e2f74d5e
@@ -1,40 +1,17 @@
|
|||||||
{
|
{
|
||||||
|
"extends": "@kevisual/types/json/backend.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "nodenext",
|
"baseUrl": ".",
|
||||||
"target": "esnext",
|
|
||||||
"noImplicitAny": false,
|
|
||||||
"outDir": "./dist",
|
|
||||||
"sourceMap": false,
|
|
||||||
"allowJs": true,
|
|
||||||
"newLine": "LF",
|
|
||||||
"baseUrl": "./",
|
|
||||||
"typeRoots": [
|
"typeRoots": [
|
||||||
"node_modules/@types",
|
"./node_modules/@types"
|
||||||
"//node_modules/@kevisual/types"
|
|
||||||
],
|
],
|
||||||
"declaration": true,
|
|
||||||
"noEmit": false,
|
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"emitDeclarationOnly": true,
|
|
||||||
"moduleResolution": "NodeNext",
|
|
||||||
"experimentalDecorators": true,
|
|
||||||
"emitDecoratorMetadata": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": [
|
"@/*": [
|
||||||
"src/*"
|
"src/*"
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"typings.d.ts",
|
"src/**/*",
|
||||||
"src/**/*.ts",
|
|
||||||
"test/**/*.ts",
|
|
||||||
"src-apps/**/*.ts",
|
|
||||||
],
|
|
||||||
"exclude": [
|
|
||||||
"node_modules",
|
|
||||||
"dist",
|
|
||||||
"src/**/*.test.ts"
|
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
17
turbo.json
17
turbo.json
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://turbo.build/schema.json",
|
|
||||||
"tasks": {
|
|
||||||
"build": {
|
|
||||||
"dependsOn": [
|
|
||||||
"^build"
|
|
||||||
],
|
|
||||||
"outputs": [
|
|
||||||
"dist/**"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"dev:lib": {
|
|
||||||
"persistent": true,
|
|
||||||
"cache": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user