/** * Wails Transport — 本地文件操作(通过 Wails IPC) */ import type { FsTransport, FileItem, FileOperationResult, DetectTypeResult } from './transport' function transformFile(file: any): FileItem { return { ...file, is_dir: file.is_dir, mod_time: file.mod_time } } function transformFileList(files: any[]): FileItem[] { return files.map(transformFile) } export class WailsTransport implements FsTransport { private checkAvailable(method: string) { if (!window.go?.main?.App?.[method]) { throw new Error(`${method} API 不可用`) } } async listDir(path: string): Promise { this.checkAvailable('ListDir') return transformFileList(await window.go.main.App.ListDir(path)) } async getFileInfo(path: string): Promise> { this.checkAvailable('GetFileInfo') return window.go.main.App.GetFileInfo(path) } async readFile(path: string): Promise { this.checkAvailable('ReadFile') return window.go.main.App.ReadFile(path) } async writeFile(path: string, content: string): Promise { this.checkAvailable('WriteFile') await window.go.main.App.WriteFile({ path: String(path), content: String(content) }) } async saveBase64File(path: string, content: string): Promise { this.checkAvailable('SaveBase64File') if (!content) throw new Error('无效的 base64 内容') await window.go.main.App.SaveBase64File({ path: String(path), content }) } async createFile(dirPath: string, filename: string): Promise { this.checkAvailable('CreateFile') const fullPath = dirPath.replace(/\/$/, '') + '/' + filename return window.go.main.App.CreateFile(fullPath) } async createDir(parentPath: string, dirname: string): Promise { this.checkAvailable('CreateDir') const fullPath = parentPath.replace(/\/$/, '') + '/' + dirname return window.go.main.App.CreateDir(fullPath) } async deletePath(path: string): Promise { this.checkAvailable('DeletePath') return window.go.main.App.DeletePath(path) } async renamePath(oldPath: string, newPath: string): Promise { this.checkAvailable('RenamePath') return window.go.main.App.RenamePath({ oldPath: String(oldPath), newPath: String(newPath) }) } async listZipContents(zipPath: string): Promise { this.checkAvailable('ListZipContents') return transformFileList(await window.go.main.App.ListZipContents(zipPath)) } async extractFileFromZip(zipPath: string, filePath: string): Promise { this.checkAvailable('ExtractFileFromZip') return window.go.main.App.ExtractFileFromZip(zipPath, filePath) } async extractFileFromZipToTemp(zipPath: string, filePath: string): Promise { this.checkAvailable('ExtractFileFromZipToTemp') return window.go.main.App.ExtractFileFromZipToTemp(zipPath, filePath) } async getZipFileInfo(zipPath: string, filePath: string): Promise { this.checkAvailable('GetZipFileInfo') return transformFile(await window.go.main.App.GetZipFileInfo(zipPath, filePath)) } async openPath(path: string): Promise { this.checkAvailable('OpenPath') await window.go.main.App.OpenPath(path) } async getFileServerURL(): Promise { this.checkAvailable('GetFileServerURL') return window.go.main.App.GetFileServerURL() } getPreviewToken(): string { return '' // 本地模式无需 token } async resolveShortcut(lnkPath: string): Promise { this.checkAvailable('ResolveShortcut') return window.go.main.App.ResolveShortcut(lnkPath) } async detectFileTypeByContent(path: string): Promise { this.checkAvailable('DetectFileTypeByContent') const result = await window.go.main.App.DetectFileTypeByContent(path) return result as unknown as DetectTypeResult } async getCommonPaths(): Promise> { this.checkAvailable('GetCommonPaths') return window.go.main.App.GetCommonPaths() } async getRecycleBinEntries(): Promise { this.checkAvailable('GetRecycleBinEntries') return window.go.main.App.GetRecycleBinEntries() } async restoreFromRecycleBin(path: string): Promise { this.checkAvailable('RestoreFromRecycleBin') await window.go.main.App.RestoreFromRecycleBin(path) } async deletePermanently(path: string): Promise { this.checkAvailable('DeletePermanently') await window.go.main.App.DeletePermanently(path) } async emptyRecycleBin(): Promise { this.checkAvailable('EmptyRecycleBin') await window.go.main.App.EmptyRecycleBin() } }