RWKV-Runner/backend-golang/file.go

135 lines
2.4 KiB
Go
Raw Normal View History

2023-05-13 20:15:18 +08:00
package backend_golang
import (
"encoding/json"
2023-05-13 23:36:30 +08:00
"fmt"
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"
"time"
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
}
if err := os.WriteFile(fileName, text, 0644); err != nil {
return err
}
return nil
}
func (a *App) ReadJson(fileName string) (any, error) {
file, err := os.ReadFile(fileName)
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-05-13 20:15:18 +08:00
_, err := os.Stat(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-05-13 20:15:18 +08:00
info, err := os.Stat(fileName)
if err != nil {
2023-05-13 23:36:30 +08:00
return FileInfo{}, err
2023-05-13 20:15:18 +08:00
}
2023-05-13 23:36:30 +08:00
return FileInfo{
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) {
files, err := os.ReadDir(dirPath)
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 {
err := os.Remove(path)
if err != nil {
return err
}
return nil
}
2023-05-23 14:04:06 +08:00
func (a *App) CopyFile(src string, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
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-05-13 23:36:30 +08:00
func (a *App) OpenFileFolder(path string) error {
2023-05-17 21:20:41 +08:00
absPath, err := filepath.Abs(path)
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
}
case "darwin":
fmt.Println("Running on macOS")
case "linux":
fmt.Println("Running on Linux")
}
return nil
}