Allow overriding `name` and `comment` when importing NSP playlists

This commit is contained in:
Deluan 2021-11-19 19:14:38 -05:00
parent 92c31c961d
commit ce7940bbfc
1 changed files with 26 additions and 2 deletions

View File

@ -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)
}