// ============================================================ // preload/file-storage.ts — 本地存储文件 API(主进程 fs 桥接) // // 提供文件读写、目录管理、下载等接口 // 渲染进程通过 window.fileStorage 调用 // ============================================================ import { ipcRenderer, contextBridge } from 'electron'; import { BIDIRECTIONAL } from '../../shared/constants/ipc'; /** 本地存储文件 API */ export function exposeFileStorage(): void { contextBridge.exposeInMainWorld('fileStorage', { /** 获取 heixiu-data 根目录路径 */ async getDataDir(): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.GET_DATA_DIR); return r?.success ? r.data : null; }, /** 写入文件(Base64 → 磁盘),返回写入的绝对路径 */ async writeFile(relativePath: string, data: string, encoding?: 'base64' | 'utf8'): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_WRITE, { relativePath, data, encoding }); return r?.success ? r.data : null; }, /** 读取文件(磁盘 → Base64) */ async readFile(relativePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_READ, { relativePath }); return r?.success ? r.data : null; }, /** 删除文件 */ async deleteFile(relativePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_DELETE, { relativePath }); return r?.success ?? false; }, /** 递归创建目录 */ async makeDir(relativePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.MAKE_DIR, { relativePath }); return r?.success ? r.data : null; }, /** 在文件管理器中显示文件(绝对路径) */ async showItemInFolder(filePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.SHOW_ITEM_IN_FOLDER, { filePath }); return r?.success ?? false; }, /** 获取文件大小(字节) */ async fileSize(relativePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_SIZE, { relativePath }); return r?.success ? r.data : null; }, /** 检查文件/目录是否存在 */ async fileExists(relativePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_EXISTS, { relativePath }); return r?.success ? r.data : false; }, /** 绝对路径文件写入(绕过 heixiu-data 沙箱) */ async writeFileAbsolute(absolutePath: string, data: string, encoding?: 'base64' | 'utf8'): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.FILE_WRITE_ABSOLUTE, { absolutePath, data, encoding }); return r?.success ? r.data : null; }, /** 绝对路径目录创建(绕过 heixiu-data 沙箱) */ async makeDirAbsolute(absolutePath: string): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.MAKE_DIR_ABSOLUTE, { absolutePath }); return r?.success ? r.data : null; }, /** 主进程下载文件到绝对路径(绕过浏览器 CORS 限制),失败时 reject 并携带错误信息 */ async downloadToPath(url: string, destPath: string, headers?: Record): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.DOWNLOAD_TO_PATH, { url, destPath, headers }); if (r?.success) return r.data; throw new Error(r?.error || '主进程下载失败'); }, /** 获取系统下载目录路径 */ async getDownloadsPath(): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.GET_DOWNLOADS_PATH); return r?.success ? r.data : null; }, /** 生成图片缩略图(读取原图 → 缩放 → JPEG 写入目标路径) */ async generateThumbnail(srcPath: string, destPath: string, maxWidth?: number, quality?: number): Promise { const r = await ipcRenderer.invoke(BIDIRECTIONAL.GENERATE_THUMBNAIL, { srcPath, destPath, maxWidth, quality }); if (r?.success) return r.data; throw new Error(r?.error || '缩略图生成失败'); }, }); }