Skip to content

Commit

Permalink
Generate Order Fields based on sanitized version of original fields
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Apr 24, 2020
1 parent 69c19e9 commit 371e8ab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions model/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type MediaFile struct {
SortArtistName string `json:"sortArtistName"`
SortAlbumArtistName string `json:"sortAlbumArtistName"`
OrderAlbumName string `json:"orderAlbumName"`
OrderArtistName string `json:"orderArtistName"`
OrderAlbumArtistName string `json:"orderAlbumArtistName"`
Compilation bool `json:"compilation"`
CreatedAt time.Time `json:"createdAt"`
Expand Down
15 changes: 13 additions & 2 deletions scanner/tag_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/utils"
"github.com/kennygrant/sanitize"
)

type TagScanner struct {
Expand Down Expand Up @@ -241,7 +243,7 @@ func (s *TagScanner) loadTracks(filePaths []string) (model.MediaFiles, error) {
}

func (s *TagScanner) toMediaFile(md *Metadata) model.MediaFile {
mf := model.MediaFile{}
mf := &model.MediaFile{}
mf.ID = s.trackID(md)
mf.Title = s.mapTrackTitle(md)
mf.Album = md.Album()
Expand All @@ -266,12 +268,21 @@ func (s *TagScanner) toMediaFile(md *Metadata) model.MediaFile {
mf.SortAlbumName = md.SortAlbum()
mf.SortArtistName = md.SortArtist()
mf.SortAlbumArtistName = md.SortAlbumArtist()
mf.OrderAlbumName = sanitizeFieldForSorting(mf.Album)
mf.OrderArtistName = sanitizeFieldForSorting(mf.Artist)
mf.OrderAlbumArtistName = sanitizeFieldForSorting(mf.AlbumArtist)

// TODO Get Creation time. https://github.com/djherbis/times ?
mf.CreatedAt = md.ModificationTime()
mf.UpdatedAt = md.ModificationTime()

return mf
return *mf
}

func sanitizeFieldForSorting(originalValue string) string {
v := utils.NoArticle(originalValue)
v = strings.TrimSpace(sanitize.Accents(v))
return utils.NoArticle(v)
}

func (s *TagScanner) mapTrackTitle(md *Metadata) string {
Expand Down
21 changes: 21 additions & 0 deletions scanner/tag_scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scanner

import (
"github.com/deluan/navidrome/conf"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("TagScanner", func() {
Describe("sanitizeFieldForSorting", func() {
BeforeEach(func() {
conf.Server.IgnoredArticles = "The"
})
It("sanitize accents", func() {
Expect(sanitizeFieldForSorting("C茅u")).To(Equal("Ceu"))
})
It("removes articles", func() {
Expect(sanitizeFieldForSorting("The Beatles")).To(Equal("Beatles"))
})
})
})

0 comments on commit 371e8ab

Please sign in to comment.