feat:自动识别逻辑增加

This commit is contained in:
fanxb 2022-11-27 20:22:42 +08:00
parent 9d8a6dcbfe
commit ca6dba024f
19 changed files with 648 additions and 380 deletions

View File

@ -0,0 +1,62 @@
import RuleInterface from "./RuleInterface";
import FileObj from "../../vo/FileObj";
import path from 'path';
let pattern = new RegExp(/s(eason)?(\d+)/);
let charSet = new Set([' ', '[', '.', '(', '']);
export default class InsertRule implements RuleInterface {
/**
* seasonname/
*/
type: string;
/**
*
*/
frontAdd: string;
/**
*
*/
endAdd: string;
constructor(data: any) {
this.type = data.type;
this.frontAdd = data.frontAdd;
this.endAdd = data.endAdd;
}
deal(file: FileObj): void {
//识别到的内容
let getStr = null;
let patternRes = path.basename(file.path).replace(/[ ]+/, "").toLocaleLowerCase().match(pattern);
if (this.type === 'season') {
if (patternRes && patternRes[2]) {
getStr = patternRes[2];
}
} else if (this.type === 'name') {
let originName = null;
if (patternRes && patternRes[2]) {
//说明是剧集,取父文件夹的父文件夹名称
originName = path.basename(path.resolve(file.path, '..'));
} else {
//说明是电影
originName = path.basename(file.path);
}
getStr = '';
for (let i = 0; i < originName.length; i++) {
let char = originName.charAt(i);
if (charSet.has(char)) {
break;
}
getStr += char;
}
}
if (getStr && getStr.length > 0) {
file.realName = file.realName + this.frontAdd + getStr + this.endAdd;
file.name = file.realName + file.expandName;
}
}
}

View File

@ -1,5 +1,5 @@
import RuleInterface from "./RuleInterface";
import FileObj from "../FileObj";
import FileObj from "../../vo/FileObj";
import path from 'path';
export default class DeleteRule implements RuleInterface {

View File

@ -1,5 +1,5 @@
import RuleInterface from "./RuleInterface";
import FileObj from "../FileObj";
import FileObj from "../../vo/FileObj";
import path from 'path';
@ -30,6 +30,14 @@ export default class InsertRule implements RuleInterface {
*/
autoSeason: boolean;
/**
*/
endFilter: boolean;
/**
*/
validEnd: Array<String>;
constructor(data: any) {
this.insertContent = data.insertContent;
@ -38,12 +46,19 @@ export default class InsertRule implements RuleInterface {
this.atIsRightToleft = data.atIsRightToleft;
this.ignorePostfix = data.ignorePostfix;
this.autoSeason = data.autoSeason;
this.endFilter = data.endFilter;
this.validEnd = data.validEnd;
}
deal(file: FileObj): void {
if (this.endFilter && file.expandName.length > 0 && this.validEnd.indexOf(file.expandName.substring(1)) == -1) {
//拓展名不符,跳过
return;
}
let str = this.ignorePostfix ? file.realName : file.name;
let season = '';
if (this.autoSeason) {
let patternRes = path.basename(file.path).replace(/[ ]+/, "").toLocaleLowerCase().match(pattern);
if (patternRes && patternRes[2]) {

View File

@ -1,4 +1,4 @@
import FileObj from "../FileObj";
import FileObj from "../../vo/FileObj";
export default interface RuleInterface {

View File

@ -1,5 +1,5 @@
import RuleInterface from "./RuleInterface";
import FileObj from "../FileObj";
import FileObj from "../../vo/FileObj";
import path from 'path';
export default class InsertRule implements RuleInterface {

View File

@ -4,6 +4,10 @@ export default class FileObj {
*
*/
name: string;
/**
*/
originName: string;
/**
*
*/
@ -36,6 +40,7 @@ export default class FileObj {
constructor(name: string, path, isFolder, createdTime, updatedTime) {
this.name = name;
this.originName = name;
this.expandName = pathUtil.extname(name);
if (this.expandName.length > 0) {
this.realName = name.substring(0, name.lastIndexOf("."));

View File

@ -1,6 +1,7 @@
import DeleteRule from "./rules/DeleteRule";
import InsertRule from "./rules/InsertRule";
import SerializationRule from "./rules/SerializationRule";
import DeleteRule from "../bo/rules/DeleteRule";
import InsertRule from "../bo/rules/InsertRule";
import SerializationRule from "../bo/rules/SerializationRule";
import AutoRule from "../bo/rules/AutoRule";
export default class RuleObj {
type: string;
@ -20,8 +21,15 @@ export default class RuleObj {
case "insert":
this.data = new InsertRule(data.data);
break;
default:
case "serialization":
this.data = new SerializationRule(data.data);
break;
case "auto":
this.data = new AutoRule(data.data);
break;
default:
throw new Error("不支持的规则:" + this.type);
}
}
}

View File

@ -3,7 +3,7 @@ import * as path from 'path';
import * as fs from 'fs-extra';
import ProcessHelper from '../util/ProcesHelper';
import FileObj from '../vo/FileObj';
import FileObj from '../entity/vo/FileObj';
import SavePathDao from '../dao/SavePathDao';
import SavePath from '../entity/dto/SavePath';
@ -28,6 +28,9 @@ class FileService {
fileList = await fs.readdir(pathStr);
}
} else {
if (!(fs.pathExists(pathStr))) {
throw new Error("路径不存在");
}
fileList = await fs.readdir(pathStr);
}
let folderList: Array<FileObj> = new Array();

View File

@ -2,9 +2,9 @@ import config from '../config';
import * as path from 'path';
import * as fs from 'fs-extra';
import FileObj from '../vo/FileObj';
import RuleObj from '../vo/RuleObj';
import RuleInterface from '../vo/rules/RuleInterface';
import FileObj from '../entity/vo/FileObj';
import RuleObj from '../entity/vo/RuleObj';
import RuleInterface from '../entity/bo/rules/RuleInterface';
class RenamerService {

View File

@ -8,7 +8,7 @@
"lint": "vue-cli-service lint"
},
"dependencies": {
"@element-plus/icons": "^0.0.11",
"@element-plus/icons-vue": "^2.0.10",
"axios": "^0.21.1",
"core-js": "^3.6.5",
"dayjs": "^1.10.7",

View File

@ -1,15 +1,18 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title>renamer</title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
<strong
>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to
continue.</strong
>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->

View File

@ -1,5 +1,6 @@
<template>
<div class="app">
<div class="head">菜单栏</div>
<div class="content">
<router-view />
</div>

View File

@ -36,8 +36,10 @@
<script>
import HttpUtil from "../utils/HttpUtil";
import Bus from "../utils/Bus";
export default {
name: "FileChose",
props: ["curChoosePath"],
data() {
return {
isWindows: false,
@ -61,9 +63,20 @@ export default {
return targetList.length > 0 ? targetList[0].id : null;
},
},
watch: {
async curChoosePath(newVal) {
console.log("变更路径:", newVal);
this.pathList = newVal;
await this.breadcrumbClick(this.pathList.length - 1);
},
},
async created() {
if (this.curChoosePath && this.curChoosePath.length > 0) {
this.pathList = this.curChoosePath;
}
await this.breadcrumbClick(this.pathList.length - 1);
await this.refreshSavePathList();
Bus.$on("refreshSavePathList", this.refreshSavePathList);
},
methods: {
@ -77,6 +90,7 @@ export default {
//
async breadcrumbClick(index) {
this.loading = true;
try {
let path = this.createPath(index);
let fileList = await HttpUtil.get("/file/query", {
path: encodeURIComponent(path),
@ -85,7 +99,9 @@ export default {
fileList.forEach((item) => (item.checked = false));
this.fileList = fileList;
this.filterText = "";
} finally {
this.loading = false;
}
return false;
},
//
@ -130,9 +146,8 @@ export default {
},
//
async savePath() {
let res = await HttpUtil.post("/file/path/save", null, { name: this.saveName, content: JSON.stringify(this.pathList) });
this.$emit("refreshSavePathList");
this.refreshSavePathList();
await HttpUtil.post("/file/path/save", null, { name: this.saveName, content: JSON.stringify(this.pathList) });
Bus.$emit("refreshSavePathList");
this.saveName = "";
this.showSave = false;
this.$message.success("操作成功");
@ -140,15 +155,9 @@ export default {
//
async cancelSavePath() {
await HttpUtil.delete("/file/path/delete", { id: this.curSavePathId });
this.refreshSavePathList();
this.$emit("refreshSavePathList");
Bus.$emit("refreshSavePathList");
this.$message.success("操作成功");
},
//
changePath(item) {
this.pathList = JSON.parse(item.content);
this.breadcrumbClick(this.pathList.length - 1);
},
},
};
</script>

View File

@ -5,11 +5,13 @@
<el-menu-item :disabled="editRule != null" index="delete">删除</el-menu-item>
<!-- <el-menu-item index="replace">替换</el-menu-item> -->
<el-menu-item :disabled="editRule != null" index="serialization">序列化</el-menu-item>
<el-menu-item :disabled="editRule != null" index="auto">自动识别</el-menu-item>
</el-menu>
<div class="rule">
<insert-rule ref="rule" :editRule="editRule" v-if="currentIndex == 'insert'" />
<delete-rule ref="rule" :editRule="editRule" v-else-if="currentIndex == 'delete'" />
<serialization-rule ref="rule" :editRule="editRule" v-else-if="currentIndex == 'serialization'" />
<auto-rule ref="rule" :editRule="editRule" v-else-if="currentIndex == 'auto'" />
</div>
</div>
<div style="text-align: center">
@ -21,9 +23,11 @@
import InsertRule from "./rules/InsertRule.vue";
import DeleteRule from "./rules/DeleteRule.vue";
import SerializationRule from "./rules/SerializationRule.vue";
import AutoRule from "./rules/AutoRule";
export default {
components: { InsertRule, DeleteRule, SerializationRule },
components: { InsertRule, DeleteRule, SerializationRule, AutoRule },
props: ["editRule"],
emits: ["ruleAdd"],
name: "Rule",
data() {
return {

View File

@ -0,0 +1,93 @@
<template>
<div class="flex">
<span class="left">识别类型</span>
<div class="location">
<el-radio v-model="ruleObj.data.type" label="season"
>季号识别
<el-tooltip effect="dark" :content="message2" placement="top">
<el-icon><InfoFilled /></el-icon>
</el-tooltip>
</el-radio>
<el-radio v-model="ruleObj.data.type" label="name"
>剧名/电影名识别
<el-tooltip effect="dark" :content="message2" placement="top">
<el-icon><InfoFilled /></el-icon>
</el-tooltip>
</el-radio>
</div>
</div>
<div class="flex">
<div class="left">前面追加:</div>
<el-input v-model="ruleObj.data.frontAdd" placeholder="识别内容前面追加,未识别到不会追加" style="width: 20em" />
</div>
<div class="flex">
<div class="left">后面追加:</div>
<el-input v-model="ruleObj.data.endAdd" placeholder="识别内容后面追加,未识别到不会追加" style="width: 20em" />
</div>
</template>
<script>
import { InfoFilled } from "@element-plus/icons-vue";
export default {
name: "AutoRule",
props: ["editRule"],
components: { InfoFilled },
data() {
return {
message1: '通过识别文件夹名称获取季号,放在插入文本最后,可识别"s1","s01","season 01","season01"等以s或season开头后接数字的',
message2:
"如果父文件夹包含season字段那么会从父文件夹的父文件夹名称中取剧名否则将从父文件夹名称中取电影名。规则为从开头开始取直到遇见第一个空格/./[等符号",
ruleObj: {
type: "auto",
message: "",
data: {
type: "",
frontAdd: "",
endAdd: "",
},
},
};
},
created() {
if (this.editRule) {
console.log(this.editRule);
this.ruleObj = JSON.parse(JSON.stringify(this.editRule));
}
},
methods: {
exportObj() {
if (this.ruleObj.data.type === "") {
this.$message({ message: "请选择识别类型", type: "warning" });
return null;
}
this.ruleObj.message = `自动识别:"${this.ruleObj.data.type == "season" ? "季号" : "剧名/电影名识别"}";`;
if (this.ruleObj.data.frontAdd.length > 0) {
this.ruleObj.message += `前缀添加:${this.ruleObj.data.frontAdd}`;
}
if (this.ruleObj.data.endAdd.length > 0) {
this.ruleObj.message += `后缀添加:${this.ruleObj.data.endAdd}`;
}
return this.ruleObj;
},
},
};
</script>
<style lang="less" scoped>
.flex {
display: flex;
justify-content: left;
align-items: center;
padding-top: 1em;
.left {
width: 6em;
}
.location {
justify-content: left;
flex-direction: column;
display: flex;
}
}
</style>

View File

@ -23,16 +23,36 @@
<div class="flex">
<div class="left">季号识别:</div>
<el-switch v-model="ruleObj.data.autoSeason" />
通过识别文件夹名称获取季号放在插入文本最后,可识别"s1","s01","season 01","season01"等以s或season开头后接数字的
<el-tooltip effect="dark" :content="message1" placement="top">
<el-icon><InfoFilled /></el-icon>
</el-tooltip>
</div>
<div class="flex">
<div class="left">有效后缀:</div>
<el-switch v-model="ruleObj.data.endFilter" />
<template v-if="ruleObj.data.endFilter">
<el-tag v-for="item in ruleObj.data.validEnd" closable :key="item" @close="deleteEnd(item)" text>{{ item }}</el-tag>
<el-input v-if="validEndInputShow" v-model="validEndInput" style="width: 5em" size="small" @keyup.enter="validEndAdd" @blur="validEndAdd" />
<el-button v-else class="button-new-tag ml-1" size="small" @click="validEndInputShow = true">+ 新增</el-button>
</template>
<el-tooltip effect="dark" :content="message2" placement="top">
<el-icon><InfoFilled /></el-icon>
</el-tooltip>
</div>
</template>
<script>
import { InfoFilled } from "@element-plus/icons-vue";
export default {
name: "InsertRule",
props: ["editRule"],
components: { InfoFilled },
data() {
return {
message1: '通过识别文件夹名称获取季号,放在插入文本最后,可识别"s1","s01","season 01","season01"等以s或season开头后接数字的',
message2: '开启本选项后,本规则只在后缀匹配时才会生效.(输入后缀不包含".")',
validEndInputShow: false,
validEndInput: "",
ruleObj: {
type: "insert",
message: "",
@ -43,6 +63,8 @@ export default {
atIsRightToleft: false,
ignorePostfix: true,
autoSeason: false,
endFilter: false,
validEnd: ["srt", "ass"],
},
},
};
@ -62,6 +84,14 @@ export default {
this.ruleObj.message = `插入:"${this.ruleObj.data.insertContent}"`;
return this.ruleObj;
},
validEndAdd() {
this.ruleObj.data.validEnd.push(this.validEndInput);
this.validEndInput = "";
this.validEndInputShow = false;
},
deleteEnd(item) {
this.ruleObj.data.validEnd.splice(this.ruleObj.data.validEnd.indexOf(item), 1);
},
},
};
</script>

View File

@ -0,0 +1,33 @@
class Bus {
constructor() {
this.list = {
}; // 收集订阅
}
// 订阅
$on (name, fn) {
this.list[name] = this.list[name] || [];
this.list[name].push(fn);
}
// 发布
$emit (name, data) {
if (this.list[name]) {
this.list[name].forEach((fn) => {
fn(data);
});
}
}
// 取消订阅
$off (name) {
if (this.list[name]) {
delete this.list[name];
}
}
}
export default new Bus;

View File

@ -1,7 +1,7 @@
<template>
<div v-loading="loading" element-loading-text="后台处理中,请稍候">
<br />
<el-button type="primary" @click="submit" size="default">重命名</el-button>
<el-button type="success" @click="submit" size="default">开始重命名</el-button>
<br /><br />
<!-- 规则列表 -->
<rule-block @ruleUpdate="ruleUpdate" />
@ -20,7 +20,6 @@
:key="item.id"
@click="clickSavePath(item)"
@close="deleteSavePath(item)"
type="primary"
text
>{{ item.name }}</el-tag
>
@ -56,17 +55,18 @@
<!-- 新增文件弹窗 -->
<el-dialog title="新增文件" v-model="dialogVisible" width="70%">
<file-chose ref="fileChose" :curSavePath="curSavePath" @addData="addData" @refreshSavePathList="refreshSavePathList" />
<file-chose ref="fileChose" :curChoosePath="curChoosePath" @addData="addData" @refreshSavePathList="refreshSavePathList" />
</el-dialog>
</div>
</template>
<script>
// @ is an alias to /src
import { ArrowDownBold, ArrowUpBold } from "@element-plus/icons";
import { ArrowDownBold, ArrowUpBold } from "@element-plus/icons-vue";
import HttpUtil from "../../utils/HttpUtil";
import FileChose from "@/components/FileChose";
import RuleBlock from "./components/RuleBlock.vue";
import Bus from "../../utils/Bus";
let numberSet = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@ -88,12 +88,13 @@ export default {
needPreview: false, //
applicationRule: null, //
savePathList: [], //
curSavePath: null, //
curChoosePath: null, //
};
},
async created() {
this.savePathList = await HttpUtil.get("/file/path");
window.isWindows = await HttpUtil.get("/file/isWindows");
Bus.$on("refreshSavePathList", this.refreshSavePathList);
},
methods: {
//
@ -181,17 +182,14 @@ export default {
this.dialogVisible = true;
},
//
clickSavePath(item) {
async clickSavePath(item) {
this.curChoosePath = JSON.parse(item.content);
this.dialogVisible = true;
console.log(item);
this.$nextTick(() => {
this.$refs["fileChose"].changePath(item);
});
},
async deleteSavePath(item) {
console.log(item);
await HttpUtil.delete("/file/path/delete", { id: item.id });
await this.refreshSavePathList();
Bus.$emit("refreshSavePathList");
},
async refreshSavePathList() {
this.savePathList = await HttpUtil.get("/file/path");

View File

@ -12,7 +12,7 @@
>
</div>
<div class="ruleBlock">
<el-checkbox v-model="item.checked" v-for="(item, index) in ruleList" :key="index">
<el-checkbox v-model="item.checked" v-for="(item, index) in ruleList" :key="index" @dblclick="editClick(item)">
<s v-if="item.blocked">{{ item.message }}</s>
<span v-else>{{ item.message }}</span>
</el-checkbox>
@ -21,7 +21,7 @@
</div>
</div>
<!-- 弹窗 -->
<el-dialog title="新增规则" v-model="addRuleDialogShow" width="70%">
<el-dialog title="新增规则" v-model="addRuleDialogShow" width="70%" @close="ruleDialogClose">
<rule :editRule="editRule" @ruleAdd="ruleAdd" v-if="addRuleDialogShow" />
</el-dialog>
<el-dialog title="选择规则模板" v-model="ruleTemplateShow" width="70%">
@ -142,10 +142,14 @@ export default {
this.ruleUpdate();
},
//
editClick() {
this.editRule = this.checkedRules[0];
editClick(rule) {
this.editRule = rule && rule.data ? rule : this.checkedRules[0];
this.addRuleDialogShow = true;
},
ruleDialogClose() {
this.editRule = null;
this.addRuleDialogShow = false;
},
},
};
</script>