97 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-01-12 22:55:40 +08:00
import config from '../config';
import * as path from 'path';
import * as fs from 'fs-extra';
import ProcessHelper from '../util/ProcesHelper';
import FileObj from '../entity/vo/FileObj';
import SavePathDao from '../dao/SavePathDao';
import SavePath from '../entity/po/SavePath';
import AutoPlanConfigDto from '../entity/dto/AutoPlanConfigDto';
import GlobalConfig from 'entity/po/GlobalConfig';
import GlobalConfigService from './GlobalConfigService';
2023-01-31 23:06:08 +08:00
import ErrorHelper from 'util/ErrorHelper';
2023-01-12 22:55:40 +08:00
const autoConfigCode = "autoConfig";
2023-01-31 20:41:38 +08:00
/**
*
*/
2023-01-31 23:06:08 +08:00
let needDeal = [];
2023-01-31 20:41:38 +08:00
/**
* key:变更前的目录value:变更后的目录.当needDeal为空时清理pathMap
*/
let pathMap = {};
/**
*
*/
let autoConfig: AutoPlanConfigDto = null;
2023-01-12 22:55:40 +08:00
class AutoPlanService {
2023-01-31 20:41:38 +08:00
static async init() {
let str = await GlobalConfigService.getVal(autoConfigCode);
if (str != null) {
} else {
autoConfig = JSON.parse(str);
}
}
2023-01-12 22:55:40 +08:00
/**
*
*/
static async saveAutoConfig(body: AutoPlanConfigDto): Promise<void> {
2023-01-31 23:06:08 +08:00
if (body.start) {
if (body.paths.length == 0) {
throw ErrorHelper.Error400("视频路径为空");
}
if (body.rules.length == 0) {
throw ErrorHelper.Error400("规则为空");
}
}
2023-01-12 22:55:40 +08:00
let configBody: GlobalConfig = {
code: autoConfigCode,
val: JSON.stringify(body),
description: "自动化计划配置"
};
await GlobalConfigService.insertOrReplace(configBody);
2023-01-31 20:41:38 +08:00
autoConfig = body;
if (body.start && !body.ignoreExist) {
2023-01-31 23:06:08 +08:00
await readDir(body.paths);
2023-01-31 20:41:38 +08:00
}
}
}
2023-01-31 23:06:08 +08:00
async function readDir(dirList: Array<string>): Promise<void> {
if (!dirList) {
return;
}
for (let i in dirList) {
let pathStr = dirList[i];
if (checkIgnore(pathStr)) {
continue;
}
if (!(await fs.stat(pathStr)).isDirectory()) {
needDeal.push(pathStr);
continue;
}
await readDir((await fs.readdir(pathStr)).map(item => path.join(pathStr, item)));
}
}
2023-01-31 20:41:38 +08:00
/**
*
*/
function checkIgnore(str: string): boolean {
for (let i in autoConfig.ignorePaths) {
if (str.match(autoConfig.ignorePaths[i])) {
return true;
2023-01-12 22:55:40 +08:00
}
}
2023-01-31 20:41:38 +08:00
return false;
2023-01-12 22:55:40 +08:00
}
export default AutoPlanService;