diff --git a/openRenamerBackend/api/ApplicationRuleApi.ts b/openRenamerBackend/api/ApplicationRuleApi.ts index deabd75..2299846 100644 --- a/openRenamerBackend/api/ApplicationRuleApi.ts +++ b/openRenamerBackend/api/ApplicationRuleApi.ts @@ -1,29 +1,29 @@ -import { Context } from "koa"; -import ApplicationRuleService from "../service/ApplicationRuleService"; -import config from "../config"; - -const router = {}; - -/** - * 获取目录下的文件列表 - */ -router["GET /applicationRule"] = async function (ctx: Context) { - ctx.body = await ApplicationRuleService.getAll(); -}; - -/** - * 更新或者插入 - */ -router['POST /applicationRule'] = async function (ctx: Context) { - ctx.body = await ApplicationRuleService.saveOrAdd(ctx.request.body); -} - -/** - * 删除 - */ -router["DELETE /applicationRule/:id"] = async function (ctx: Context) { - await ApplicationRuleService.deleteById(ctx.params.id); - ctx.body = ""; -}; - -export default router; +import { Context } from "koa"; +import ApplicationRuleService from "../service/ApplicationRuleService"; +import config from "../config"; + +const router = {}; + +/** + * 获取目录下的文件列表 + */ +router["GET /applicationRule"] = async function (ctx: Context) { + ctx.body = await ApplicationRuleService.getAll(); +}; + +/** + * 更新或者插入 + */ +router['POST /applicationRule'] = async function (ctx: Context) { + ctx.body = await ApplicationRuleService.saveOrAdd(ctx.request.body); +} + +/** + * 删除 + */ +router["DELETE /applicationRule/:id"] = async function (ctx: Context) { + await ApplicationRuleService.deleteById(ctx.params.id); + ctx.body = ""; +}; + +export default router; diff --git a/openRenamerBackend/api/RenamerApi.ts b/openRenamerBackend/api/RenamerApi.ts index e22e861..8e18334 100644 --- a/openRenamerBackend/api/RenamerApi.ts +++ b/openRenamerBackend/api/RenamerApi.ts @@ -1,22 +1,22 @@ -import { Context } from "koa"; -import RenamerService from "../service/RenamerService"; - -const router = {}; - -/** - * 预览文件修改后的状态 - */ -router["POST /renamer/preview"] = async function (ctx: Context) { - ctx.body = await RenamerService.preview(ctx.request.body.fileList, ctx.request.body.ruleList); -}; - -/** - * 提交修改 - */ -router["POST /renamer/submit"] = async function (ctx: Context) { - ctx.body = await RenamerService.rename(ctx.request.body.fileList, ctx.request.body.changedFileList); -}; - - - -export default router; +import { Context } from "koa"; +import RenamerService from "../service/RenamerService"; + +const router = {}; + +/** + * 预览文件修改后的状态 + */ +router["POST /renamer/preview"] = async function (ctx: Context) { + ctx.body = await RenamerService.preview(ctx.request.body.fileList, ctx.request.body.ruleList); +}; + +/** + * 提交修改 + */ +router["POST /renamer/submit"] = async function (ctx: Context) { + ctx.body = await RenamerService.rename(ctx.request.body.fileList, ctx.request.body.changedFileList); +}; + + + +export default router; diff --git a/openRenamerBackend/dao/ApplicationRuleDao.ts b/openRenamerBackend/dao/ApplicationRuleDao.ts index 41383b6..5b223e0 100644 --- a/openRenamerBackend/dao/ApplicationRuleDao.ts +++ b/openRenamerBackend/dao/ApplicationRuleDao.ts @@ -1,52 +1,52 @@ -import ErrorHelper from "../util/ErrorHelper"; -import ApplicationRule from "../entity/dto/ApplicationRule"; -import SqliteHelper from "../util/SqliteHelper"; - -export default class ApplicationRuleDao { - /** - * 查询所有 - * @param obj - * @returns - */ - static async getAll(): Promise> { - let res = await SqliteHelper.pool.all('select id,createdDate,updatedDate,name,comment,content from application_rule'); - return res; - } - - - /** - * 新增 - * @param obj - * @returns - */ - static async addOne(obj: ApplicationRule): Promise { - let res = await SqliteHelper.pool.run('insert into application_rule(createdDate,updatedDate,name,comment,content) values(?,?,?,?,?)' - , obj.createdDate, obj.updatedDate, obj.name, obj.comment, obj.content); - return res.lastID; - } - - /** - * 更新 - * @param obj - */ - static async updateOne(obj: ApplicationRule): Promise { - let res = await SqliteHelper.pool.run('update application_rule set updatedDate=?,name=?,comment=?,content=? where id=?' - , obj.updatedDate, obj.name, obj.comment, obj.content, obj.id); - if (res.changes == 0) { - throw ErrorHelper.Error404("数据不存在"); - } - } - - /** - * 删除 - * @param id - */ - static async delete(id: number): Promise { - let res = await SqliteHelper.pool.run('delete from application_rule where id=?', id); - if (res.changes == 0) { - throw ErrorHelper.Error404("数据不存在"); - } - } - - +import ErrorHelper from "../util/ErrorHelper"; +import ApplicationRule from "../entity/dto/ApplicationRule"; +import SqliteHelper from "../util/SqliteHelper"; + +export default class ApplicationRuleDao { + /** + * 查询所有 + * @param obj + * @returns + */ + static async getAll(): Promise> { + let res = await SqliteHelper.pool.all('select id,createdDate,updatedDate,name,comment,content from application_rule'); + return res; + } + + + /** + * 新增 + * @param obj + * @returns + */ + static async addOne(obj: ApplicationRule): Promise { + let res = await SqliteHelper.pool.run('insert into application_rule(createdDate,updatedDate,name,comment,content) values(?,?,?,?,?)' + , obj.createdDate, obj.updatedDate, obj.name, obj.comment, obj.content); + return res.lastID; + } + + /** + * 更新 + * @param obj + */ + static async updateOne(obj: ApplicationRule): Promise { + let res = await SqliteHelper.pool.run('update application_rule set updatedDate=?,name=?,comment=?,content=? where id=?' + , obj.updatedDate, obj.name, obj.comment, obj.content, obj.id); + if (res.changes == 0) { + throw ErrorHelper.Error404("数据不存在"); + } + } + + /** + * 删除 + * @param id + */ + static async delete(id: number): Promise { + let res = await SqliteHelper.pool.run('delete from application_rule where id=?', id); + if (res.changes == 0) { + throw ErrorHelper.Error404("数据不存在"); + } + } + + } \ No newline at end of file diff --git a/openRenamerBackend/database.db b/openRenamerBackend/database.db deleted file mode 100644 index e69de29..0000000 diff --git a/openRenamerBackend/entity/dto/ApplicationRule.ts b/openRenamerBackend/entity/dto/ApplicationRule.ts index 8692c0b..3565fcd 100644 --- a/openRenamerBackend/entity/dto/ApplicationRule.ts +++ b/openRenamerBackend/entity/dto/ApplicationRule.ts @@ -1,23 +1,23 @@ -export default class ApplicationRule { - /** - 创建时间 - */ - createdDate: number; - /** - 更新时间 - */ - updatedDate: number; - id: number; - /** - 名称 - */ - name: string; - /** - 说明 - */ - comment: string; - /** - 规则内容,json序列化后 - */ - content: string; +export default class ApplicationRule { + /** + 创建时间 + */ + createdDate: number; + /** + 更新时间 + */ + updatedDate: number; + id: number; + /** + 名称 + */ + name: string; + /** + 说明 + */ + comment: string; + /** + 规则内容,json序列化后 + */ + content: string; } \ No newline at end of file diff --git a/openRenamerBackend/index.ts b/openRenamerBackend/index.ts index f170ae1..8dff10c 100644 --- a/openRenamerBackend/index.ts +++ b/openRenamerBackend/index.ts @@ -1,39 +1,39 @@ -import koa from "koa"; -import Router from "koa-router"; -import koaBody from "koa-body"; -import * as path from "path"; -import RouterMW from "./middleware/controllerEngine"; - -import config from "./config"; -import handleError from "./middleware/handleError"; -import init from "./middleware/init"; -import SqliteUtil from './util/SqliteHelper'; -import log from './util/LogUtil'; - - -console.log(config); -const app = new koa(); - -let router = new Router({ - prefix: config.urlPrefix -}); - -app.use(require('koa-static')(path.join(config.rootPath, 'static'))); - -//表单解析 -app.use(koaBody(config.bodyLimit)); -//请求预处理 -app.use(init); -//错误处理 -app.use(handleError); - -app.use(RouterMW(router, path.join(config.rootPath, "dist/api"))); -(async () => { - await SqliteUtil.createPool(); - app.listen(config.port); - log.info(`server listened `, config.port); -})(); - -app.on("error", (error) => { - console.error(error); -}) +import koa from "koa"; +import Router from "koa-router"; +import koaBody from "koa-body"; +import * as path from "path"; +import RouterMW from "./middleware/controllerEngine"; + +import config from "./config"; +import handleError from "./middleware/handleError"; +import init from "./middleware/init"; +import SqliteUtil from './util/SqliteHelper'; +import log from './util/LogUtil'; + + +console.log(config); +const app = new koa(); + +let router = new Router({ + prefix: config.urlPrefix +}); + +app.use(require('koa-static')(path.join(config.rootPath, 'static'))); + +//表单解析 +app.use(koaBody(config.bodyLimit)); +//请求预处理 +app.use(init); +//错误处理 +app.use(handleError); + +app.use(RouterMW(router, path.join(config.rootPath, "dist/api"))); +(async () => { + await SqliteUtil.createPool(); + app.listen(config.port); + log.info(`server listened `, config.port); +})(); + +app.on("error", (error) => { + console.error(error); +}) diff --git a/openRenamerBackend/middleware/controllerEngine.ts b/openRenamerBackend/middleware/controllerEngine.ts index b90351e..51268ce 100644 --- a/openRenamerBackend/middleware/controllerEngine.ts +++ b/openRenamerBackend/middleware/controllerEngine.ts @@ -1,50 +1,50 @@ -import * as fs from 'fs-extra'; -import * as path from 'path'; -import log from '../util/LogUtil'; - -async function addMapping(router, filePath: string) { - let mapping = require(filePath).default; - for (let url in mapping) { - if (url.startsWith('GET ')) { - let temp = url.substring(4); - router.get(temp, mapping[url]); - log.info(`----GET:${temp}`); - } else if (url.startsWith('POST ')) { - let temp = url.substring(5); - router.post(temp, mapping[url]); - log.info(`----POST:${temp}`); - } else if (url.startsWith('PUT ')) { - let temp = url.substring(4); - router.put(temp, mapping[url]); - log.info(`----PUT:${temp}`); - } else if (url.startsWith('DELETE ')) { - let temp = url.substring(7); - router.delete(temp, mapping[url]); - log.info(`----DELETE: ${temp}`); - } else { - log.info(`xxxxx无效路径:${url}`); - } - } -} - -function addControllers(router, filePath) { - let files = fs.readdirSync(filePath).filter(item => item.endsWith('.js')); - for (let index in files) { - let element = files[index]; - let temp = path.join(filePath, element); - let state = fs.statSync(temp); - if (state.isDirectory()) { - addControllers(router, temp); - } else { - if (!temp.endsWith('Helper.js')) { - log.info('\n--开始处理: ' + element + '路由'); - addMapping(router, temp); - } - } - } -} - -export default function engine(router, folder) { - addControllers(router, folder); - return router.routes(); -} +import * as fs from 'fs-extra'; +import * as path from 'path'; +import log from '../util/LogUtil'; + +async function addMapping(router, filePath: string) { + let mapping = require(filePath).default; + for (let url in mapping) { + if (url.startsWith('GET ')) { + let temp = url.substring(4); + router.get(temp, mapping[url]); + log.info(`----GET:${temp}`); + } else if (url.startsWith('POST ')) { + let temp = url.substring(5); + router.post(temp, mapping[url]); + log.info(`----POST:${temp}`); + } else if (url.startsWith('PUT ')) { + let temp = url.substring(4); + router.put(temp, mapping[url]); + log.info(`----PUT:${temp}`); + } else if (url.startsWith('DELETE ')) { + let temp = url.substring(7); + router.delete(temp, mapping[url]); + log.info(`----DELETE: ${temp}`); + } else { + log.info(`xxxxx无效路径:${url}`); + } + } +} + +function addControllers(router, filePath) { + let files = fs.readdirSync(filePath).filter(item => item.endsWith('.js')); + for (let index in files) { + let element = files[index]; + let temp = path.join(filePath, element); + let state = fs.statSync(temp); + if (state.isDirectory()) { + addControllers(router, temp); + } else { + if (!temp.endsWith('Helper.js')) { + log.info('\n--开始处理: ' + element + '路由'); + addMapping(router, temp); + } + } + } +} + +export default function engine(router, folder) { + addControllers(router, folder); + return router.routes(); +} diff --git a/openRenamerBackend/middleware/handleError.ts b/openRenamerBackend/middleware/handleError.ts index 79a803c..687f1e6 100644 --- a/openRenamerBackend/middleware/handleError.ts +++ b/openRenamerBackend/middleware/handleError.ts @@ -1,17 +1,17 @@ -import log from '../util/LogUtil'; - -let f = async (ctx, next) => { - try { - await next(); - } catch (error: any) { - if (error.status != undefined) { - ctx.status = error.status; - } else { - ctx.status = 500; - } - ctx.body = error.message; - log.error(error); - } -} - +import log from '../util/LogUtil'; + +let f = async (ctx, next) => { + try { + await next(); + } catch (error: any) { + if (error.status != undefined) { + ctx.status = error.status; + } else { + ctx.status = 500; + } + ctx.body = error.message; + log.error(error); + } +} + export default f; \ No newline at end of file diff --git a/openRenamerBackend/middleware/init.ts b/openRenamerBackend/middleware/init.ts index e9a2ebd..0a53b4a 100644 --- a/openRenamerBackend/middleware/init.ts +++ b/openRenamerBackend/middleware/init.ts @@ -1,51 +1,51 @@ -import config from '../config'; -import ObjectHelper from '../util/ObjectOperate'; - -let doSuccess = (ctx, body) => { - switch (ctx.method) { - case 'GET': - ctx.status = body !== null ? 200 : 204; - ctx.body = body; - break; - case 'POST': - ctx.status = body !== null ? 201 : 204; - ctx.body = body; - break; - case 'PUT': - ctx.status = body !== null ? 200 : 204; - ctx.body = body; - break; - case 'DELETE': - ctx.status = body !== null ? 200 : 204; - ctx.body = body; - break; - } - Object.assign(ctx.allParams, ctx.params); -} - -export default async (ctx, next) => { - //跨域 - ctx.set("Access-Control-Allow-Origin", "*"); - ctx.set("Access-Control-Allow-Headers", "X-Requested-With"); - ctx.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); - ctx.set("X-Powered-By", ' 3.2.1'); - ctx.set("Content-Type", "application/json;charset=utf-8"); - //合并请求参数到allParams - let objs = new Array(); - if (ctx.method == "POST" || ctx.method == "PUT") { - if (ctx.request.body) { - if (ctx.request.body.fields != undefined && ctx.request.body.files != undefined) { - objs.push(ctx.request.body.fields, ctx.request.body.files); - } else { - objs.push(ctx.request.body); - } - } - } - objs.push(ctx.query); - ctx.allParams = ObjectHelper.combineObject(objs); - - ctx.onSuccess = function (body = null) { - doSuccess(ctx, body); - }; - await next(); +import config from '../config'; +import ObjectHelper from '../util/ObjectOperate'; + +let doSuccess = (ctx, body) => { + switch (ctx.method) { + case 'GET': + ctx.status = body !== null ? 200 : 204; + ctx.body = body; + break; + case 'POST': + ctx.status = body !== null ? 201 : 204; + ctx.body = body; + break; + case 'PUT': + ctx.status = body !== null ? 200 : 204; + ctx.body = body; + break; + case 'DELETE': + ctx.status = body !== null ? 200 : 204; + ctx.body = body; + break; + } + Object.assign(ctx.allParams, ctx.params); +} + +export default async (ctx, next) => { + //跨域 + ctx.set("Access-Control-Allow-Origin", "*"); + ctx.set("Access-Control-Allow-Headers", "X-Requested-With"); + ctx.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); + ctx.set("X-Powered-By", ' 3.2.1'); + ctx.set("Content-Type", "application/json;charset=utf-8"); + //合并请求参数到allParams + let objs = new Array(); + if (ctx.method == "POST" || ctx.method == "PUT") { + if (ctx.request.body) { + if (ctx.request.body.fields != undefined && ctx.request.body.files != undefined) { + objs.push(ctx.request.body.fields, ctx.request.body.files); + } else { + objs.push(ctx.request.body); + } + } + } + objs.push(ctx.query); + ctx.allParams = ObjectHelper.combineObject(objs); + + ctx.onSuccess = function (body = null) { + doSuccess(ctx, body); + }; + await next(); } \ No newline at end of file diff --git a/openRenamerBackend/service/ApplicationRuleService.ts b/openRenamerBackend/service/ApplicationRuleService.ts index 3a648bf..b4078cf 100644 --- a/openRenamerBackend/service/ApplicationRuleService.ts +++ b/openRenamerBackend/service/ApplicationRuleService.ts @@ -1,33 +1,33 @@ -import config from '../config'; -import * as path from 'path'; -import * as fs from 'fs-extra'; -import ApplicationRule from '../entity/dto/ApplicationRule'; -import ApplicationRuleDao from '../dao/ApplicationRuleDao'; - - - -class ApplicationRuleService { - static async saveOrAdd(ruleObj: ApplicationRule): Promise { - ruleObj.updatedDate = Date.now(); - if (!ruleObj.id) { - //说明是新增 - ruleObj.createdDate = Date.now(); - ruleObj.id = await ApplicationRuleDao.addOne(ruleObj); - } else { - //说明是修改 - await ApplicationRuleDao.updateOne(ruleObj); - } - return ruleObj; - } - - static async getAll(): Promise> { - return await ApplicationRuleDao.getAll(); - } - - static async deleteById(id: number): Promise { - await ApplicationRuleDao.delete(id); - } - -} - -export default ApplicationRuleService; +import config from '../config'; +import * as path from 'path'; +import * as fs from 'fs-extra'; +import ApplicationRule from '../entity/dto/ApplicationRule'; +import ApplicationRuleDao from '../dao/ApplicationRuleDao'; + + + +class ApplicationRuleService { + static async saveOrAdd(ruleObj: ApplicationRule): Promise { + ruleObj.updatedDate = Date.now(); + if (!ruleObj.id) { + //说明是新增 + ruleObj.createdDate = Date.now(); + ruleObj.id = await ApplicationRuleDao.addOne(ruleObj); + } else { + //说明是修改 + await ApplicationRuleDao.updateOne(ruleObj); + } + return ruleObj; + } + + static async getAll(): Promise> { + return await ApplicationRuleDao.getAll(); + } + + static async deleteById(id: number): Promise { + await ApplicationRuleDao.delete(id); + } + +} + +export default ApplicationRuleService; diff --git a/openRenamerBackend/sqls/v001__init.sql b/openRenamerBackend/sqls/v001__init.sql index b26904d..d266d29 100644 --- a/openRenamerBackend/sqls/v001__init.sql +++ b/openRenamerBackend/sqls/v001__init.sql @@ -1,14 +1,14 @@ --- 初始化建表 --- 应用规则表 -CREATE TABLE application_rule ( - -- 创建时间 - createdDate INTEGER NOT NULL, - -- 更新时间 - updatedDate INTEGER NOT NULL, - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - -- 注释 - comment TEXT NOT NULL DEFAULT '', - -- 规则内容,json序列化后保存 - content TEXT NOT NULL DEFAULT '' +-- 初始化建表 +-- 应用规则表 +CREATE TABLE application_rule ( + -- 创建时间 + createdDate INTEGER NOT NULL, + -- 更新时间 + updatedDate INTEGER NOT NULL, + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + -- 注释 + comment TEXT NOT NULL DEFAULT '', + -- 规则内容,json序列化后保存 + content TEXT NOT NULL DEFAULT '' ); \ No newline at end of file diff --git a/openRenamerBackend/util/ErrorHelper.ts b/openRenamerBackend/util/ErrorHelper.ts index cb8240a..c8d12b7 100644 --- a/openRenamerBackend/util/ErrorHelper.ts +++ b/openRenamerBackend/util/ErrorHelper.ts @@ -1,32 +1,32 @@ -class ErrorHelper { - /** - * 返回一个自定义错误 - * @param {String} message - * @param {Number} status - */ - static newError(message, status) { - return getError(message, status); - } - - static Error403(message){ - return getError(message,403); - } - static Error404(message){ - return getError(message,404); - } - static Error406(message){ - return getError(message,406); - } - static Error400(message){ - return getError(message,400); - } - -} - -let getError = (message, status) => { - let error = new Error(message); - error['status'] = status; - return error; -} - +class ErrorHelper { + /** + * 返回一个自定义错误 + * @param {String} message + * @param {Number} status + */ + static newError(message, status) { + return getError(message, status); + } + + static Error403(message){ + return getError(message,403); + } + static Error404(message){ + return getError(message,404); + } + static Error406(message){ + return getError(message,406); + } + static Error400(message){ + return getError(message,400); + } + +} + +let getError = (message, status) => { + let error = new Error(message); + error['status'] = status; + return error; +} + export default ErrorHelper; \ No newline at end of file diff --git a/openRenamerBackend/util/LogUtil.ts b/openRenamerBackend/util/LogUtil.ts index b57116b..40533a6 100644 --- a/openRenamerBackend/util/LogUtil.ts +++ b/openRenamerBackend/util/LogUtil.ts @@ -1,9 +1,9 @@ -import { getLogger, configure } from "log4js"; -configure({ - appenders: { cheese: { type: "console" } }, - categories: { default: { appenders: ["cheese"], level: "info" } } -}); -const logger = getLogger(); -logger.level = "debug"; - +import { getLogger, configure } from "log4js"; +configure({ + appenders: { cheese: { type: "console" } }, + categories: { default: { appenders: ["cheese"], level: "info" } } +}); +const logger = getLogger(); +logger.level = "debug"; + export default logger; \ No newline at end of file diff --git a/openRenamerBackend/util/ObjectOperate.ts b/openRenamerBackend/util/ObjectOperate.ts index 8287ed2..115cc3c 100644 --- a/openRenamerBackend/util/ObjectOperate.ts +++ b/openRenamerBackend/util/ObjectOperate.ts @@ -1,18 +1,18 @@ -/* - 合并node对象,对于相同的属性后面覆盖前面 -*/ -class ObjectOperation { - static combineObject(...objs) { - if (objs.length == 1 && objs[0] instanceof Array) { - objs = objs[0]; - } - let sum = {}; - let length = objs.length; - for (let i = 0; i < length; i++) { - sum = Object.assign(sum,objs[i]); - } - return sum; - } -} - +/* + 合并node对象,对于相同的属性后面覆盖前面 +*/ +class ObjectOperation { + static combineObject(...objs) { + if (objs.length == 1 && objs[0] instanceof Array) { + objs = objs[0]; + } + let sum = {}; + let length = objs.length; + for (let i = 0; i < length; i++) { + sum = Object.assign(sum,objs[i]); + } + return sum; + } +} + export default ObjectOperation \ No newline at end of file diff --git a/openRenamerBackend/vo/FileObj.ts b/openRenamerBackend/vo/FileObj.ts index aa0703b..79e3cee 100644 --- a/openRenamerBackend/vo/FileObj.ts +++ b/openRenamerBackend/vo/FileObj.ts @@ -1,50 +1,50 @@ -import * as pathUtil from "path"; -export default class FileObj { - /** - * 文件名 - */ - name: string; - /** - * 拓展名 - */ - expandName: string; - /** - * 去掉拓展名后的名字 - */ - realName: string; - /** - * 所属路径 - */ - path: string; - /** - * 是否文件夹 - */ - isFolder: boolean; - /** - * 重命名错误原因 - */ - errorMessage: string; - /** - * 创建时间ms - */ - createdTime: number; - /** - * 更新时间ms - */ - updatedTime: number; - - - constructor(name: string, path, isFolder, createdTime, updatedTime) { - this.name = name; - this.expandName = pathUtil.extname(name); - if (this.expandName.length > 0) { - this.realName = name.substring(0, name.lastIndexOf(".")); - } else { - this.realName = name; - } - this.path = path; - this.isFolder = isFolder; - this.createdTime = createdTime; - this.updatedTime = updatedTime; - } +import * as pathUtil from "path"; +export default class FileObj { + /** + * 文件名 + */ + name: string; + /** + * 拓展名 + */ + expandName: string; + /** + * 去掉拓展名后的名字 + */ + realName: string; + /** + * 所属路径 + */ + path: string; + /** + * 是否文件夹 + */ + isFolder: boolean; + /** + * 重命名错误原因 + */ + errorMessage: string; + /** + * 创建时间ms + */ + createdTime: number; + /** + * 更新时间ms + */ + updatedTime: number; + + + constructor(name: string, path, isFolder, createdTime, updatedTime) { + this.name = name; + this.expandName = pathUtil.extname(name); + if (this.expandName.length > 0) { + this.realName = name.substring(0, name.lastIndexOf(".")); + } else { + this.realName = name; + } + this.path = path; + this.isFolder = isFolder; + this.createdTime = createdTime; + this.updatedTime = updatedTime; + } } \ No newline at end of file diff --git a/openRenamerBackend/vo/RuleObj.ts b/openRenamerBackend/vo/RuleObj.ts index 55122a3..8a1e925 100644 --- a/openRenamerBackend/vo/RuleObj.ts +++ b/openRenamerBackend/vo/RuleObj.ts @@ -1,27 +1,27 @@ -import DeleteRule from "./rules/DeleteRule"; -import InsertRule from "./rules/InsertRule"; -import SerializationRule from "./rules/SerializationRule"; - -export default class RuleObj { - type: string; - message: string; - /** - * 具体参数 - */ - data: any; - - constructor(data: any) { - this.type = data.type; - this.message = data.message; - switch (this.type) { - case "delete": - this.data = new DeleteRule(data.data); - break; - case "insert": - this.data = new InsertRule(data.data); - break; - default: - this.data = new SerializationRule(data.data); - } - } +import DeleteRule from "./rules/DeleteRule"; +import InsertRule from "./rules/InsertRule"; +import SerializationRule from "./rules/SerializationRule"; + +export default class RuleObj { + type: string; + message: string; + /** + * 具体参数 + */ + data: any; + + constructor(data: any) { + this.type = data.type; + this.message = data.message; + switch (this.type) { + case "delete": + this.data = new DeleteRule(data.data); + break; + case "insert": + this.data = new InsertRule(data.data); + break; + default: + this.data = new SerializationRule(data.data); + } + } } \ No newline at end of file diff --git a/openRenamerBackend/vo/rules/DeleteRule.ts b/openRenamerBackend/vo/rules/DeleteRule.ts index aacd042..3a63cb8 100644 --- a/openRenamerBackend/vo/rules/DeleteRule.ts +++ b/openRenamerBackend/vo/rules/DeleteRule.ts @@ -1,92 +1,92 @@ -import RuleInterface from "./RuleInterface"; -import FileObj from "../FileObj"; -import path from 'path'; - -export default class DeleteRule implements RuleInterface { - /** - * 类别:deletePart:部分删除,deleteAll:全部删除 - */ - type: string; - /** - * 部分删除时的开始信息 - */ - start: DeleteRuleItem; - /** - * 部分删除时的结束信息 - - */ - end: DeleteRuleItem; - /** - * 忽略拓展名,true:忽略,false:不忽略 - */ - ignorePostfix: boolean; - - constructor(data: any) { - this.type = data.type; - this.start = new DeleteRuleItem(data.start); - this.end = new DeleteRuleItem(data.end); - this.ignorePostfix = data.ignorePostfix; - } - - - - deal(file: FileObj): void { - if (this.type === 'deleteAll') { - file.realName = ""; - if (!this.ignorePostfix) { - file.expandName = ""; - } - } else { - let str = file.realName + (this.ignorePostfix ? "" : file.expandName); - let startIndex = this.start.calIndex(str); - let endIndex = this.end.calIndex(str); - if (startIndex < 0 || endIndex < 0) { - return; - } - str = str.substring(0, startIndex) + str.substring(endIndex + 1); - if (this.ignorePostfix) { - file.realName = str; - } else { - file.expandName = path.extname(str); - if (file.expandName.length > 0) { - file.realName = str.substring(0, str.lastIndexOf(".")); - } else { - file.realName = str; - } - } - } - - file.name = file.realName + file.expandName; - } - -} - -class DeleteRuleItem { - /** - * location:位置,text:文本,end:直到末尾 - */ - type: string; - /** - * 对应的值 - */ - value: string; - - constructor(data: any) { - this.type = data.type; - this.value = data.value; - } - - /** - * 计算位置 - */ - calIndex(str: string): number { - if (this.type === 'location') { - return parseInt(this.value) - 1; - } else if (this.type === 'text') { - return str.indexOf(this.value); - } else if (this.type === 'end') { - return str.length - 1; - } - return -1; - } -} +import RuleInterface from "./RuleInterface"; +import FileObj from "../FileObj"; +import path from 'path'; + +export default class DeleteRule implements RuleInterface { + /** + * 类别:deletePart:部分删除,deleteAll:全部删除 + */ + type: string; + /** + * 部分删除时的开始信息 + */ + start: DeleteRuleItem; + /** + * 部分删除时的结束信息 + + */ + end: DeleteRuleItem; + /** + * 忽略拓展名,true:忽略,false:不忽略 + */ + ignorePostfix: boolean; + + constructor(data: any) { + this.type = data.type; + this.start = new DeleteRuleItem(data.start); + this.end = new DeleteRuleItem(data.end); + this.ignorePostfix = data.ignorePostfix; + } + + + + deal(file: FileObj): void { + if (this.type === 'deleteAll') { + file.realName = ""; + if (!this.ignorePostfix) { + file.expandName = ""; + } + } else { + let str = file.realName + (this.ignorePostfix ? "" : file.expandName); + let startIndex = this.start.calIndex(str); + let endIndex = this.end.calIndex(str); + if (startIndex < 0 || endIndex < 0) { + return; + } + str = str.substring(0, startIndex) + str.substring(endIndex + 1); + if (this.ignorePostfix) { + file.realName = str; + } else { + file.expandName = path.extname(str); + if (file.expandName.length > 0) { + file.realName = str.substring(0, str.lastIndexOf(".")); + } else { + file.realName = str; + } + } + } + + file.name = file.realName + file.expandName; + } + +} + +class DeleteRuleItem { + /** + * location:位置,text:文本,end:直到末尾 + */ + type: string; + /** + * 对应的值 + */ + value: string; + + constructor(data: any) { + this.type = data.type; + this.value = data.value; + } + + /** + * 计算位置 + */ + calIndex(str: string): number { + if (this.type === 'location') { + return parseInt(this.value) - 1; + } else if (this.type === 'text') { + return str.indexOf(this.value); + } else if (this.type === 'end') { + return str.length - 1; + } + return -1; + } +} diff --git a/openRenamerBackend/vo/rules/InsertRule.ts b/openRenamerBackend/vo/rules/InsertRule.ts index 9f04d58..f6670e6 100644 --- a/openRenamerBackend/vo/rules/InsertRule.ts +++ b/openRenamerBackend/vo/rules/InsertRule.ts @@ -1,66 +1,66 @@ -import RuleInterface from "./RuleInterface"; -import FileObj from "../FileObj"; -import path from 'path'; - -export default class InsertRule implements RuleInterface { - - /** - * 插入内容 - */ - insertContent: string; - /** - * 操作类别,front:前缀,backend:后缀,at:位置,replace:替换当前文件名 - */ - type: string; - /** - * 当type为at,时的位置,从1开始 - */ - atInput: number; - /** - * 当type为at,时的方向,true:从右到左,false:从左到右 - */ - atIsRightToleft: boolean; - /** - * 忽略拓展名,true:忽略,false:不忽略 - */ - ignorePostfix: boolean; - - constructor(data: any) { - this.insertContent = data.insertContent; - this.type = data.type; - this.atInput = data.atInput; - this.atIsRightToleft = data.atIsRightToleft; - this.ignorePostfix = data.ignorePostfix; - } - - - deal(file: FileObj): void { - let str = this.ignorePostfix ? file.realName : file.name; - switch (this.type) { - case "front": - str = this.insertContent + str; - break; - case "backend": - str = str + this.insertContent; - break; - case "at": - let index = this.atIsRightToleft ? str.length - this.atInput + 1 : this.atInput - 1; - str = str.substring(0, index) + this.insertContent + str.substring(index); - break; - case "replace": - str = this.insertContent; - break; - } - if (this.ignorePostfix) { - file.realName = str; - } else { - file.expandName = path.extname(str); - if (file.expandName.length > 0) { - file.realName = str.substring(0, str.lastIndexOf(".")); - } else { - file.realName = str; - } - } - file.name = file.realName + file.expandName; - } +import RuleInterface from "./RuleInterface"; +import FileObj from "../FileObj"; +import path from 'path'; + +export default class InsertRule implements RuleInterface { + + /** + * 插入内容 + */ + insertContent: string; + /** + * 操作类别,front:前缀,backend:后缀,at:位置,replace:替换当前文件名 + */ + type: string; + /** + * 当type为at,时的位置,从1开始 + */ + atInput: number; + /** + * 当type为at,时的方向,true:从右到左,false:从左到右 + */ + atIsRightToleft: boolean; + /** + * 忽略拓展名,true:忽略,false:不忽略 + */ + ignorePostfix: boolean; + + constructor(data: any) { + this.insertContent = data.insertContent; + this.type = data.type; + this.atInput = data.atInput; + this.atIsRightToleft = data.atIsRightToleft; + this.ignorePostfix = data.ignorePostfix; + } + + + deal(file: FileObj): void { + let str = this.ignorePostfix ? file.realName : file.name; + switch (this.type) { + case "front": + str = this.insertContent + str; + break; + case "backend": + str = str + this.insertContent; + break; + case "at": + let index = this.atIsRightToleft ? str.length - this.atInput + 1 : this.atInput - 1; + str = str.substring(0, index) + this.insertContent + str.substring(index); + break; + case "replace": + str = this.insertContent; + break; + } + if (this.ignorePostfix) { + file.realName = str; + } else { + file.expandName = path.extname(str); + if (file.expandName.length > 0) { + file.realName = str.substring(0, str.lastIndexOf(".")); + } else { + file.realName = str; + } + } + file.name = file.realName + file.expandName; + } } \ No newline at end of file diff --git a/openRenamerBackend/vo/rules/RuleInterface.ts b/openRenamerBackend/vo/rules/RuleInterface.ts index 84a6b0f..4d4a3f8 100644 --- a/openRenamerBackend/vo/rules/RuleInterface.ts +++ b/openRenamerBackend/vo/rules/RuleInterface.ts @@ -1,6 +1,6 @@ -import FileObj from "../FileObj"; - -export default interface RuleInterface { - - deal(file: FileObj): void; +import FileObj from "../FileObj"; + +export default interface RuleInterface { + + deal(file: FileObj): void; } \ No newline at end of file diff --git a/openRenamerBackend/vo/rules/SerializationRule.ts b/openRenamerBackend/vo/rules/SerializationRule.ts index 1df7114..3b5b569 100644 --- a/openRenamerBackend/vo/rules/SerializationRule.ts +++ b/openRenamerBackend/vo/rules/SerializationRule.ts @@ -1,80 +1,80 @@ -import RuleInterface from "./RuleInterface"; -import FileObj from "../FileObj"; -import path from 'path'; - -export default class InsertRule implements RuleInterface { - /** - * 开始位置 - */ - start: number; - /** - * 记录当前的值是多少 - */ - currentIndex: number; - /** - * 增量 - */ - increment: number; - /** - * 是否填充0 - */ - addZero: boolean; - /** - * 填充后长度 - */ - numLength: number; - /** - * 插入位置,front:前缀,backend:后缀,at:位置 - */ - insertType: string; - /** - * 插入的位置 - */ - insertValue: number; - /** - * 忽略拓展名 - */ - ignorePostfix: boolean; - - constructor(data: any) { - this.start = data.start; - this.currentIndex = data.start; - this.increment = data.increment; - this.addZero = data.addZero; - this.numLength = data.numLength; - this.insertType = data.insertType; - this.insertValue = data.insertValue; - this.ignorePostfix = data.ignorePostfix; - } - - deal(file: FileObj): void { - let length = this.currentIndex.toString().length; - let numStr = (this.addZero && this.numLength > length ? "0".repeat(this.numLength - length) : "") + this.currentIndex; - let str = this.ignorePostfix ? file.realName : file.name; - switch (this.insertType) { - case "front": - str = numStr + str; - break; - case "backend": - str = str + numStr; - break; - case "at": - str = str.substring(0, this.insertValue - 1) + numStr + str.substring(this.insertValue - 1); - break; - } - this.currentIndex += this.increment; - - if (this.ignorePostfix) { - file.realName = str; - } else { - file.expandName = path.extname(str); - if (file.expandName.length > 0) { - file.realName = str.substring(0, str.lastIndexOf(".")); - } else { - file.realName = str; - } - } - - file.name = file.realName + file.expandName; - } +import RuleInterface from "./RuleInterface"; +import FileObj from "../FileObj"; +import path from 'path'; + +export default class InsertRule implements RuleInterface { + /** + * 开始位置 + */ + start: number; + /** + * 记录当前的值是多少 + */ + currentIndex: number; + /** + * 增量 + */ + increment: number; + /** + * 是否填充0 + */ + addZero: boolean; + /** + * 填充后长度 + */ + numLength: number; + /** + * 插入位置,front:前缀,backend:后缀,at:位置 + */ + insertType: string; + /** + * 插入的位置 + */ + insertValue: number; + /** + * 忽略拓展名 + */ + ignorePostfix: boolean; + + constructor(data: any) { + this.start = data.start; + this.currentIndex = data.start; + this.increment = data.increment; + this.addZero = data.addZero; + this.numLength = data.numLength; + this.insertType = data.insertType; + this.insertValue = data.insertValue; + this.ignorePostfix = data.ignorePostfix; + } + + deal(file: FileObj): void { + let length = this.currentIndex.toString().length; + let numStr = (this.addZero && this.numLength > length ? "0".repeat(this.numLength - length) : "") + this.currentIndex; + let str = this.ignorePostfix ? file.realName : file.name; + switch (this.insertType) { + case "front": + str = numStr + str; + break; + case "backend": + str = str + numStr; + break; + case "at": + str = str.substring(0, this.insertValue - 1) + numStr + str.substring(this.insertValue - 1); + break; + } + this.currentIndex += this.increment; + + if (this.ignorePostfix) { + file.realName = str; + } else { + file.expandName = path.extname(str); + if (file.expandName.length > 0) { + file.realName = str.substring(0, str.lastIndexOf(".")); + } else { + file.realName = str; + } + } + + file.name = file.realName + file.expandName; + } } \ No newline at end of file