From ce7940bbfce72f726717eac007394f2a04a46d4b Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 19 Nov 2021 19:14:38 -0500 Subject: [PATCH] Allow overriding `name` and `comment` when importing NSP playlists --- core/playlists.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/core/playlists.go b/core/playlists.go index 83f8605f2..cd05fc4b2 100644 --- a/core/playlists.go +++ b/core/playlists.go @@ -94,13 +94,20 @@ func (s *playlists) newSyncedPlaylist(baseDir string, playlistFile string) (*mod } func (s *playlists) parseNSP(ctx context.Context, pls *model.Playlist, file io.Reader) (*model.Playlist, error) { - pls.Rules = &criteria.Criteria{} + nsp := &nspFile{} dec := json.NewDecoder(file) - err := dec.Decode(pls.Rules) + err := dec.Decode(nsp) if err != nil { log.Error(ctx, "Error parsing SmartPlaylist", "playlist", pls.Name, err) return nil, err } + pls.Rules = &nsp.Criteria + if nsp.Name != "" { + pls.Name = nsp.Name + } + if nsp.Comment != "" { + pls.Comment = nsp.Comment + } return pls, nil } @@ -236,3 +243,20 @@ func (s *playlists) Update(ctx context.Context, playlistId string, return repo.Put(pls) }) } + +type nspFile struct { + criteria.Criteria + Name string `json:"name"` + Comment string `json:"comment"` +} + +func (i *nspFile) UnmarshalJSON(data []byte) error { + m := map[string]interface{}{} + err := json.Unmarshal(data, &m) + if err != nil { + return err + } + i.Name, _ = m["name"].(string) + i.Comment, _ = m["comment"].(string) + return json.Unmarshal(data, &i.Criteria) +}