清理: - 删除重复的 composables(useFilePreview.js、useFileEdit.js) - 已有 TypeScript 版本在 FileSystem/composables/ 优化: - 统一 API 层错误日志到 debugLog(system.ts) - 移除 UpdatePanel 调试面板和调试文本 代码质量: - 提升代码可维护性 - 统一错误处理方式
288 lines
7.1 KiB
TypeScript
288 lines
7.1 KiB
TypeScript
/**
|
||
* 系统信息相关 API
|
||
*/
|
||
|
||
import type { SystemInfo, CPU, Memory, Disk, File } from './types'
|
||
import { debugError } from '@/utils/debugLog'
|
||
|
||
/**
|
||
* 转换后端文件数据格式(蛇形 → 驼峰)
|
||
* 后端返回 is_dir,前端使用 isDir
|
||
*/
|
||
function transformFile(file: any): File {
|
||
return {
|
||
...file,
|
||
isDir: file.is_dir
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量转换文件列表
|
||
*/
|
||
function transformFileList(files: any[]): File[] {
|
||
return files.map(transformFile)
|
||
}
|
||
|
||
/**
|
||
* 获取系统信息
|
||
*/
|
||
export async function getSystemInfo(): Promise<SystemInfo> {
|
||
if (!window.go?.main?.App?.GetSystemInfo) {
|
||
throw new Error('GetSystemInfo API 不可用')
|
||
}
|
||
return await window.go.main.App.GetSystemInfo()
|
||
}
|
||
|
||
/**
|
||
* 获取 CPU 信息
|
||
*/
|
||
export async function getCPUInfo(): Promise<CPU> {
|
||
if (!window.go?.main?.App?.GetCPUInfo) {
|
||
throw new Error('GetCPUInfo API 不可用')
|
||
}
|
||
return await window.go.main.App.GetCPUInfo()
|
||
}
|
||
|
||
/**
|
||
* 获取内存信息
|
||
*/
|
||
export async function getMemoryInfo(): Promise<Memory> {
|
||
if (!window.go?.main?.App?.GetMemoryInfo) {
|
||
throw new Error('GetMemoryInfo API 不可用')
|
||
}
|
||
return await window.go.main.App.GetMemoryInfo()
|
||
}
|
||
|
||
/**
|
||
* 获取磁盘信息
|
||
*/
|
||
export async function getDiskInfo(): Promise<Disk> {
|
||
if (!window.go?.main?.App?.GetDiskInfo) {
|
||
throw new Error('GetDiskInfo API 不可用')
|
||
}
|
||
return await window.go.main.App.GetDiskInfo()
|
||
}
|
||
|
||
/**
|
||
* 列出目录文件
|
||
*/
|
||
export async function listDir(path: string): Promise<File[]> {
|
||
if (!window.go?.main?.App?.ListDir) {
|
||
throw new Error('ListDir API 不可用')
|
||
}
|
||
|
||
const files = await window.go.main.App.ListDir(path)
|
||
return transformFileList(files)
|
||
}
|
||
|
||
/**
|
||
* 读取文件
|
||
*/
|
||
export async function readFile(path: string): Promise<string> {
|
||
if (!window.go?.main?.App?.ReadFile) {
|
||
throw new Error('ReadFile API 不可用')
|
||
}
|
||
return await window.go.main.App.ReadFile(path)
|
||
}
|
||
|
||
/**
|
||
* 写入文件
|
||
*/
|
||
export async function writeFile(path: string, content: string): Promise<void> {
|
||
if (!window.go?.main?.App?.WriteFile) {
|
||
throw new Error('WriteFile API 不可用')
|
||
}
|
||
// 确保传递的是字符串类型
|
||
await window.go.main.App.WriteFile({
|
||
path: String(path),
|
||
content: String(content)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 删除文件或目录
|
||
*/
|
||
export async function deletePath(path: string): Promise<any> {
|
||
if (!window.go?.main?.App?.DeletePath) {
|
||
throw new Error('DeletePath API 不可用')
|
||
}
|
||
return await window.go.main.App.DeletePath(path)
|
||
}
|
||
|
||
/**
|
||
* 创建目录
|
||
*/
|
||
export async function createDir(path: string): Promise<any> {
|
||
if (!window.go?.main?.App?.CreateDir) {
|
||
throw new Error('CreateDir API 不可用')
|
||
}
|
||
return await window.go.main.App.CreateDir(path)
|
||
}
|
||
|
||
/**
|
||
* 创建文件
|
||
*/
|
||
export async function createFile(path: string): Promise<any> {
|
||
if (!window.go?.main?.App?.CreateFile) {
|
||
throw new Error('CreateFile API 不可用')
|
||
}
|
||
return await window.go.main.App.CreateFile(path)
|
||
}
|
||
|
||
/**
|
||
* 重命名文件或目录
|
||
*/
|
||
export async function renamePath(oldPath: string, newPath: string): Promise<any> {
|
||
if (!window.go?.main?.App?.RenamePath) {
|
||
throw new Error('RenamePath API 不可用')
|
||
}
|
||
return await window.go.main.App.RenamePath({
|
||
oldPath: String(oldPath),
|
||
newPath: String(newPath)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取环境变量
|
||
*/
|
||
export async function getEnvVars(): Promise<Record<string, string>> {
|
||
if (!window.go?.main?.App?.GetEnvVars) {
|
||
throw new Error('GetEnvVars API 不可用')
|
||
}
|
||
return await window.go.main.App.GetEnvVars()
|
||
}
|
||
|
||
/**
|
||
* 列出 zip 文件内容
|
||
*/
|
||
export async function listZipContents(zipPath: string): Promise<File[]> {
|
||
if (!window.go?.main?.App?.ListZipContents) {
|
||
throw new Error('ListZipContents API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.ListZipContents(zipPath)
|
||
return transformFileList(result)
|
||
} catch (error) {
|
||
debugError('[API] listZipContents 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从 zip 文件中提取单个文件内容
|
||
*/
|
||
export async function extractFileFromZip(zipPath: string, filePath: string): Promise<string> {
|
||
if (!window.go?.main?.App?.ExtractFileFromZip) {
|
||
throw new Error('ExtractFileFromZip API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.ExtractFileFromZip(zipPath, filePath)
|
||
return result
|
||
} catch (error) {
|
||
debugError('[API] extractFileFromZip 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从 zip 文件中提取单个文件到临时目录
|
||
* 返回临时文件的完整路径,适用于图片等二进制文件
|
||
*/
|
||
export async function extractFileFromZipToTemp(zipPath: string, filePath: string): Promise<string> {
|
||
if (!window.go?.main?.App?.ExtractFileFromZipToTemp) {
|
||
throw new Error('ExtractFileFromZipToTemp API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.ExtractFileFromZipToTemp(zipPath, filePath)
|
||
return result
|
||
} catch (error) {
|
||
debugError('[API] extractFileFromZipToTemp 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取 zip 文件中特定文件的信息
|
||
*/
|
||
export async function getZipFileInfo(zipPath: string, filePath: string): Promise<File> {
|
||
if (!window.go?.main?.App?.GetZipFileInfo) {
|
||
throw new Error('GetZipFileInfo API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.GetZipFileInfo(zipPath, filePath)
|
||
return transformFile(result)
|
||
} catch (error) {
|
||
debugError('[API] getZipFileInfo 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 使用系统默认程序打开文件或目录
|
||
*/
|
||
export async function openPath(path: string): Promise<void> {
|
||
if (!window.go?.main?.App?.OpenPath) {
|
||
throw new Error('OpenPath API 不可用')
|
||
}
|
||
try {
|
||
await window.go.main.App.OpenPath(path)
|
||
} catch (error) {
|
||
debugError('[API] openPath 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取本地文件服务器URL
|
||
*/
|
||
export async function getFileServerURL(): Promise<string> {
|
||
if (!window.go?.main?.App?.GetFileServerURL) {
|
||
throw new Error('GetFileServerURL API 不可用')
|
||
}
|
||
return await window.go.main.App.GetFileServerURL()
|
||
}
|
||
|
||
/**
|
||
* 解析快捷方式文件,返回目标路径信息
|
||
*/
|
||
export async function resolveShortcut(lnkPath: string): Promise<{
|
||
success: boolean
|
||
message?: string
|
||
targetPath?: string
|
||
targetExists?: boolean
|
||
targetAccessible?: boolean
|
||
targetInfo?: any
|
||
}> {
|
||
if (!window.go?.main?.App?.ResolveShortcut) {
|
||
throw new Error('ResolveShortcut API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.ResolveShortcut(lnkPath)
|
||
return result
|
||
} catch (error) {
|
||
debugError('[API] resolveShortcut 错误:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 通过文件内容检测文件类型(用于小文件,500KB以内)
|
||
*/
|
||
export async function detectFileTypeByContent(path: string): Promise<{
|
||
extension: string
|
||
category: string // 'image' | 'text' | 'binary' | 'pdf' | 'archive' | 'unknown'
|
||
mime_type: string
|
||
confidence: number
|
||
}> {
|
||
if (!window.go?.main?.App?.DetectFileTypeByContent) {
|
||
throw new Error('DetectFileTypeByContent API 不可用')
|
||
}
|
||
try {
|
||
const result = await window.go.main.App.DetectFileTypeByContent(path)
|
||
return result as any
|
||
} catch (error) {
|
||
debugError('[API] detectFileTypeByContent 错误:', error)
|
||
throw error
|
||
}
|
||
}
|