1 parent
046482fccd
commit
aa4e3388fc
3 files changed
+77
-6
No files matched your search
@@ -0,0 +1,73 @@
|
|||||||
|
import { existsSync, readdirSync, statSync } from 'fs'
|
||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
|
const accountDirCache = new Map<string, string>()
|
||||||
|
|
||||||
|
const cleanAccountDirName = (dirName: string): string => {
|
||||||
|
const trimmed = dirName.trim()
|
||||||
|
if (!trimmed) return trimmed
|
||||||
|
|
||||||
|
if (trimmed.toLowerCase().startsWith('wxid_')) {
|
||||||
|
const match = trimmed.match(/^(wxid_[^_]+)/i)
|
||||||
|
if (match) return match[1]
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
const suffixMatch = trimmed.match(/^(.+)_([a-zA-Z0-9]{4})$/)
|
||||||
|
if (suffixMatch) return suffixMatch[1]
|
||||||
|
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
const isDirectory = (path: string): boolean => {
|
||||||
|
try {
|
||||||
|
return statSync(path).isDirectory()
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const resolveAccountDir = (dbPath?: string, wxid?: string): string | null => {
|
||||||
|
if (!dbPath || !wxid) return null
|
||||||
|
|
||||||
|
const cleanedWxid = cleanAccountDirName(wxid)
|
||||||
|
const normalized = dbPath.replace(/[\\/]+$/, '')
|
||||||
|
const cacheKey = `${normalized}|${cleanedWxid.toLowerCase()}`
|
||||||
|
|
||||||
|
const cached = accountDirCache.get(cacheKey)
|
||||||
|
if (cached && existsSync(cached)) return cached
|
||||||
|
if (cached && !existsSync(cached)) {
|
||||||
|
accountDirCache.delete(cacheKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
const lowerWxid = cleanedWxid.toLowerCase()
|
||||||
|
if (!lowerWxid.startsWith('wxid_')) {
|
||||||
|
const direct = join(normalized, cleanedWxid)
|
||||||
|
if (existsSync(direct) && isDirectory(direct)) {
|
||||||
|
accountDirCache.set(cacheKey, direct)
|
||||||
|
return direct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const entries = readdirSync(normalized)
|
||||||
|
for (const entry of entries) {
|
||||||
|
const entryPath = join(normalized, entry)
|
||||||
|
if (!isDirectory(entryPath)) continue
|
||||||
|
|
||||||
|
const lowerEntry = entry.toLowerCase()
|
||||||
|
const isExactMatch = lowerEntry === lowerWxid
|
||||||
|
const isSuffixMatch = lowerEntry.startsWith(`${lowerWxid}_`)
|
||||||
|
const shouldMatch = lowerWxid.startsWith('wxid_')
|
||||||
|
? isSuffixMatch
|
||||||
|
: (isExactMatch || isSuffixMatch)
|
||||||
|
|
||||||
|
if (shouldMatch) {
|
||||||
|
accountDirCache.set(cacheKey, entryPath)
|
||||||
|
return entryPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch { }
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { parentPort } from 'worker_threads'
|
import { parentPort } from 'worker_threads'
|
||||||
import { wcdbService } from './wcdbService'
|
import { wcdbService } from './wcdbService'
|
||||||
import { ConfigService } from './config'
|
import { resolveAccountDir } from './accountDirResolver'
|
||||||
|
|
||||||
export interface TopContact {
|
export interface TopContact {
|
||||||
username: string
|
username: string
|
||||||
@@ -159,8 +159,7 @@ class AnnualReportService {
|
|||||||
if (!dbPath) return { success: false, error: '未配置数据库路径' }
|
if (!dbPath) return { success: false, error: '未配置数据库路径' }
|
||||||
if (!decryptKey) return { success: false, error: '未配置解密密钥' }
|
if (!decryptKey) return { success: false, error: '未配置解密密钥' }
|
||||||
|
|
||||||
const configService = ConfigService.getInstance()
|
const accountDir = resolveAccountDir(dbPath, wxid)
|
||||||
const accountDir = configService.getAccountDir(dbPath, wxid)
|
|
||||||
if (!accountDir) return { success: false, error: '未找到账号目录' }
|
if (!accountDir) return { success: false, error: '未找到账号目录' }
|
||||||
|
|
||||||
const ok = await wcdbService.open(accountDir, decryptKey)
|
const ok = await wcdbService.open(accountDir, decryptKey)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { parentPort } from 'worker_threads'
|
import { parentPort } from 'worker_threads'
|
||||||
import { wcdbService } from './wcdbService'
|
import { wcdbService } from './wcdbService'
|
||||||
import { ConfigService } from './config'
|
import { resolveAccountDir } from './accountDirResolver'
|
||||||
|
|
||||||
|
|
||||||
export interface DualReportMessage {
|
export interface DualReportMessage {
|
||||||
@@ -110,8 +110,7 @@ class DualReportService {
|
|||||||
if (!dbPath) return { success: false, error: '未配置数据库路径' }
|
if (!dbPath) return { success: false, error: '未配置数据库路径' }
|
||||||
if (!decryptKey) return { success: false, error: '未配置解密密钥' }
|
if (!decryptKey) return { success: false, error: '未配置解密密钥' }
|
||||||
|
|
||||||
const configService = ConfigService.getInstance()
|
const accountDir = resolveAccountDir(dbPath, wxid)
|
||||||
const accountDir = configService.getAccountDir(dbPath, wxid)
|
|
||||||
if (!accountDir) return { success: false, error: '无法找到账号目录' }
|
if (!accountDir) return { success: false, error: '无法找到账号目录' }
|
||||||
const ok = await wcdbService.open(accountDir, decryptKey)
|
const ok = await wcdbService.open(accountDir, decryptKey)
|
||||||
if (!ok) return { success: false, error: 'WCDB 打开失败' }
|
if (!ok) return { success: false, error: 'WCDB 打开失败' }
|
||||||
|
|||||||
Reference in new issue
Block a user