mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
Merge branch 'develop' of https://github.com/Wxw-Gu/WechatExplorer into develop
This commit is contained in:
commit
62abd872ee
4 files changed
+230
-26
No files matched your search
@@ -584,6 +584,19 @@ const detectAssetExtension = (buffer: Buffer): string | null => {
|
||||
return 'webp'
|
||||
return null
|
||||
}
|
||||
const htmlArchiveStickerResourceIsValid = async (
|
||||
outputDir: string,
|
||||
value?: string
|
||||
): Promise<boolean> => {
|
||||
const filePath = htmlArchiveResourcePath(outputDir, value)
|
||||
if (!filePath) return false
|
||||
try {
|
||||
return detectAssetExtension(await fs.readFile(filePath)) !== null
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false
|
||||
throw error
|
||||
}
|
||||
}
|
||||
async function readAvatarAsset(
|
||||
source: string
|
||||
): Promise<{ extension: string; buffer: Buffer } | null> {
|
||||
@@ -928,7 +941,10 @@ async function runSingleExport(
|
||||
request.format === 'html'
|
||||
? options.outputFolderName || safeFilePart(request.outputName)
|
||||
: `${safeFilePart(request.outputName)}_${exportStamp()}`
|
||||
const root = options.outputRoot || request.outputDirectory || (await resolveDefaultExportRoot(outputFolder))
|
||||
const root =
|
||||
options.outputRoot ||
|
||||
request.outputDirectory ||
|
||||
(await resolveDefaultExportRoot(outputFolder))
|
||||
await fs.mkdir(root, { recursive: true })
|
||||
const outputDir = join(root, outputFolder)
|
||||
const outputPath =
|
||||
@@ -1329,12 +1345,28 @@ async function runSingleExport(
|
||||
const reusableImageQuality =
|
||||
previous?.exportMediaQuality === 'original' ||
|
||||
(request.preferOriginal === false && previous?.exportMediaQuality === 'thumbnail')
|
||||
let reusableMediaExists = false
|
||||
if (reusableMediaType && previous?.exportMediaUrl) {
|
||||
reusableMediaExists = await resourceExists(previous.exportMediaUrl)
|
||||
if (
|
||||
reusableMediaExists &&
|
||||
reusableMediaType === 'sticker' &&
|
||||
!(await htmlArchiveStickerResourceIsValid(outputDir, previous.exportMediaUrl))
|
||||
) {
|
||||
reusableMediaExists = false
|
||||
resourceExistence.set(previous.exportMediaUrl, Promise.resolve(false))
|
||||
delete previous.exportMediaUrl
|
||||
delete previous.exportMediaType
|
||||
delete previous.exportMediaName
|
||||
delete previous.exportMediaQuality
|
||||
}
|
||||
}
|
||||
if (
|
||||
reusableMediaType &&
|
||||
previous?.exportMediaUrl &&
|
||||
(!previous.exportMediaType || previous.exportMediaType === reusableMediaType) &&
|
||||
(reusableMediaType !== 'image' || reusableImageQuality) &&
|
||||
(await resourceExists(previous.exportMediaUrl))
|
||||
reusableMediaExists
|
||||
) {
|
||||
message.exportMediaUrl = previous.exportMediaUrl
|
||||
message.exportMediaType = reusableMediaType
|
||||
@@ -1446,13 +1478,10 @@ async function runSingleExport(
|
||||
} else if (message.contentData.type === 'sticker' && stickerService) {
|
||||
const stickerSource = message.contentData.url || message.contentData.thumbUrl
|
||||
const result = await stickerService.resolveSticker(stickerSource, message.contentData.md5)
|
||||
const decoded = result.data
|
||||
? decodeDataUrl(result.data)
|
||||
: stickerSource
|
||||
? await readAvatarAsset(stickerSource)
|
||||
: null
|
||||
if (decoded) {
|
||||
const name = `sticker_${bufferHashPart(decoded.buffer)}.${decoded.extension}`
|
||||
const decoded = result.data ? decodeDataUrl(result.data) : null
|
||||
const stickerExtension = decoded ? detectAssetExtension(decoded.buffer) : null
|
||||
if (decoded && stickerExtension) {
|
||||
const name = `sticker_${bufferHashPart(decoded.buffer)}.${stickerExtension}`
|
||||
const mediaUrl = `media/${name}`
|
||||
if (!(await resourceExists(mediaUrl))) {
|
||||
await fs.writeFile(join(outputDir, 'media', name), decoded.buffer)
|
||||
|
||||
+20
-16
@@ -70,16 +70,18 @@ export class StickerService {
|
||||
|
||||
private async readCached(cacheKey: string): Promise<string | null> {
|
||||
const extensions = ['.gif', '.png', '.webp', '.jpg', '.jpeg']
|
||||
const cacheDirs = [
|
||||
this.cacheDir,
|
||||
this.legacyCacheDir
|
||||
]
|
||||
const cacheDirs = [this.cacheDir, this.legacyCacheDir]
|
||||
for (const cacheDir of cacheDirs) {
|
||||
for (const ext of extensions) {
|
||||
const filePath = path.join(cacheDir, `${cacheKey}${ext}`)
|
||||
if (!fs.existsSync(filePath)) continue
|
||||
const buffer = await fs.readFile(filePath)
|
||||
return this.toDataUrl(buffer, ext)
|
||||
const detectedExtension = this.detectExtension(buffer)
|
||||
if (!detectedExtension) {
|
||||
console.warn(`[StickerService] ignored invalid local cache key=${cacheKey}`)
|
||||
continue
|
||||
}
|
||||
return this.toDataUrl(buffer, detectedExtension)
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -108,7 +110,11 @@ export class StickerService {
|
||||
const filePath = path.join(cacheRoot, month, 'Emoticon', prefix, md5)
|
||||
if (!fs.existsSync(filePath)) continue
|
||||
const buffer = await fs.readFile(filePath)
|
||||
const ext = this.detectExtension(buffer) || '.gif'
|
||||
const ext = this.detectExtension(buffer)
|
||||
if (!ext) {
|
||||
console.warn(`[StickerService] ignored invalid WeChat cache md5=${md5} month=${month}`)
|
||||
continue
|
||||
}
|
||||
return this.toDataUrl(buffer, ext)
|
||||
}
|
||||
|
||||
@@ -169,7 +175,14 @@ export class StickerService {
|
||||
return
|
||||
}
|
||||
|
||||
const ext = this.detectExtension(buffer) || this.getExtFromUrl(url) || '.gif'
|
||||
const ext = this.detectExtension(buffer)
|
||||
if (!ext) {
|
||||
console.warn(
|
||||
`[StickerService] download returned invalid image md5=${cacheKey} host=${this.getUrlHost(url)}`
|
||||
)
|
||||
resolve({ success: false, error: '表情包响应不是支持的图片格式' })
|
||||
return
|
||||
}
|
||||
try {
|
||||
await fs.ensureDir(this.cacheDir)
|
||||
await fs.writeFile(path.join(this.cacheDir, `${cacheKey}${ext}`), buffer)
|
||||
@@ -208,15 +221,6 @@ export class StickerService {
|
||||
return null
|
||||
}
|
||||
|
||||
private getExtFromUrl(url: string): string | null {
|
||||
try {
|
||||
const ext = path.extname(new URL(url).pathname).toLowerCase()
|
||||
return ['.gif', '.png', '.webp', '.jpg', '.jpeg'].includes(ext) ? ext : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private getUrlHost(url: string): string {
|
||||
try {
|
||||
return new URL(url).hostname || 'unknown'
|
||||
|
||||
Reference in new issue
Block a user