feat(v1.4.8): 搜索结果批量重命名 + 关键词替换模式

- 搜索结果工具条新增「批量重命名搜索结果…」入口(scope=search)
- 后端 rename-preview/start 支持 scope=search:与列表同款模糊匹配(关键词空格分词 AND,filename/OCR LIKE),可选 folder 递归限定
- 新增关键词替换模式:find 关键词 + replace 替换成(留空=删除关键词),不含关键词的文件自动跳过并报告
- 弹窗模式选择改 2x2 网格,搜索范围显示「搜索:关键词」徽标
- 关键词模式隐藏「包含子目录」(搜索范围已含子目录)
This commit is contained in:
jinghui committed 2026-11-18 22:57:46 +08:00
1 parent c59791f994
commit 6e086d629d
8 files changed
+234 -68

No files matched your search

+92 -25
View File
@@ -195,6 +195,15 @@ async function callInpaint(imageBuffer, maskBuffer) {
/** 收集文件夹内图片(含子文件夹) */
function collectImages(req, libraryId, folder) {
return collectRenameTargets(req, { libraryId, folder, scope: 'folder' });
}
/**
* 收集重命名目标
* scope=folder(默认):文件夹递归(沿用 collectImages 语义)
* scope=search:按关键词模糊匹配整个库(filename/ocr_text LIKE,跨文件夹)
*/
function collectRenameTargets(req, { libraryId, folder, scope, keywords }) {
const configManager = req.app.get('configManager');
const dbPool = req.app.get('dbPool');
if (!configManager || !dbPool) throw new ValidationError('服务未初始化');
@@ -206,6 +215,13 @@ function collectImages(req, libraryId, folder) {
const db = dbPool.acquire(library.path);
try {
const model = new ImageModel(db.db);
if (scope === 'search') {
const kw = String(keywords || '').trim();
if (!kw) throw new ValidationError('搜索关键词不能为空');
// folder 与搜索列表同款过滤(含子目录):选中文件夹时范围一致,未选中则全库
const { images } = model.search({ keywords: kw, folder: folder || undefined }, null);
return { library, images };
}
const { images } = model.search({ folder }, null);
return { library, images };
} finally {
@@ -320,11 +336,14 @@ const ILLEGAL_NAME_RE = /[\\/:*?"<>|]/;
/**
* 计算重命名计划(preview 与执行共用,保证一致)
* @param {object} opts { images, folder, mode: 'prefix'|'suffix'|'replace', text, includeSubdirs }
* @param {object} opts { images, folder, mode: 'prefix'|'suffix'|'replace'|'keyword', text, find?, includeSubdirs }
* keyword 模式:把文件名中的 find 全部替换为 text(text 可为空 = 删除关键词),
* 不含 find 的文件标记 skipReason='不含关键词'
* @returns {Array<{img, newFilename, newPath, absNew, skipReason?}>}
*/
function computeRenamePlan({ images, folder, mode, text, includeSubdirs }) {
function computeRenamePlan({ images, folder, mode, text, find, includeSubdirs }) {
const cleanText = String(text || '').trim();
const cleanFind = String(find || '').trim();
let list = images;
if (!includeSubdirs) {
// 只重命名该文件夹直接子项,排除子目录内文件
@@ -357,6 +376,9 @@ function computeRenamePlan({ images, folder, mode, text, includeSubdirs }) {
newBase = group.length > 1
? `${cleanText}-${String(i + 1).padStart(width, '0')}`
: cleanText;
} else if (mode === 'keyword') {
// 关键词替换:文件名中所有出现的 find 换成 text;不含 find 跳过
newBase = base.includes(cleanFind) ? base.split(cleanFind).join(cleanText) : base;
} else newBase = base;
const newFilename = `${newBase}${ext}`;
@@ -364,7 +386,9 @@ function computeRenamePlan({ images, folder, mode, text, includeSubdirs }) {
const absNew = newPath; // 相对库根,执行时再 join
const entry = { img, newFilename, newPath, skipReason: null };
if (newFilename === img.filename) {
if (mode === 'keyword' && !base.includes(cleanFind)) {
entry.skipReason = '不含关键词';
} else if (newFilename === img.filename) {
entry.skipReason = '文件名未变化';
} else if (used.has(newPath.toLowerCase())) {
entry.skipReason = '目标文件名冲突(本次内)';
@@ -377,15 +401,23 @@ function computeRenamePlan({ images, folder, mode, text, includeSubdirs }) {
}
async function runRename(req, task) {
const { library, images } = collectImages(req, task.payload.libraryId, task.payload.folder);
const p = task.payload;
const { library, images } = collectRenameTargets(req, {
libraryId: p.libraryId,
folder: p.folder,
scope: p.scope,
keywords: p.keywords
});
const opts = task.payload.options;
const isSearch = p.scope === 'search';
const plan = computeRenamePlan({
images,
folder: task.payload.folder,
mode: opts.mode,
text: opts.text,
includeSubdirs: opts.includeSubdirs !== false
find: opts.find,
includeSubdirs: isSearch ? true : opts.includeSubdirs !== false
});
task.total = plan.length;
@@ -439,26 +471,40 @@ async function runRename(req, task) {
/**
* POST /api/batch/rename-preview
* body: { libraryId, folder, options: { mode, text, includeSubdirs } }
* body: { libraryId, folder?, scope?: 'folder'|'search', keywords?, options: { mode, text, find?, includeSubdirs } }
* scope=search 时按 keywords 模糊匹配全库(跨文件夹),folder 可不传
* 返回重命名预览(不执行)
*/
router.post(
'/rename-preview',
asyncHandler(async (req, res) => {
const { libraryId, folder, options } = req.body || {};
if (!libraryId || !folder) throw new ValidationError('缺少 libraryId 或 folder');
const { libraryId, folder, scope, options } = req.body || {};
if (!libraryId) throw new ValidationError('缺少 libraryId');
const isSearch = scope === 'search';
if (!isSearch && !folder) throw new ValidationError('缺少 libraryId 或 folder');
const mode = options && options.mode;
if (!['prefix', 'suffix', 'replace'].includes(mode)) {
throw new ValidationError('mode 必须是 prefix / suffix / replace');
if (!['prefix', 'suffix', 'replace', 'keyword'].includes(mode)) {
throw new ValidationError('mode 必须是 prefix / suffix / replace / keyword');
}
const text = String((options && options.text) || '').trim();
if (!text) throw new ValidationError('重命名文本不能为空');
if (ILLEGAL_NAME_RE.test(text)) throw new ValidationError('文本包含文件名非法字符 \\ / : * ? " < > |');
const find = String((options && options.find) || '').trim();
if (mode === 'keyword') {
if (!find) throw new ValidationError('请填写要查找的关键词');
if (ILLEGAL_NAME_RE.test(find)) throw new ValidationError('关键词包含文件名非法字符 \\ / : * ? " < > |');
if (ILLEGAL_NAME_RE.test(text)) throw new ValidationError('替换文本包含文件名非法字符 \\ / : * ? " < > |');
} else {
if (!text) throw new ValidationError('重命名文本不能为空');
if (ILLEGAL_NAME_RE.test(text)) throw new ValidationError('文本包含文件名非法字符 \\ / : * ? " < > |');
}
const { images } = collectImages(req, libraryId, folder);
const targets = collectRenameTargets(req, {
libraryId, folder, scope, keywords: options && options.keywords
});
const plan = computeRenamePlan({
images, folder, mode, text,
includeSubdirs: options.includeSubdirs !== false
images: targets.images,
folder: isSearch ? null : folder,
mode, text, find,
includeSubdirs: isSearch ? true : options.includeSubdirs !== false
});
const effective = plan.filter((p) => !p.skipReason);
res.json({
@@ -479,20 +525,28 @@ router.post(
router.post(
'/start',
asyncHandler(async (req, res) => {
const { libraryId, folder, type, output, options } = req.body || {};
if (!libraryId || !folder) throw new ValidationError('缺少 libraryId 或 folder');
const { libraryId, folder, type, output, options, scope } = req.body || {};
if (!libraryId) throw new ValidationError('缺少 libraryId');
const isSearch = type === 'rename' && scope === 'search';
if (!folder && !isSearch) throw new ValidationError('缺少 libraryId 或 folder');
if (!['add', 'remove', 'rename'].includes(type)) throw new ValidationError('type 必须是 add / remove / rename');
if (type === 'add') {
if (!options || !String(options.text || '').trim()) throw new ValidationError('水印文字不能为空');
} else if (type === 'rename') {
const mode = options && options.mode;
if (!['prefix', 'suffix', 'replace'].includes(mode)) {
throw new ValidationError('mode 必须是 prefix / suffix / replace');
if (!['prefix', 'suffix', 'replace', 'keyword'].includes(mode)) {
throw new ValidationError('mode 必须是 prefix / suffix / replace / keyword');
}
if (!String(options.text || '').trim()) throw new ValidationError('重命名文本不能为空');
if (ILLEGAL_NAME_RE.test(String(options.text).trim())) {
throw new ValidationError('文本包含文件名非法字符 \\ / : * ? " < > |');
const text = String((options && options.text) || '').trim();
const find = String((options && options.find) || '').trim();
if (mode === 'keyword') {
if (!find) throw new ValidationError('请填写要查找的关键词');
if (ILLEGAL_NAME_RE.test(find)) throw new ValidationError('关键词包含文件名非法字符 \\ / : * ? " < > |');
if (ILLEGAL_NAME_RE.test(text)) throw new ValidationError('替换文本包含文件名非法字符 \\ / : * ? " < > |');
} else {
if (!text) throw new ValidationError('重命名文本不能为空');
if (ILLEGAL_NAME_RE.test(text)) throw new ValidationError('文本包含文件名非法字符 \\ / : * ? " < > |');
}
} else {
const r = options && options.region;
@@ -502,10 +556,23 @@ router.post(
if (r.w <= 0 || r.h <= 0) throw new ValidationError('水印区域宽高必须大于 0');
}
const { images } = collectImages(req, libraryId, folder);
if (images.length === 0) throw new ValidationError(type === 'rename' ? '该文件夹下没有文件' : '该文件夹下没有图片');
const { images } = collectRenameTargets(req, {
libraryId, folder, scope, keywords: options && options.keywords
});
if (images.length === 0) {
throw new ValidationError(
scope === 'search' ? '搜索没有匹配到文件' : (type === 'rename' ? '该文件夹下没有文件' : '该文件夹下没有图片')
);
}
const task = newTask(type, { libraryId, folder, output, options });
const task = newTask(type, {
libraryId,
folder: folder || null,
scope: scope || 'folder',
keywords: scope === 'search' ? String(options?.keywords || '').trim() : null,
output,
options
});
task.total = images.length;
logger.info(`▶️ 批量${type === 'add' ? '加水印' : type === 'remove' ? '去水印' : '重命名'}启动: ${folder} (${images.length} 张)`);
+1 -1
View File
@@ -16,7 +16,7 @@ const { getSettings } = require('./settings');
const HUB_URL = (process.env.HUB_URL || 'https://auth.88531.cn/api').replace(/\/+$/, '');
const HUB_PROJECT_ID = process.env.HUB_PROJECT_ID || '';
const HUB_API_KEY = process.env.HUB_API_KEY || '';
const SITE_VERSION = process.env.SITE_VERSION || '1.4.7';
const SITE_VERSION = process.env.SITE_VERSION || '1.4.8';
const TIMEOUT_MS = 8000;
const state = {