项目文件夹

文件
wehub-resource-sync ead81af521
Deploy to GitHub Pages / deploy (push) Failing after 0s
CI / go (push) Has been cancelled
CI / build (darwin, macos-14) (push) Has been cancelled
CI / build (windows, windows-2025) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

64 行
1.5 KiB
Go

// Package resume persists the last-played track and position so playback
// can be resumed on the next launch.
package resume
import (
"encoding/json"
"os"
"path/filepath"
"github.com/bjarneo/cliamp/internal/appdir"
)
// State holds enough information to resume a previous playback session.
type State struct {
Path string `json:"path"`
PositionSec int `json:"position_sec"`
Playlist string `json:"playlist,omitempty"`
}
func stateFile() (string, error) {
dir, err := appdir.Dir()
if err != nil {
return "", err
}
return filepath.Join(dir, "resume.json"), nil
}
// Save writes the resume state to disk. No-ops for empty path or zero/negative
// position to avoid overwriting a valid resume file with useless data.
// Errors are silently ignored so a failed write never disrupts normal exit.
func Save(path string, positionSec int, playlist string) {
if path == "" || positionSec <= 0 {
return
}
f, err := stateFile()
if err != nil {
return
}
data, err := json.Marshal(State{Path: path, PositionSec: positionSec, Playlist: playlist})
if err != nil {
return
}
_ = os.MkdirAll(filepath.Dir(f), 0o755)
_ = os.WriteFile(f, data, 0o600)
}
// Load reads the resume state from disk. Returns a zero State if the file
// does not exist or cannot be parsed.
func Load() State {
f, err := stateFile()
if err != nil {
return State{}
}
data, err := os.ReadFile(f)
if err != nil {
return State{}
}
var s State
if err := json.Unmarshal(data, &s); err != nil {
return State{}
}
return s
}