1. 增加配置持久化
2. 完善细节
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Config holds persistent application settings.
|
||||
type Config struct {
|
||||
Theme string `json:"theme"`
|
||||
PreferredAccel string `json:"preferredAccel"`
|
||||
OutputDir string `json:"outputDir"`
|
||||
NamingRule string `json:"namingRule"`
|
||||
FFmpegPath string `json:"ffmpegPath"`
|
||||
FFprobePath string `json:"ffprobePath"`
|
||||
|
||||
dir string `json:"-"` // config directory path
|
||||
}
|
||||
|
||||
var current *Config
|
||||
|
||||
// Load reads config from AppData, creating defaults if needed.
|
||||
func Load() *Config {
|
||||
dir := appDataDir()
|
||||
_ = os.MkdirAll(dir, 0755)
|
||||
|
||||
cfg := &Config{
|
||||
Theme: "light",
|
||||
NamingRule: "{name}_{codec}",
|
||||
dir: dir,
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, "config.json")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Printf("[config] read %s: %v (creating new)", path, err)
|
||||
cfg.Save()
|
||||
log.Printf("[config] created new config at %s", path)
|
||||
return cfg
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, cfg); err != nil {
|
||||
log.Printf("[config] corrupted config, resetting: %v", err)
|
||||
cfg = &Config{Theme: "light", NamingRule: "{name}_{codec}", dir: dir}
|
||||
cfg.Save()
|
||||
return cfg
|
||||
}
|
||||
|
||||
cfg.dir = dir
|
||||
log.Printf("[config] loaded from %s: theme=%s accel=%s", path, cfg.Theme, cfg.PreferredAccel)
|
||||
current = cfg
|
||||
return cfg
|
||||
}
|
||||
|
||||
// Save writes the current config to disk.
|
||||
func (c *Config) Save() {
|
||||
path := filepath.Join(c.dir, "config.json")
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[config] marshal error: %v", err)
|
||||
return
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
log.Printf("[config] write error: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("[config] saved: accel=%s out=%s", c.PreferredAccel, c.OutputDir)
|
||||
}
|
||||
|
||||
// BinDir returns the directory where extracted ffmpeg binaries live.
|
||||
func (c *Config) BinDir() string {
|
||||
d := filepath.Join(c.dir, "bin")
|
||||
os.MkdirAll(d, 0755)
|
||||
return d
|
||||
}
|
||||
|
||||
// Get returns the current global config, loading if needed.
|
||||
func Get() *Config {
|
||||
if current == nil {
|
||||
return Load()
|
||||
}
|
||||
return current
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func appDataDir() string {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, ".config", "ffmpeg-gui")
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func appDataDir() string {
|
||||
appdata := os.Getenv("APPDATA")
|
||||
if appdata == "" {
|
||||
appdata = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming")
|
||||
}
|
||||
return filepath.Join(appdata, "ffmpeg-gui")
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExtractBin copies an embedded binary to the bin directory if not already present.
|
||||
func (c *Config) ExtractBin(name string, embedded []byte) string {
|
||||
dest := filepath.Join(c.BinDir(), name)
|
||||
if _, err := os.Stat(dest); err == nil {
|
||||
return dest
|
||||
}
|
||||
log.Printf("[config] extracting %s ...", name)
|
||||
if err := os.WriteFile(dest, embedded, 0755); err != nil {
|
||||
log.Printf("[config] extract %s failed: %v", name, err)
|
||||
return ""
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
// ExtractZip extracts an embedded zip file to the bin directory.
|
||||
// Extracts ffmpeg.exe and ffprobe.exe from the zip, skipping if already present.
|
||||
func (c *Config) ExtractZip(embedded []byte) (ffmpegPath, ffprobePath string) {
|
||||
dir := c.BinDir()
|
||||
ffmpegPath = filepath.Join(dir, "ffmpeg.exe")
|
||||
ffprobePath = filepath.Join(dir, "ffprobe.exe")
|
||||
|
||||
// If both exist, skip extraction
|
||||
if fileExists(ffmpegPath) && fileExists(ffprobePath) {
|
||||
return
|
||||
}
|
||||
|
||||
reader, err := zip.NewReader(bytes.NewReader(embedded), int64(len(embedded)))
|
||||
if err != nil {
|
||||
log.Printf("[config] zip open failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range reader.File {
|
||||
name := strings.ToLower(f.Name)
|
||||
name = filepath.Base(name)
|
||||
if name != "ffmpeg.exe" && name != "ffprobe.exe" {
|
||||
continue
|
||||
}
|
||||
|
||||
dest := filepath.Join(dir, name)
|
||||
if fileExists(dest) {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("[config] extracting %s (%d bytes)...", name, f.UncompressedSize64)
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
log.Printf("[config] zip open %s: %v", name, err)
|
||||
continue
|
||||
}
|
||||
out, err := os.Create(dest)
|
||||
if err != nil {
|
||||
rc.Close()
|
||||
continue
|
||||
}
|
||||
_, err = io.Copy(out, rc)
|
||||
rc.Close()
|
||||
out.Close()
|
||||
if err != nil {
|
||||
os.Remove(dest)
|
||||
log.Printf("[config] extract %s failed: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user