RWKV-Runner/backend-golang/file.go

214 lines
4.4 KiB
Go
Raw Permalink 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
)
2023-12-12 22:13:09 +08:00
func (a *App) SaveFile(path string, savedContent []byte) error {
if err := os.WriteFile(a.exDir+path, savedContent, 0644); err != nil {
return err
}
return nil
}
2023-05-13 20:15:18 +08:00
func (a *App) SaveJson(fileName string, jsonData any) error {
text, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return err
}
2023-06-01 16:54:21 +08:00
if err := os.WriteFile(a.exDir+fileName, text, 0644); err != nil {
2023-05-13 20:15:18 +08:00
return err
}
return nil
}
func (a *App) ReadJson(fileName string) (any, error) {
2023-06-01 16:54:21 +08:00
file, err := os.ReadFile(a.exDir + fileName)
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
}
2023-05-18 22:48:37 +08:00
func (a *App) FileExists(fileName string) bool {
2023-06-01 16:54:21 +08:00
_, err := os.Stat(a.exDir + fileName)
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"`
}
func (a *App) ReadFileInfo(fileName string) (*FileInfo, error) {
2023-06-01 16:54:21 +08:00
info, err := os.Stat(a.exDir + fileName)
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) {
2023-06-01 16:54:21 +08:00
files, err := os.ReadDir(a.exDir + dirPath)
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 {
2023-06-01 16:54:21 +08:00
err := os.Remove(a.exDir + path)
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 {
2023-06-01 16:54:21 +08:00
sourceFile, err := os.Open(a.exDir + src)
2023-05-23 14:04:06 +08:00
if err != nil {
return err
}
defer sourceFile.Close()
2023-06-01 16:54:21 +08:00
err = os.MkdirAll(a.exDir+dst[:strings.LastIndex(dst, "/")], 0755)
2023-05-31 16:27:43 +08:00
if err != nil {
return err
}
2023-06-01 16:54:21 +08:00
destFile, err := os.Create(a.exDir + dst)
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
}
2023-06-14 21:11:08 +08:00
func (a *App) OpenFileFolder(path string, relative bool) error {
var absPath string
var err error
if relative {
absPath, err = filepath.Abs(a.exDir + path)
} else {
absPath, err = filepath.Abs(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
}