From e83058c3bc5073d08c3da6a67af4263b2d945ca5 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 8 Feb 2024 20:47:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E8=84=9A=E6=9C=AC=E3=80=81?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E3=80=81=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/config.ts | 33 +++++++++++++++------------------ back/api/log.ts | 21 ++++++++++++++++++++- back/api/script.ts | 22 ++++++++++++++++++---- back/services/config.ts | 30 ++++++++++++++++++++++++++++++ back/services/script.ts | 10 ++++++++-- 5 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 back/services/config.ts diff --git a/back/api/config.ts b/back/api/config.ts index f2e6e69c..a3afb5f2 100644 --- a/back/api/config.ts +++ b/back/api/config.ts @@ -7,7 +7,7 @@ import * as fs from 'fs/promises'; import { celebrate, Joi } from 'celebrate'; import { join } from 'path'; import { SAMPLE_FILES } from '../config/const'; -import got from 'got'; +import ConfigService from '../services/config'; const route = Router(); export default (app: Router) => { @@ -50,24 +50,9 @@ export default (app: Router) => { route.get( '/detail', async (req: Request, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { - let content = ''; - const _path = req.query.path as string; - if (config.blackFileList.includes(_path) || !_path) { - res.send({ code: 403, message: '文件无法访问' }); - } - if (_path.startsWith('sample/')) { - const res = await got.get( - `https://gitlab.com/whyour/qinglong/-/raw/master/${_path}`, - ); - content = res.body; - } else if (_path.startsWith('data/scripts/')) { - content = await getFileContentByName(join(config.rootPath, _path)); - } else { - content = await getFileContentByName(join(config.configPath, _path)); - } - res.send({ code: 200, data: content }); + const configService = Container.get(ConfigService); + await configService.getFile(req.query.path as string, res); } catch (e) { return next(e); } @@ -100,4 +85,16 @@ export default (app: Router) => { } }, ); + + route.get( + '/:file', + async (req: Request, res: Response, next: NextFunction) => { + try { + const configService = Container.get(ConfigService); + await configService.getFile(req.params.file, res); + } catch (e) { + return next(e); + } + }, + ); }; diff --git a/back/api/log.ts b/back/api/log.ts index edb6cf63..f7fcabf7 100644 --- a/back/api/log.ts +++ b/back/api/log.ts @@ -29,7 +29,6 @@ export default (app: Router) => { route.get( '/detail', async (req: Request, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { if (blacklist.includes(req.path)) { return res.send({ code: 403, message: '暂无权限' }); @@ -47,6 +46,26 @@ export default (app: Router) => { }, ); + route.get( + '/:file', + async (req: Request, res: Response, next: NextFunction) => { + try { + if (blacklist.includes(req.path)) { + return res.send({ code: 403, message: '暂无权限' }); + } + const filePath = join( + config.logPath, + (req.query.path || '') as string, + req.params.file, + ); + const content = await getFileContentByName(filePath); + res.send({ code: 200, data: content }); + } catch (e) { + return next(e); + } + }, + ); + route.delete( '/', celebrate({ diff --git a/back/api/script.ts b/back/api/script.ts index 98639e52..d0fc19b2 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -75,14 +75,28 @@ export default (app: Router) => { route.get( '/detail', async (req: Request, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { - const filePath = join( - config.scriptPath, + const scriptService = Container.get(ScriptService); + const content = await scriptService.getFile( req.query.path as string, req.query.file as string, ); - const content = await getFileContentByName(filePath); + res.send({ code: 200, data: content }); + } catch (e) { + return next(e); + } + }, + ); + + route.get( + '/:file', + async (req: Request, res: Response, next: NextFunction) => { + try { + const scriptService = Container.get(ScriptService); + const content = await scriptService.getFile( + req.query.path as string, + req.params.file, + ); res.send({ code: 200, data: content }); } catch (e) { return next(e); diff --git a/back/services/config.ts b/back/services/config.ts new file mode 100644 index 00000000..2b5a2703 --- /dev/null +++ b/back/services/config.ts @@ -0,0 +1,30 @@ +import { Service, Inject } from 'typedi'; +import path, { join } from 'path'; +import config from '../config'; +import { getFileContentByName } from '../config/util'; +import { Response } from 'express'; +import got from 'got'; + +@Service() +export default class ConfigService { + constructor() {} + + public async getFile(filePath: string, res: Response) { + let content = ''; + if (config.blackFileList.includes(filePath) || !filePath) { + res.send({ code: 403, message: '文件无法访问' }); + } + if (filePath.startsWith('sample/')) { + const res = await got.get( + `https://gitlab.com/whyour/qinglong/-/raw/master/${filePath}`, + ); + content = res.body; + } else if (filePath.startsWith('data/scripts/')) { + content = await getFileContentByName(join(config.rootPath, filePath)); + } else { + content = await getFileContentByName(join(config.configPath, filePath)); + } + + res.send({ code: 200, data: content }); + } +} diff --git a/back/services/script.ts b/back/services/script.ts index 10b19e72..3796213e 100644 --- a/back/services/script.ts +++ b/back/services/script.ts @@ -1,12 +1,12 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; -import path from 'path'; +import path, { join } from 'path'; import SockService from './sock'; import CronService from './cron'; import ScheduleService, { TaskCallbacks } from './schedule'; import config from '../config'; import { TASK_COMMAND } from '../config/const'; -import { getPid, killTask, rmPath } from '../config/util'; +import { getFileContentByName, getPid, killTask, rmPath } from '../config/util'; @Service() export default class ScriptService { @@ -61,4 +61,10 @@ export default class ScriptService { return { code: 200 }; } + + public async getFile(filePath: string, fileName: string) { + const _filePath = join(config.scriptPath, filePath, fileName); + const content = await getFileContentByName(_filePath); + return content; + } }