diff --git a/CHANGELOG.md b/CHANGELOG.md
index daa36d0..107f7ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,16 @@
# 更新日志
+## 2026-11-18 → v1.4.8
+
+**搜索结果批量改名 + 关键词替换**——搜索出一批文件后直接对命中结果批量改名,支持把文件名中的关键词替换掉:
+
+- ✨ **搜索结果入口**:任意搜索(Header 搜索框,模糊匹配文件名+OCR 文本)后,结果区顶部出现「批量重命名搜索结果…」按钮;打开弹窗头部徽标显示当前搜索词,范围与搜索列表完全一致(选中文件夹时含其子目录,未选中则全库)
+- ✨ **新重命名模式「关键词替换」**:找什么→替换成(文件名中所有出现处全部替换);**替换成留空 = 删除该关键词**;不含关键词的文件自动跳过并在结果中报告;预览实时显示替换前后对照
+- ✨ 搜索范围与列表同款 `LIKE` 模糊匹配(多关键词 AND、filename+ocr_text),后端 `POST /api/batch/rename-preview` 与 `/start` 新增 `scope='search'` + `keywords` 参数(folder 可不传,传了则按文件夹递归限定范围)
+- ✨ 弹窗模式改 2×2 布局(4 种模式);关键词模式下隐藏「包含子目录」开关(搜索范围本身就含子目录);文件夹右键入口的原有三种模式行为不变
+- 🛡️ 关键词与替换文本同样走文件名非法字符 `\ / : * ? " < > |` 前后端校验;预览与执行共用同一计划(命中数、跳过原因一致)
+- 端到端实测:preview/scope=search 预览命中数、keyword 执行改名+DB path 同步、搜索态按钮与弹窗渲染
+
## 2026-11-18 → v1.4.7
**文件类型自动识别(通用机制)**——原先统计卡、文件夹卡、多选条等多处硬编码「图片/N 张图片」,库里实际有图片/视频/文档三种类型(356 个文件被显示成「356 图片总数」)。现改为类型注册表驱动,**以后新增文件类型只需在注册表加一行,全站文案与统计自动识别**:
diff --git a/backend/src/routes/batch.js b/backend/src/routes/batch.js
index 422d0af..90d380c 100644
--- a/backend/src/routes/batch.js
+++ b/backend/src/routes/batch.js
@@ -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} 张)`);
diff --git a/backend/utils/hub.js b/backend/utils/hub.js
index 428a79e..9aad8eb 100644
--- a/backend/utils/hub.js
+++ b/backend/utils/hub.js
@@ -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 = {
diff --git a/docker-compose.yml b/docker-compose.yml
index 80405a3..57a1a31 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -28,7 +28,7 @@ services:
- HUB_API_KEY=${HUB_API_KEY:-}
- NEWAPI_KEY=${NEWAPI_KEY:-}
# 当前站点版本(与 frontend/package.json、CHANGELOG 保持一致,界面版本徽标来源)
- - SITE_VERSION=1.4.7
+ - SITE_VERSION=1.4.8
# 让容器经 host.docker.internal 访问宿主机的 AI 侧车
extra_hosts:
- "host.docker.internal:host-gateway"
diff --git a/frontend/src/components/BatchRenameModal.jsx b/frontend/src/components/BatchRenameModal.jsx
index 2332de6..0be6488 100644
--- a/frontend/src/components/BatchRenameModal.jsx
+++ b/frontend/src/components/BatchRenameModal.jsx
@@ -15,15 +15,17 @@ import { batchAPI } from '../api';
const MODES = [
{ key: 'prefix', label: '前面加', desc: '在原文件名前加文本', hint: '文本 + 原文件名 + 扩展名' },
{ key: 'suffix', label: '后面加', desc: '在原文件名后加文本', hint: '原文件名 + 文本 + 扩展名' },
- { key: 'replace', label: '全部改名', desc: '原文件名替换为文本', hint: '同目录多个文件自动编号:文本-001、文本-002…' }
+ { key: 'replace', label: '全部改名', desc: '原文件名替换为文本', hint: '同目录多个文件自动编号:文本-001、文本-002…' },
+ { key: 'keyword', label: '关键词替换', desc: '替换文件名中的关键词', hint: '把文件名中的「找什么」全部换成「替换成」,替换成留空 = 删除该关键词' }
];
const ILLEGAL_RE = /[\\/:*?"<>|]/;
-function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
+function BatchRenameModal({ isOpen, folder, libraryId, scope, keywords, onClose, onFinished }) {
const [phase, setPhase] = useState('config'); // config | running | done
const [mode, setMode] = useState('prefix');
const [text, setText] = useState('');
+ const [find, setFind] = useState('');
const [includeSubdirs, setIncludeSubdirs] = useState(true);
const [preview, setPreview] = useState(null); // { count, total, samples, skipped }
const [previewErr, setPreviewErr] = useState('');
@@ -48,6 +50,7 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
setPhase('config');
setMode('prefix');
setText('');
+ setFind('');
setIncludeSubdirs(true);
setPreview(null);
setPreviewErr('');
@@ -59,11 +62,11 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
}, [isOpen, stopPolling]);
// 实时预览(防抖 300ms)
+ const needInput = mode === 'keyword' ? find.trim() : text.trim();
useEffect(() => {
if (!isOpen || phase !== 'config') return;
if (previewTimerRef.current) clearTimeout(previewTimerRef.current);
- const t = text.trim();
- if (!t) {
+ if (!needInput) {
setPreview(null);
setPreviewErr('');
return;
@@ -71,8 +74,8 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
previewTimerRef.current = setTimeout(async () => {
try {
const resp = await batchAPI.renamePreview({
- libraryId, folder,
- options: { mode, text: t, includeSubdirs }
+ libraryId, folder, scope, keywords,
+ options: { mode, text: text.trim(), find: find.trim(), keywords: keywords || undefined, includeSubdirs }
});
setPreview(resp);
setPreviewErr('');
@@ -82,7 +85,7 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
}
}, 300);
return () => { if (previewTimerRef.current) clearTimeout(previewTimerRef.current); };
- }, [isOpen, phase, mode, text, includeSubdirs, libraryId, folder]);
+ }, [isOpen, phase, mode, text, find, includeSubdirs, libraryId, folder, scope, keywords, needInput]);
const startPolling = useCallback((id) => {
stopPolling();
@@ -102,19 +105,35 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {
const handleStart = async () => {
setError('');
const t = text.trim();
- if (!t) {
- setError('请输入重命名文本');
- return;
- }
- if (ILLEGAL_RE.test(t)) {
- setError('文本包含文件名非法字符 \\ / : * ? " < > |');
- return;
+ const f = find.trim();
+ if (mode === 'keyword') {
+ if (!f) {
+ setError('请填写要查找的关键词');
+ return;
+ }
+ if (ILLEGAL_RE.test(f)) {
+ setError('关键词包含文件名非法字符 \\ / : * ? " < > |');
+ return;
+ }
+ if (ILLEGAL_RE.test(t)) {
+ setError('替换文本包含文件名非法字符 \\ / : * ? " < > |');
+ return;
+ }
+ } else {
+ if (!t) {
+ setError('请输入重命名文本');
+ return;
+ }
+ if (ILLEGAL_RE.test(t)) {
+ setError('文本包含文件名非法字符 \\ / : * ? " < > |');
+ return;
+ }
}
setStarting(true);
try {
const resp = await batchAPI.start({
- libraryId, folder, type: 'rename',
- options: { mode, text: t, includeSubdirs }
+ libraryId, folder, scope, keywords, type: 'rename',
+ options: { mode, text: t, find: f, keywords: keywords || undefined, includeSubdirs }
});
setTaskId(resp.taskId);
setPhase('running');
@@ -161,7 +180,7 @@ function BatchRenameModal({ isOpen, folder, libraryId, onClose, onFinished }) {