175 lines
5.0 KiB
Go
175 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"u-desk/internal/filesystem"
|
|
)
|
|
|
|
// ReadFile 读取文件
|
|
func (a *App) ReadFile(path string) (string, error) {
|
|
return a.filesystem.ReadFile(path)
|
|
}
|
|
|
|
// WatchFile 开始监听指定文件的变化,变化时发送 file-changed 事件
|
|
func (a *App) WatchFile(path string) error {
|
|
if a.fileWatcher == nil {
|
|
return fmt.Errorf("文件监听器未初始化")
|
|
}
|
|
return a.fileWatcher.WatchFile(path)
|
|
}
|
|
|
|
// UnwatchFile 停止监听文件变化
|
|
func (a *App) UnwatchFile() {
|
|
if a.fileWatcher != nil {
|
|
a.fileWatcher.UnwatchFile()
|
|
}
|
|
}
|
|
|
|
// WriteFile 写入文件
|
|
func (a *App) WriteFile(req WriteFileRequest) error {
|
|
return a.filesystem.WriteFile(req.Path, req.Content)
|
|
}
|
|
|
|
// SaveBase64File 将 base64 内容解码后写入文件(用于图片等二进制数据)
|
|
func (a *App) SaveBase64File(req SaveBase64FileRequest) error {
|
|
return a.filesystem.SaveBase64File(req.Path, req.Content)
|
|
}
|
|
|
|
// ListDir 列出目录
|
|
func (a *App) ListDir(path string) ([]map[string]interface{}, error) {
|
|
return a.filesystem.ListDir(path)
|
|
}
|
|
|
|
// CreateDir 创建目录
|
|
func (a *App) CreateDir(path string) (*filesystem.FileOperationResult, error) {
|
|
return a.filesystem.CreateDir(path)
|
|
}
|
|
|
|
// CreateFile 创建文件
|
|
func (a *App) CreateFile(path string) (*filesystem.FileOperationResult, error) {
|
|
return a.filesystem.CreateFile(path)
|
|
}
|
|
|
|
// DeletePath 删除文件或目录
|
|
func (a *App) DeletePath(path string) (*filesystem.FileOperationResult, error) {
|
|
return a.filesystem.DeletePath(path)
|
|
}
|
|
|
|
// RenamePath 重命名文件或目录
|
|
func (a *App) RenamePath(req RenamePathRequest) (*filesystem.FileOperationResult, error) {
|
|
return a.filesystem.RenamePath(req.OldPath, req.NewPath)
|
|
}
|
|
|
|
// GetFileInfo 获取文件信息
|
|
func (a *App) GetFileInfo(path string) (map[string]interface{}, error) {
|
|
return a.filesystem.GetFileInfo(path)
|
|
}
|
|
|
|
// OpenPath 使用系统默认程序打开文件或目录
|
|
func (a *App) OpenPath(path string) error {
|
|
return a.filesystem.OpenPath(path)
|
|
}
|
|
|
|
// ========== Zip 文件操作接口 ==========
|
|
|
|
// ListZipContents 列出 zip 文件内容
|
|
func (a *App) ListZipContents(zipPath string) ([]map[string]interface{}, error) {
|
|
return a.filesystem.ListZipContents(zipPath)
|
|
}
|
|
|
|
// ExtractFileFromZip 从 zip 文件中提取单个文件内容
|
|
func (a *App) ExtractFileFromZip(zipPath, filePath string) (string, error) {
|
|
return a.filesystem.ExtractFileFromZip(zipPath, filePath)
|
|
}
|
|
|
|
// ExtractFileFromZipToTemp 从 zip 文件中提取单个文件到临时目录
|
|
func (a *App) ExtractFileFromZipToTemp(zipPath, filePath string) (string, error) {
|
|
return a.filesystem.ExtractFileFromZipToTemp(zipPath, filePath)
|
|
}
|
|
|
|
// GetZipFileInfo 获取 zip 文件中特定文件的信息
|
|
func (a *App) GetZipFileInfo(zipPath, filePath string) (map[string]interface{}, error) {
|
|
return a.filesystem.GetZipFileInfo(zipPath, filePath)
|
|
}
|
|
|
|
// ResolveShortcut 解析快捷方式文件,返回目标路径信息
|
|
func (a *App) ResolveShortcut(lnkPath string) (map[string]interface{}, error) {
|
|
targetPath, err := a.filesystem.ResolveShortcut(lnkPath)
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
}, err
|
|
}
|
|
|
|
fileInfo, err := a.filesystem.GetFileInfo(targetPath)
|
|
if err != nil {
|
|
return map[string]interface{}{
|
|
"success": true,
|
|
"targetPath": targetPath,
|
|
"targetExists": false,
|
|
"targetAccessible": false,
|
|
}, nil
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"success": true,
|
|
"targetPath": targetPath,
|
|
"targetExists": true,
|
|
"targetAccessible": true,
|
|
"targetInfo": fileInfo,
|
|
}, nil
|
|
}
|
|
|
|
// startFileServer 启动文件服务器
|
|
func (a *App) startFileServer() {
|
|
if _, err := filesystem.StartLocalFileServer(); err != nil {
|
|
slog.Error("文件服务器启动失败", "error", err)
|
|
return
|
|
}
|
|
slog.Info("文件服务器已启动", "addr", "http://"+filesystem.GetLocalFileServerAddr())
|
|
}
|
|
|
|
// ========== 文件服务器接口 ==========
|
|
|
|
// GetFileServerURL 获取本地文件服务器的URL
|
|
func (a *App) GetFileServerURL() string {
|
|
return fmt.Sprintf("http://%s", filesystem.GetLocalFileServerAddr())
|
|
}
|
|
|
|
// DetectFileTypeByContent 通过文件内容检测文件类型
|
|
func (a *App) DetectFileTypeByContent(path string) (map[string]interface{}, error) {
|
|
return filesystem.DetectFileTypeByContentSimple(path)
|
|
}
|
|
|
|
// initFilesystemModule 延迟初始化文件系统模块
|
|
func (a *App) initFilesystemModule() {
|
|
if a.filesystem != nil {
|
|
slog.Info("文件系统模块已初始化,跳过")
|
|
return
|
|
}
|
|
|
|
slog.Info("延迟初始化文件系统模块")
|
|
fsConfig := filesystem.DefaultConfig()
|
|
|
|
var err error
|
|
a.filesystem, err = filesystem.NewFileSystemService(fsConfig)
|
|
if err != nil {
|
|
slog.Error("文件系统模块初始化失败", "error", err)
|
|
return
|
|
}
|
|
|
|
go a.startFileServer()
|
|
|
|
slog.Info("文件系统模块初始化完成")
|
|
}
|
|
|
|
// ========== 审计日志接口 ==========
|
|
|
|
// GetAuditLogs 获取审计日志
|
|
func (a *App) GetAuditLogs(limit int) ([]map[string]interface{}, error) {
|
|
return a.filesystem.GetAuditLogs(limit)
|
|
}
|