Don't import invalid .nsp files

This commit is contained in:
Deluan 2021-10-22 15:14:41 -04:00 committed by Deluan Quintão
parent 6a550dab77
commit 2c860edeb5
4 changed files with 9 additions and 4 deletions

View file

@ -38,13 +38,13 @@ func IsPlaylist(filePath string) bool {
func (s *playlists) ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error) { func (s *playlists) ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error) {
pls, err := s.parsePlaylist(ctx, fname, dir) pls, err := s.parsePlaylist(ctx, fname, dir)
if err != nil { if err != nil {
log.Error(ctx, "Error parsing playlist", "playlist", fname, err) log.Error(ctx, "Error parsing playlist", "path", filepath.Join(dir, fname), err)
return nil, err return nil, err
} }
log.Debug("Found playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", len(pls.Tracks)) log.Debug("Found playlist", "name", pls.Name, "lastUpdated", pls.UpdatedAt, "path", pls.Path, "numTracks", len(pls.Tracks))
err = s.updatePlaylist(ctx, pls) err = s.updatePlaylist(ctx, pls)
if err != nil { if err != nil {
log.Error(ctx, "Error updating playlist", "playlist", fname, err) log.Error(ctx, "Error updating playlist", "path", filepath.Join(dir, fname), err)
} }
return pls, err return pls, err
} }

View file

@ -3,6 +3,7 @@ package criteria
import ( import (
"encoding/json" "encoding/json"
"errors"
"strings" "strings"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
@ -71,6 +72,8 @@ func (c *Criteria) UnmarshalJSON(data []byte) error {
c.Expression = Any(aux.Any) c.Expression = Any(aux.Any)
} else if len(aux.All) > 0 { } else if len(aux.All) > 0 {
c.Expression = All(aux.All) c.Expression = All(aux.All)
} else {
return errors.New("invalid criteria json. missing rules (key 'all' or 'any')")
} }
c.Sort = aux.Sort c.Sort = aux.Sort
c.Order = aux.Order c.Order = aux.Order

View file

@ -226,7 +226,10 @@ func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool {
} }
func (r *playlistRepository) addCriteria(sql SelectBuilder, c criteria.Criteria) SelectBuilder { func (r *playlistRepository) addCriteria(sql SelectBuilder, c criteria.Criteria) SelectBuilder {
sql = sql.Where(c.ToSql()).Limit(uint64(c.Limit)).Offset(uint64(c.Offset)) sql = sql.Where(c.ToSql())
if c.Limit > 0 {
sql = sql.Limit(uint64(c.Limit)).Offset(uint64(c.Offset))
}
if order := c.OrderBy(); order != "" { if order := c.OrderBy(); order != "" {
sql = sql.OrderBy(order) sql = sql.OrderBy(order)
} }

View file

@ -39,7 +39,6 @@ func (s *playlistImporter) processPlaylists(ctx context.Context, dir string) int
} }
pls, err := s.pls.ImportFile(ctx, dir, f.Name()) pls, err := s.pls.ImportFile(ctx, dir, f.Name())
if err != nil { if err != nil {
log.Error(ctx, "Error parsing playlist", "playlist", f.Name(), err)
continue continue
} }
if pls.IsSmartPlaylist() { if pls.IsSmartPlaylist() {