82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package recovery
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"navimigrate/internal/model"
|
|
)
|
|
|
|
const ManifestVersion = 1
|
|
|
|
type Manifest struct {
|
|
Version int `json:"version"`
|
|
GeneratedAt string `json:"generated_at"`
|
|
UpdatedAt string `json:"updated_at,omitempty"`
|
|
DownloadDir string `json:"download_dir,omitempty"`
|
|
QobuzDLPath string `json:"qobuz_dl_path,omitempty"`
|
|
Playlists []PlaylistManifest `json:"playlists"`
|
|
}
|
|
|
|
type PlaylistManifest struct {
|
|
Name string `json:"name"`
|
|
TargetID string `json:"target_id,omitempty"`
|
|
Tracks []TrackManifest `json:"tracks"`
|
|
}
|
|
|
|
type TrackManifest struct {
|
|
Source model.Track `json:"source"`
|
|
LookupError string `json:"lookup_error,omitempty"`
|
|
|
|
QobuzQuery string `json:"qobuz_query,omitempty"`
|
|
QobuzScore float64 `json:"qobuz_score,omitempty"`
|
|
QobuzTrackID string `json:"qobuz_track_id,omitempty"`
|
|
QobuzAlbumID string `json:"qobuz_album_id,omitempty"`
|
|
QobuzAlbumTitle string `json:"qobuz_album_title,omitempty"`
|
|
QobuzAlbumArtist string `json:"qobuz_album_artist,omitempty"`
|
|
|
|
DownloadAttempted bool `json:"download_attempted,omitempty"`
|
|
Downloaded bool `json:"downloaded,omitempty"`
|
|
DownloadError string `json:"download_error,omitempty"`
|
|
|
|
RematchAttempted bool `json:"rematch_attempted,omitempty"`
|
|
Rematched bool `json:"rematched,omitempty"`
|
|
RematchTrackID string `json:"rematch_track_id,omitempty"`
|
|
RematchReason string `json:"rematch_reason,omitempty"`
|
|
|
|
AddAttempted bool `json:"add_attempted,omitempty"`
|
|
Added bool `json:"added,omitempty"`
|
|
AddError string `json:"add_error,omitempty"`
|
|
}
|
|
|
|
func Save(path string, m Manifest) error {
|
|
f, err := os.Create(path)
|
|
if err != nil {
|
|
return fmt.Errorf("create manifest file: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
enc := json.NewEncoder(f)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(m); err != nil {
|
|
return fmt.Errorf("encode manifest: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Load(path string) (Manifest, error) {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return Manifest{}, fmt.Errorf("read manifest file: %w", err)
|
|
}
|
|
var m Manifest
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
return Manifest{}, fmt.Errorf("decode manifest: %w", err)
|
|
}
|
|
if m.Version == 0 {
|
|
m.Version = ManifestVersion
|
|
}
|
|
return m, nil
|
|
}
|