|
- // Package resume persists and restores the playback position across
- // application restarts — a port of the Delphi setresume/getresume feature.
- package resume
-
- import (
- "encoding/json"
- "errors"
- "os"
- )
-
- // State holds enough information to restore a playback session.
- type State struct {
- PlaylistLength int `json:"playlist_length"`
- PlaylistPos int `json:"playlist_pos"`
- OffsetSeconds int `json:"offset_seconds"`
- TrackTitle string `json:"track_title"`
- }
-
- // Save persists the given state to a JSON file.
- func Save(path string, s State) error {
- data, err := json.Marshal(s)
- if err != nil {
- return err
- }
- return os.WriteFile(path, data, 0644)
- }
-
- // Load reads the state from disk. Returns (nil, nil) if no file exists yet.
- func Load(path string) (*State, error) {
- data, err := os.ReadFile(path)
- if errors.Is(err, os.ErrNotExist) {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- var s State
- if err := json.Unmarshal(data, &s); err != nil {
- return nil, err
- }
- return &s, nil
- }
-
- // Delete removes the resume file (call after successful restore).
- func Delete(path string) error {
- err := os.Remove(path)
- if errors.Is(err, os.ErrNotExist) {
- return nil
- }
- return err
- }
|