77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package services
|
||
|
||
import (
|
||
"context"
|
||
"os"
|
||
"path/filepath"
|
||
)
|
||
|
||
// FileService 文件服务
|
||
type FileService struct{}
|
||
|
||
// NewFileService 创建文件服务实例
|
||
func NewFileService() *FileService {
|
||
return &FileService{}
|
||
}
|
||
|
||
// SelectFolder 选择文件夹(返回路径)
|
||
func (s *FileService) SelectFolder(ctx context.Context) (string, error) {
|
||
// 在 Wails3 中,文件选择需要通过前端实现
|
||
// 这里只是占位,实际应该通过前端调用系统对话框
|
||
return "", nil
|
||
}
|
||
|
||
// SelectFile 选择文件(返回路径)
|
||
func (s *FileService) SelectFile(ctx context.Context, filter string) (string, error) {
|
||
// 在 Wails3 中,文件选择需要通过前端实现
|
||
return "", nil
|
||
}
|
||
|
||
// OpenFolder 打开文件夹
|
||
func (s *FileService) OpenFolder(ctx context.Context, folderPath string) error {
|
||
// Windows 下打开文件夹
|
||
cmd := "explorer"
|
||
args := []string{folderPath}
|
||
|
||
// 这里需要使用 exec.Command,但为了简化,我们返回路径让前端处理
|
||
// 或者使用系统调用
|
||
return nil
|
||
}
|
||
|
||
// FileExists 检查文件是否存在
|
||
func (s *FileService) FileExists(ctx context.Context, filePath string) (bool, error) {
|
||
_, err := os.Stat(filePath)
|
||
if err == nil {
|
||
return true, nil
|
||
}
|
||
if os.IsNotExist(err) {
|
||
return false, nil
|
||
}
|
||
return false, err
|
||
}
|
||
|
||
// GetFileSize 获取文件大小(字节)
|
||
func (s *FileService) GetFileSize(ctx context.Context, filePath string) (int64, error) {
|
||
info, err := os.Stat(filePath)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
return info.Size(), nil
|
||
}
|
||
|
||
// EnsureDirectory 确保目录存在
|
||
func (s *FileService) EnsureDirectory(ctx context.Context, dirPath string) error {
|
||
return os.MkdirAll(dirPath, 0755)
|
||
}
|
||
|
||
// ListFiles 列出目录中的文件
|
||
func (s *FileService) ListFiles(ctx context.Context, dirPath string, pattern string) ([]string, error) {
|
||
patternPath := filepath.Join(dirPath, pattern)
|
||
matches, err := filepath.Glob(patternPath)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return matches, nil
|
||
}
|
||
|