diff --git a/server/handler/handler_sub_by_folder.go b/server/handler/handler_sub_by_folder.go index c5c9ac7..cdf0823 100644 --- a/server/handler/handler_sub_by_folder.go +++ b/server/handler/handler_sub_by_folder.go @@ -29,10 +29,11 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) { Where("albums.parent_id = 1"). Group("albums.id"). Find(&folders) - indexMap := make(map[string]*subsonic.Index) - indexes := []*subsonic.Index{} + // [a-z#] -> 27 + indexMap := make(map[string]*subsonic.Index, 27) + resp := make([]*subsonic.Index, 0, 27) for _, folder := range folders { - i := indexOf(folder.IndexRightPath()) + i := lowerUDecOrHash(folder.IndexRightPath()) index, ok := indexMap[i] if !ok { index = &subsonic.Index{ @@ -40,18 +41,18 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) { Artists: []*subsonic.Artist{}, } indexMap[i] = index - indexes = append(indexes, index) + resp = append(resp, index) } index.Artists = append(index.Artists, newArtistByFolder(folder)) } - sort.Slice(indexes, func(i, j int) bool { - return indexes[i].Name < indexes[j].Name + sort.Slice(resp, func(i, j int) bool { + return resp[i].Name < resp[j].Name }) sub := subsonic.NewResponse() sub.Indexes = &subsonic.Indexes{ LastModified: 0, - Index: indexes, + Index: resp, } respond(w, r, sub) } diff --git a/server/handler/handler_sub_by_tags.go b/server/handler/handler_sub_by_tags.go index d4a4e4e..79bfc2d 100644 --- a/server/handler/handler_sub_by_tags.go +++ b/server/handler/handler_sub_by_tags.go @@ -22,10 +22,11 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) { `). Group("artists.id"). Find(&artists) - indexMap := make(map[string]*subsonic.Index) - indexes := &subsonic.Artists{} + // [a-z#] -> 27 + indexMap := make(map[string]*subsonic.Index, 27) + resp := make([]*subsonic.Index, 0, 27) for _, artist := range artists { - i := indexOf(artist.IndexName()) + i := lowerUDecOrHash(artist.IndexName()) index, ok := indexMap[i] if !ok { index = &subsonic.Index{ @@ -33,16 +34,18 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) { Artists: []*subsonic.Artist{}, } indexMap[i] = index - indexes.List = append(indexes.List, index) + resp = append(resp, index) } index.Artists = append(index.Artists, newArtistByTags(artist)) } - sort.Slice(indexes.List, func(i, j int) bool { - return indexes.List[i].Name < indexes.List[j].Name + sort.Slice(resp, func(i, j int) bool { + return resp[i].Name < resp[j].Name }) sub := subsonic.NewResponse() - sub.Artists = indexes + sub.Artists = &subsonic.Artists{ + List: resp, + } respond(w, r, sub) } diff --git a/server/handler/handler_sub_common.go b/server/handler/handler_sub_common.go index 6dc0c8b..9868f6e 100644 --- a/server/handler/handler_sub_common.go +++ b/server/handler/handler_sub_common.go @@ -17,7 +17,7 @@ import ( "github.com/sentriz/gonic/server/subsonic" ) -func indexOf(in string) string { +func lowerUDecOrHash(in string) string { lower := unicode.ToLower(rune(in[0])) if !unicode.IsLetter(lower) { return "#"