* 使用多种编码尝试读取文件
(filePath: string)
| 40 | * 使用多种编码尝试读取文件 |
| 41 | */ |
| 42 | async function readFileWithEncoding(filePath: string): Promise<string> { |
| 43 | const buffer = await fs.promises.readFile(filePath); |
| 44 | const encodings = ['utf-8', 'gbk', 'gb2312', 'latin1']; |
| 45 | |
| 46 | for (const encoding of encodings) { |
| 47 | try { |
| 48 | const content = iconv.decode(buffer, encoding); |
| 49 | const replacementChars = (content.match(/\uFFFD/g) || []).length; |
| 50 | |
| 51 | if (content.length > 0) { |
| 52 | if (content.length < 100) { |
| 53 | if (replacementChars > 5) continue; |
| 54 | } else { |
| 55 | if (replacementChars / content.length > 0.05) continue; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (encoding !== 'utf-8') { |
| 60 | // 非 UTF-8 编码,静默处理 |
| 61 | } |
| 62 | return content; |
| 63 | } catch { |
| 64 | continue; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const content = iconv.decode(buffer, 'utf-8'); |
| 69 | return content; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 计算 blob 名称(SHA-256 哈希) |