Merge pull request #135 from JiQingzhe2004/main

优化
This commit is contained in:
Forrest
2026-01-29 19:06:12 +08:00
committed by GitHub
3 changed files with 130 additions and 41 deletions

View File

@@ -899,42 +899,71 @@ export class ImageDecryptService {
} }
private findCachedOutput(cacheKey: string, preferHd: boolean = false, sessionId?: string): string | null { private findCachedOutput(cacheKey: string, preferHd: boolean = false, sessionId?: string): string | null {
const root = this.getCacheRoot() const allRoots = this.getAllCacheRoots()
const normalizedKey = this.normalizeDatBase(cacheKey.toLowerCase()) const normalizedKey = this.normalizeDatBase(cacheKey.toLowerCase())
const extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'] const extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp']
if (sessionId) { // 遍历所有可能的缓存根路径
const sessionDir = join(root, this.sanitizeDirName(sessionId)) for (const root of allRoots) {
if (existsSync(sessionDir)) { // 策略1: 新目录结构 Images/{sessionId}/{YYYY-MM}/{file}_hd.jpg
try { if (sessionId) {
const sessionEntries = readdirSync(sessionDir) const sessionDir = join(root, this.sanitizeDirName(sessionId))
for (const entry of sessionEntries) { if (existsSync(sessionDir)) {
const timeDir = join(sessionDir, entry) try {
if (!this.isDirectory(timeDir)) continue const dateDirs = readdirSync(sessionDir, { withFileTypes: true })
const hit = this.findCachedOutputInDir(timeDir, normalizedKey, extensions, preferHd) .filter(d => d.isDirectory() && /^\d{4}-\d{2}$/.test(d.name))
if (hit) return hit .map(d => d.name)
} .sort()
} catch { .reverse() // 最新的日期优先
// ignore
for (const dateDir of dateDirs) {
const imageDir = join(sessionDir, dateDir)
const hit = this.findCachedOutputInDir(imageDir, normalizedKey, extensions, preferHd)
if (hit) return hit
}
} catch { }
} }
} }
}
// 新目录结构: Images/{normalizedKey}/{normalizedKey}_thumb.jpg 或 _hd.jpg // 策略2: 遍历所有 sessionId 目录查找(如果没有指定 sessionId
const imageDir = join(root, normalizedKey) try {
if (existsSync(imageDir)) { const sessionDirs = readdirSync(root, { withFileTypes: true })
const hit = this.findCachedOutputInDir(imageDir, normalizedKey, extensions, preferHd) .filter(d => d.isDirectory())
if (hit) return hit .map(d => d.name)
}
// 兼容旧的平铺结构 for (const session of sessionDirs) {
for (const ext of extensions) { const sessionDir = join(root, session)
const candidate = join(root, `${cacheKey}${ext}`) // 检查是否是日期目录结构
if (existsSync(candidate)) return candidate try {
} const subDirs = readdirSync(sessionDir, { withFileTypes: true })
for (const ext of extensions) { .filter(d => d.isDirectory() && /^\d{4}-\d{2}$/.test(d.name))
const candidate = join(root, `${cacheKey}_t${ext}`) .map(d => d.name)
if (existsSync(candidate)) return candidate
for (const dateDir of subDirs) {
const imageDir = join(sessionDir, dateDir)
const hit = this.findCachedOutputInDir(imageDir, normalizedKey, extensions, preferHd)
if (hit) return hit
}
} catch { }
}
} catch { }
// 策略3: 旧目录结构 Images/{normalizedKey}/{normalizedKey}_thumb.jpg
const oldImageDir = join(root, normalizedKey)
if (existsSync(oldImageDir)) {
const hit = this.findCachedOutputInDir(oldImageDir, normalizedKey, extensions, preferHd)
if (hit) return hit
}
// 策略4: 最旧的平铺结构 Images/{file}.jpg
for (const ext of extensions) {
const candidate = join(root, `${cacheKey}${ext}`)
if (existsSync(candidate)) return candidate
}
for (const ext of extensions) {
const candidate = join(root, `${cacheKey}_t${ext}`)
if (existsSync(candidate)) return candidate
}
} }
return null return null
@@ -1104,15 +1133,19 @@ export class ImageDecryptService {
if (this.cacheIndexed) return if (this.cacheIndexed) return
if (this.cacheIndexing) return this.cacheIndexing if (this.cacheIndexing) return this.cacheIndexing
this.cacheIndexing = new Promise((resolve) => { this.cacheIndexing = new Promise((resolve) => {
const root = this.getCacheRoot() // 扫描所有可能的缓存根目录
try { const allRoots = this.getAllCacheRoots()
this.indexCacheDir(root, 2, 0) this.logInfo('开始索引缓存', { roots: allRoots.length })
} catch {
this.cacheIndexed = true for (const root of allRoots) {
this.cacheIndexing = null try {
resolve() this.indexCacheDir(root, 3, 0) // 增加深度到3支持 sessionId/YYYY-MM 结构
return } catch (e) {
this.logError('索引目录失败', e, { root })
}
} }
this.logInfo('缓存索引完成', { entries: this.resolvedCache.size })
this.cacheIndexed = true this.cacheIndexed = true
this.cacheIndexing = null this.cacheIndexing = null
resolve() resolve()
@@ -1120,6 +1153,39 @@ export class ImageDecryptService {
return this.cacheIndexing return this.cacheIndexing
} }
/**
* 获取所有可能的缓存根路径(用于查找已缓存的图片)
* 包含当前路径、配置路径、旧版本路径
*/
private getAllCacheRoots(): string[] {
const roots: string[] = []
const configured = this.configService.get('cachePath')
const documentsPath = app.getPath('documents')
// 主要路径(当前使用的)
const mainRoot = this.getCacheRoot()
roots.push(mainRoot)
// 如果配置了自定义路径,也检查其下的 Images
if (configured) {
roots.push(join(configured, 'Images'))
roots.push(join(configured, 'images'))
}
// 默认路径
roots.push(join(documentsPath, 'WeFlow', 'Images'))
roots.push(join(documentsPath, 'WeFlow', 'images'))
// 兼容旧路径(如果有的话)
roots.push(join(documentsPath, 'WeFlowData', 'Images'))
// 去重并过滤存在的路径
const uniqueRoots = Array.from(new Set(roots))
const existingRoots = uniqueRoots.filter(r => existsSync(r))
return existingRoots
}
private indexCacheDir(root: string, maxDepth: number, depth: number): void { private indexCacheDir(root: string, maxDepth: number, depth: number): void {
let entries: string[] let entries: string[]
try { try {

View File

@@ -247,10 +247,33 @@ export class WcdbCore {
// InitProtection (Added for security) // InitProtection (Added for security)
try { try {
this.wcdbInitProtection = this.lib.func('bool InitProtection(const char* resourcePath)') this.wcdbInitProtection = this.lib.func('bool InitProtection(const char* resourcePath)')
const protectionOk = this.wcdbInitProtection(dllDir)
// 尝试多个可能的资源路径
const resourcePaths = [
dllDir, // DLL 所在目录
dirname(dllDir), // 上级目录
this.resourcesPath, // 配置的资源路径
join(process.cwd(), 'resources') // 开发环境
].filter(Boolean)
let protectionOk = false
for (const resPath of resourcePaths) {
try {
console.log(`[WCDB] 尝试 InitProtection: ${resPath}`)
protectionOk = this.wcdbInitProtection(resPath)
if (protectionOk) {
console.log(`[WCDB] InitProtection 成功: ${resPath}`)
break
}
} catch (e) {
console.warn(`[WCDB] InitProtection 失败 (${resPath}):`, e)
}
}
if (!protectionOk) { if (!protectionOk) {
console.error('Core security check failed') console.warn('[WCDB] Core security check failed - 继续运行但可能不稳定')
return false this.writeLog('InitProtection 失败,继续运行')
// 不返回 false允许继续运行
} }
} catch (e) { } catch (e) {
console.warn('InitProtection symbol not found:', e) console.warn('InitProtection symbol not found:', e)

2
package-lock.json generated
View File

@@ -6,7 +6,7 @@
"packages": { "packages": {
"": { "": {
"name": "weflow", "name": "weflow",
"version": "1.4.0", "version": "1.4.1",
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"better-sqlite3": "^12.5.0", "better-sqlite3": "^12.5.0",