RWKV-Runner/backend-golang/file.go

261 lines
5.1 KiB
Go
Raw Normal View History

2023-05-13 20:15:18 +08:00
package backend_golang
import (
"encoding/json"
2023-06-01 16:54:21 +08:00
"errors"
2023-05-23 14:04:06 +08:00
"io"
2023-05-13 20:15:18 +08:00
"os"
2023-05-13 23:36:30 +08:00
"os/exec"
2023-05-17 21:20:41 +08:00
"path/filepath"
2023-05-13 23:36:30 +08:00
"runtime"
2023-05-31 16:27:43 +08:00
"strings"
2023-05-13 23:36:30 +08:00
"time"
2023-06-16 00:12:13 +08:00
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
2023-05-13 20:15:18 +08:00
)
2024-02-02 22:00:01 +08:00
func (a *App) GetAbsPath(path string) (string, error) {
var absPath string
var err error
if filepath.IsAbs(path) {
absPath = path
} else {
absPath, err = filepath.Abs(filepath.Join(a.exDir, path))
if err != nil {
return "", err
}
}
absPath = strings.ReplaceAll(absPath, "/", string(os.PathSeparator))
println("GetAbsPath:", absPath)
return absPath, nil
}
2023-12-12 22:13:09 +08:00
func (a *App) SaveFile(path string, savedContent []byte) error {
2024-02-02 22:00:01 +08:00
absPath, err := a.GetAbsPath(path)
if err != nil {
return err
}
if err := os.WriteFile(absPath, savedContent, 0644); err != nil {
2023-12-12 22:13:09 +08:00
return err
}
return nil
}
2024-02-02 22:00:01 +08:00
func (a *App) SaveJson(path string, jsonData any) error {
2023-05-13 20:15:18 +08:00
text, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return err
}
2024-02-02 22:00:01 +08:00
absPath, err := a.GetAbsPath(path)
if err != nil {
return err
}
if err := os.WriteFile(absPath, text, 0644); err != nil {
2023-05-13 20:15:18 +08:00
return err
}
return nil
}
2024-02-02 22:00:01 +08:00
func (a *App) ReadJson(path string) (any, error) {
absPath, err := a.GetAbsPath(path)
if err != nil {
return nil, err
}
file, err := os.ReadFile(absPath)
2023-05-13 20:15:18 +08:00
if err != nil {
return nil, err
}
var data any
err = json.Unmarshal(file, &data)
if err != nil {
return nil, err
}
return data, nil
}
2024-02-02 22:00:01 +08:00
func (a *App) FileExists(path string) bool {
absPath, err := a.GetAbsPath(path)
if err != nil {
return false
}
_, err = os.Stat(absPath)
2023-05-18 22:48:37 +08:00
return err == nil
2023-05-13 20:15:18 +08:00
}
2023-05-13 23:36:30 +08:00
type FileInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
ModTime string `json:"modTime"`
}
2024-02-02 22:00:01 +08:00
func (a *App) ReadFileInfo(path string) (*FileInfo, error) {
absPath, err := a.GetAbsPath(path)
if err != nil {
return nil, err
}
info, err := os.Stat(absPath)
2023-05-13 20:15:18 +08:00
if err != nil {
return nil, err
2023-05-13 20:15:18 +08:00
}
return &FileInfo{
2023-05-13 23:36:30 +08:00
Name: info.Name(),
Size: info.Size(),
IsDir: info.IsDir(),
ModTime: info.ModTime().Format(time.RFC3339),
2023-05-13 20:15:18 +08:00
}, nil
}
2023-05-13 23:36:30 +08:00
func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) {
2024-02-02 22:00:01 +08:00
absDirPath, err := a.GetAbsPath(dirPath)
if err != nil {
return nil, err
}
files, err := os.ReadDir(absDirPath)
2023-05-13 23:36:30 +08:00
if err != nil {
return nil, err
}
var filesInfo []FileInfo
for _, file := range files {
info, err := file.Info()
if err != nil {
return nil, err
}
filesInfo = append(filesInfo, FileInfo{
Name: info.Name(),
Size: info.Size(),
IsDir: info.IsDir(),
ModTime: info.ModTime().Format(time.RFC3339),
})
}
return filesInfo, nil
}
2023-05-20 10:52:25 +08:00
func (a *App) DeleteFile(path string) error {
2024-02-02 22:00:01 +08:00
absPath, err := a.GetAbsPath(path)
if err != nil {
return err
}
err = os.Remove(absPath)
2023-05-20 10:52:25 +08:00
if err != nil {
return err
}
return nil
}
2023-05-23 14:04:06 +08:00
func (a *App) CopyFile(src string, dst string) error {
2024-02-02 22:00:01 +08:00
absSrc, err := a.GetAbsPath(src)
if err != nil {
return err
}
absDst, err := a.GetAbsPath(dst)
if err != nil {
return err
}
sourceFile, err := os.Open(absSrc)
2023-05-23 14:04:06 +08:00
if err != nil {
return err
}
defer sourceFile.Close()
2024-02-02 22:00:01 +08:00
err = os.MkdirAll(filepath.Dir(absDst), 0755)
2023-05-31 16:27:43 +08:00
if err != nil {
return err
}
2024-02-02 22:00:01 +08:00
destFile, err := os.Create(absDst)
2023-05-23 14:04:06 +08:00
if err != nil {
return err
}
defer destFile.Close()
2023-05-23 14:36:24 +08:00
_, err = io.Copy(destFile, sourceFile)
2023-05-23 14:04:06 +08:00
if err != nil {
return err
}
return nil
}
2023-06-16 00:12:13 +08:00
func (a *App) OpenSaveFileDialog(filterPattern string, defaultFileName string, savedContent string) (string, error) {
2023-07-28 12:30:05 +08:00
return a.OpenSaveFileDialogBytes(filterPattern, defaultFileName, []byte(savedContent))
}
func (a *App) OpenSaveFileDialogBytes(filterPattern string, defaultFileName string, savedContent []byte) (string, error) {
2023-06-16 00:12:13 +08:00
path, err := wruntime.SaveFileDialog(a.ctx, wruntime.SaveDialogOptions{
DefaultFilename: defaultFileName,
Filters: []wruntime.FileFilter{{
Pattern: filterPattern,
}},
CanCreateDirectories: true,
})
if err != nil {
return "", err
}
if path == "" {
return "", nil
}
2023-07-28 12:30:05 +08:00
if err := os.WriteFile(path, savedContent, 0644); err != nil {
2023-06-16 00:12:13 +08:00
return "", err
}
return path, nil
}
// Only return the path of the selected file, because communication between frontend and backend is slow. Use AssetServer Handler to read the file.
func (a *App) OpenOpenFileDialog(filterPattern string) (string, error) {
path, err := wruntime.OpenFileDialog(a.ctx, wruntime.OpenDialogOptions{
Filters: []wruntime.FileFilter{{Pattern: filterPattern}},
})
if err != nil {
return "", err
}
if path == "" {
return "", nil
}
return path, nil
}
2024-02-02 22:00:01 +08:00
func (a *App) OpenFileFolder(path string) error {
absPath, err := a.GetAbsPath(path)
2023-05-17 21:20:41 +08:00
if err != nil {
return err
}
2023-05-13 23:36:30 +08:00
switch os := runtime.GOOS; os {
case "windows":
2023-05-17 21:20:41 +08:00
cmd := exec.Command("explorer", "/select,", absPath)
2023-05-13 23:36:30 +08:00
err := cmd.Run()
if err != nil {
return err
}
2023-06-01 16:54:21 +08:00
return nil
2023-05-13 23:36:30 +08:00
case "darwin":
2023-06-01 16:54:21 +08:00
cmd := exec.Command("open", "-R", absPath)
err := cmd.Run()
if err != nil {
return err
}
return nil
2023-05-13 23:36:30 +08:00
case "linux":
2023-06-05 22:55:06 +08:00
cmd := exec.Command("xdg-open", absPath)
err := cmd.Run()
if err != nil {
return err
}
return nil
2023-05-13 23:36:30 +08:00
}
2023-06-01 16:54:21 +08:00
return errors.New("unsupported OS")
2023-05-13 23:36:30 +08:00
}
2023-12-12 22:13:09 +08:00
func (a *App) StartFile(path string) error {
2023-12-12 23:48:32 +08:00
cmd, err := CmdHelper(true, path)
if err != nil {
return err
}
err = cmd.Start()
2023-12-12 22:13:09 +08:00
return err
}