feat: Docker 独立部署版 v1.1.0 + OCR 图片文字搜索
上游 ZangXincz/FlyPic 已归档停更、飞牛应用商店下架,本提交使其脱离 飞牛应用框架,以普通 Docker 容器继续运行。 - ✨ Docker 独立部署:多阶段 Dockerfile + docker-compose.yml,一键构建 - ✨ OCR 图片内文字搜索:扫描时 tesseract(chi_sim+eng) 提取文字入 ocr_text 列, 搜索同时匹配文件名与图片内容 - 🐛 修复 LibraryService 缺少 logger 引用导致添加素材库 500 - 🐛 移除写权限校验,支持只读挂载素材目录 - 📝 容器内路径与宿主机一致(中文路径直接可用) - 🎨 保持原交互:单击选中、Ctrl/Shift 多选、双击放大 - 🔒 config/(JWT 密钥)加入 gitignore
This commit is contained in:
1 parent
7359937892
commit
d96986ec79
11 files changed
+254
-118
No files matched your search
+15
-5
@@ -90,6 +90,13 @@ class LibraryDatabase {
|
||||
// 列已存在,忽略错误
|
||||
}
|
||||
|
||||
// 添加 ocr_text 列(如果不存在)
|
||||
try {
|
||||
this.db.exec(`ALTER TABLE images ADD COLUMN ocr_text TEXT DEFAULT ''`);
|
||||
} catch (e) {
|
||||
// 列已存在,忽略错误
|
||||
}
|
||||
|
||||
// Folders table
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS folders (
|
||||
@@ -115,6 +122,8 @@ class LibraryDatabase {
|
||||
-- 评分和收藏索引:优化筛选查询
|
||||
CREATE INDEX IF NOT EXISTS idx_rating ON images(rating DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_favorite ON images(favorite DESC);
|
||||
-- OCR 文本索引:优化关键词搜索
|
||||
CREATE INDEX IF NOT EXISTS idx_ocr_text ON images(ocr_text);
|
||||
`);
|
||||
|
||||
// Metadata table for tracking database modifications
|
||||
@@ -168,8 +177,8 @@ class LibraryDatabase {
|
||||
insertImage(imageData) {
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT OR REPLACE INTO images
|
||||
(path, filename, folder, size, width, height, format, file_type, created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(path, filename, folder, size, width, height, format, file_type, created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at, ocr_text)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
const result = stmt.run(
|
||||
imageData.path,
|
||||
@@ -185,7 +194,8 @@ class LibraryDatabase {
|
||||
imageData.file_hash,
|
||||
imageData.thumbnail_path,
|
||||
imageData.thumbnail_size,
|
||||
Date.now()
|
||||
Date.now(),
|
||||
imageData.ocr_text || ''
|
||||
);
|
||||
// 更新数据库修改时间
|
||||
this.updateLastModified();
|
||||
@@ -219,8 +229,8 @@ class LibraryDatabase {
|
||||
if (keywords && keywords.trim()) {
|
||||
const terms = keywords.trim().split(/\s+/);
|
||||
terms.forEach(term => {
|
||||
baseQuery += ' AND filename LIKE ?';
|
||||
params.push(`%${term}%`);
|
||||
baseQuery += ' AND (filename LIKE ? OR ocr_text LIKE ?)';
|
||||
params.push(`%${term}%`, `%${term}%`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -65,8 +65,8 @@ class ImageModel extends BaseModel {
|
||||
const query = `
|
||||
INSERT OR REPLACE INTO images
|
||||
(path, filename, folder, size, width, height, format, file_type,
|
||||
created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at, ocr_text)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
return this.execute(query, [
|
||||
@@ -83,7 +83,8 @@ class ImageModel extends BaseModel {
|
||||
data.fileHash,
|
||||
data.thumbnailPath,
|
||||
data.thumbnailSize,
|
||||
Date.now()
|
||||
Date.now(),
|
||||
data.ocrText || ''
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -94,8 +95,8 @@ class ImageModel extends BaseModel {
|
||||
const query = `
|
||||
INSERT OR REPLACE INTO images
|
||||
(path, filename, folder, size, width, height, format, file_type,
|
||||
created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
created_at, modified_at, file_hash, thumbnail_path, thumbnail_size, indexed_at, ocr_text)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
return this.transaction(() => {
|
||||
@@ -105,7 +106,7 @@ class ImageModel extends BaseModel {
|
||||
data.path, data.filename, data.folder, data.size,
|
||||
data.width, data.height, data.format, data.fileType || 'image',
|
||||
data.createdAt, data.modifiedAt, data.fileHash,
|
||||
data.thumbnailPath, data.thumbnailSize, Date.now()
|
||||
data.thumbnailPath, data.thumbnailSize, Date.now(), data.ocrText || ''
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -168,8 +169,8 @@ class ImageModel extends BaseModel {
|
||||
if (filters.keywords) {
|
||||
const terms = filters.keywords.trim().split(/\s+/);
|
||||
terms.forEach(term => {
|
||||
query += ' AND filename LIKE ?';
|
||||
params.push(`%${term}%`);
|
||||
query += ' AND (filename LIKE ? OR ocr_text LIKE ?)';
|
||||
params.push(`%${term}%`, `%${term}%`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* 素材库服务层
|
||||
* 封装素材库相关的业务逻辑
|
||||
*/
|
||||
const logger = require('../utils/logger');
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
@@ -50,21 +51,8 @@ class LibraryService {
|
||||
|
||||
// 检查文件夹访问权限
|
||||
try {
|
||||
// 尝试读取目录
|
||||
// 只验证读取权限,不要求写入(素材库可只读挂载)
|
||||
fs.readdirSync(normalizedPath);
|
||||
|
||||
// 尝试在目录中创建测试文件(检查写权限)
|
||||
const testFile = path.join(normalizedPath, '.flypic-test');
|
||||
try {
|
||||
fs.writeFileSync(testFile, 'test');
|
||||
fs.unlinkSync(testFile);
|
||||
} catch (writeError) {
|
||||
logger.warn(`无写入权限: ${normalizedPath}`);
|
||||
throw new ValidationError(
|
||||
'无法访问该文件夹。请在飞牛 fnOS 的"数据共享"中将此文件夹添加到 FlyPic 应用的访问权限。',
|
||||
'permission'
|
||||
);
|
||||
}
|
||||
} catch (readError) {
|
||||
if (readError.code === 'EACCES' || readError.code === 'EPERM') {
|
||||
logger.warn(`无访问权限: ${normalizedPath}`);
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* OCR 文字识别模块
|
||||
* 直接调用系统 tesseract 命令行,无需 npm 依赖
|
||||
* Docker 镜像已预装 tesseract-ocr + chi_sim + eng
|
||||
*/
|
||||
|
||||
const { execFile } = require('child_process');
|
||||
const { promisify } = require('util');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const logger = require('../src/utils/logger');
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
// Tesseract 路径
|
||||
const TESSERACT_BIN = process.env.TESSERACT_BIN || 'tesseract';
|
||||
|
||||
/**
|
||||
* 对单张图片进行 OCR 文字识别
|
||||
* @param {string} imagePath - 图片绝对路径
|
||||
* @returns {Promise<string>} 识别出的文字(清理后的纯文本)
|
||||
*/
|
||||
async function extractText(imagePath) {
|
||||
try {
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(imagePath)) return '';
|
||||
|
||||
// 使用临时文件输出(tesseract 不支持直接输出到 stdout 处理图片)
|
||||
const tmpFile = `/tmp/ocr_${Date.now()}_${Math.random().toString(36).slice(2)}.txt`;
|
||||
|
||||
try {
|
||||
await execFileAsync(TESSERACT_BIN, [
|
||||
imagePath,
|
||||
tmpFile.replace('.txt', ''), // tesseract 自动加 .txt 后缀
|
||||
'-l', 'chi_sim+eng', // 中英文
|
||||
'--psm', '3', // 自动页面分割
|
||||
'--oem', '3' // 默认 OCR 引擎
|
||||
], { timeout: 15000 }); // 15秒超时
|
||||
|
||||
// 读取结果
|
||||
const resultPath = tmpFile.replace('.txt', '.txt');
|
||||
if (fs.existsSync(resultPath)) {
|
||||
const text = fs.readFileSync(resultPath, 'utf-8');
|
||||
fs.unlinkSync(resultPath);
|
||||
return text.replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
} catch (e) {
|
||||
// OCR 失败静默处理
|
||||
} finally {
|
||||
// 清理临时文件
|
||||
try { fs.unlinkSync(tmpFile); } catch(e) {}
|
||||
try { fs.unlinkSync(tmpFile.replace('.txt', '.txt')); } catch(e) {}
|
||||
}
|
||||
|
||||
return '';
|
||||
} catch (error) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { extractText };
|
||||
@@ -10,6 +10,7 @@ const {
|
||||
clearSharpCache
|
||||
} = require('./thumbnail');
|
||||
const scanManager = require('./scanManager');
|
||||
const { extractText } = require('./ocr');
|
||||
const { constants } = require('../src/config');
|
||||
const logger = require('../src/utils/logger');
|
||||
|
||||
@@ -304,6 +305,11 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
const actualWidth = thumbnails.width || metadata.width;
|
||||
const actualHeight = thumbnails.height || metadata.height;
|
||||
|
||||
// OCR 文字识别(可选:tesseract 失败时返回空字符串,不影响主流程)
|
||||
stepStart = Date.now();
|
||||
const ocrText = await extractText(imagePath);
|
||||
stepTimes.ocr = Date.now() - stepStart;
|
||||
|
||||
const imageData = {
|
||||
path: relativePath.replace(/\\/g, '/'),
|
||||
filename: filename,
|
||||
@@ -317,7 +323,8 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
modified_at: Math.floor(metadata.modified_at),
|
||||
file_hash: currentHash,
|
||||
thumbnail_path: thumbnails.thumbnail_path,
|
||||
thumbnail_size: thumbnails.thumbnail_size
|
||||
thumbnail_size: thumbnails.thumbnail_size,
|
||||
ocr_text: ocrText
|
||||
};
|
||||
|
||||
if (dryRun) {
|
||||
@@ -336,7 +343,8 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
`总计${totalTime}ms`,
|
||||
`哈希${stepTimes.hashCalc}ms`,
|
||||
`元数据${stepTimes.metadata}ms`,
|
||||
`缩略图${stepTimes.thumbnail}ms`
|
||||
`缩略图${stepTimes.thumbnail}ms`,
|
||||
`OCR${stepTimes.ocr}ms`
|
||||
];
|
||||
|
||||
// 只有当总耗时超过 500ms 时才输出警告
|
||||
|
||||
Reference in new issue
Block a user