// scripts/encrypt_pdf.ts import * as fs from 'fs' import * as path from 'path' import pako from 'pako' import { fileURLToPath } from 'url' const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PDF_DIR = path.resolve(__dirname, '../../public/pdf') // 工具:ArrayBuffer / Uint8Array 转 Base64 function ab2b64(buffer: ArrayBuffer) { let binary = ''; const bytes = new Uint8Array(buffer); bytes.forEach(b => binary += String.fromCharCode(b)); return btoa(binary); } // 简单混淆:Base64 -> URL安全 + 去掉 '=' function obfuscate(base64str: string) { return base64str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } // 主函数:压缩 + 混淆 function compressAndObfuscate(inputPath: string, outputPath: string) { const input = fs.readFileSync(inputPath) // 压缩成 Uint8Array const compressed = pako.deflate(new Uint8Array(input)); // 转 Base64 字符串 const b64 = ab2b64(compressed.buffer); // 混淆返回 const output = obfuscate(b64); fs.writeFileSync(outputPath, output) console.log(`✔ Compress: ${path.basename(inputPath)}`) } function encryptAllPDFs(): void { const files = fs.readdirSync(PDF_DIR) for (const file of files) { const fullPath = path.join(PDF_DIR, file) if (!fs.statSync(fullPath).isFile() || !file.endsWith('.pdf')) continue const encPath = fullPath + '.enc' if (fs.existsSync(encPath)) { console.log(`⏭ Skipped (already Compress): ${file}`) continue } compressAndObfuscate(fullPath, encPath) } } encryptAllPDFs()