Web-based Winamp controller for CarPC � Go backend, mobile-first UI
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

52 行
1.2KB

  1. // Package resume persists and restores the playback position across
  2. // application restarts — a port of the Delphi setresume/getresume feature.
  3. package resume
  4. import (
  5. "encoding/json"
  6. "errors"
  7. "os"
  8. )
  9. // State holds enough information to restore a playback session.
  10. type State struct {
  11. PlaylistLength int `json:"playlist_length"`
  12. PlaylistPos int `json:"playlist_pos"`
  13. OffsetSeconds int `json:"offset_seconds"`
  14. TrackTitle string `json:"track_title"`
  15. }
  16. // Save persists the given state to a JSON file.
  17. func Save(path string, s State) error {
  18. data, err := json.Marshal(s)
  19. if err != nil {
  20. return err
  21. }
  22. return os.WriteFile(path, data, 0644)
  23. }
  24. // Load reads the state from disk. Returns (nil, nil) if no file exists yet.
  25. func Load(path string) (*State, error) {
  26. data, err := os.ReadFile(path)
  27. if errors.Is(err, os.ErrNotExist) {
  28. return nil, nil
  29. }
  30. if err != nil {
  31. return nil, err
  32. }
  33. var s State
  34. if err := json.Unmarshal(data, &s); err != nil {
  35. return nil, err
  36. }
  37. return &s, nil
  38. }
  39. // Delete removes the resume file (call after successful restore).
  40. func Delete(path string) error {
  41. err := os.Remove(path)
  42. if errors.Is(err, os.ErrNotExist) {
  43. return nil
  44. }
  45. return err
  46. }