add working GetMusicDirectory

This commit is contained in:
sentriz
2019-05-15 11:38:49 +01:00
parent bd95806a23
commit 4a5b36ccee
3 changed files with 29 additions and 130 deletions

View File

@@ -1,7 +1,6 @@
package handler
import (
"fmt"
"net/http"
"github.com/sentriz/gonic/db"
@@ -13,7 +12,6 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
// for this, so we're going to return root directories as "artists"
var folders []*db.Folder
c.DB.Where("parent_id = ?", 1).Find(&folders)
fmt.Println(folders, "++++++++")
var indexMap = make(map[rune]*subsonic.Index)
var indexes []*subsonic.Index
for _, folder := range folders {
@@ -46,20 +44,18 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
respondError(w, r, 10, "please provide an `id` parameter")
return
}
var folders []*db.Folder
c.DB.Where("parent_id = ?", id).Find(&folders)
if len(folders) == 0 {
respondError(w, r, 40, "couldn't find any directories")
return
}
sub := subsonic.NewResponse()
var cFolder db.Folder
c.DB.First(&cFolder, id)
sub := subsonic.NewResponse()
sub.Directory = &subsonic.Directory{
ID: cFolder.ID,
Parent: cFolder.ParentID,
Name: cFolder.Name,
}
var folders []*db.Folder
c.DB.
Where("parent_id = ?", id).
Find(&folders)
for _, folder := range folders {
sub.Directory.Children = append(sub.Directory.Children, &subsonic.Child{
Parent: cFolder.ID,
@@ -69,5 +65,26 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
CoverID: folder.CoverID,
})
}
var tracks []*db.Track
c.DB.
Where("folder_id = ?", id).
Preload("Album").
Find(&tracks)
for _, track := range tracks {
sub.Directory.Children = append(sub.Directory.Children, &subsonic.Child{
Parent: cFolder.ID,
IsDir: false,
Title: track.Title,
Album: track.Album.Title,
Artist: track.Artist,
Bitrate: track.Bitrate,
ContentType: track.ContentType,
CoverID: cFolder.CoverID,
Duration: 0,
Path: track.Path,
Size: track.Size,
Track: track.TrackNumber,
})
}
respond(w, r, sub)
}

View File

@@ -1,118 +0,0 @@
// func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
// var artists []*db.Artist
// c.DB.Find(&artists)
// indexMap := make(map[byte]*subsonic.Index)
// for _, artist := range artists {
// first := artist.Name[0]
// if !unicode.IsLetter(rune(first)) {
// first = 0x23 // '#'
// }
// _, ok := indexMap[first]
// if !ok {
// indexMap[first] = &subsonic.Index{
// Name: string(first),
// Artists: []*subsonic.Artist{},
// }
// }
// indexMap[first].Artists = append(
// indexMap[first].Artists,
// &subsonic.Artist{
// ID: artist.ID,
// Name: artist.Name,
// },
// )
// }
// indexes := []*subsonic.Index{}
// for _, v := range indexMap {
// indexes = append(indexes, v)
// }
// sub := subsonic.NewResponse()
// sub.Indexes = &subsonic.Indexes{
// Index: &indexes,
// }
// respond(w, r, sub)
// }
// func browseArtist(c *gorm.DB, artist *db.Artist) *subsonic.Directory {
// var cover db.Cover
// var dir subsonic.Directory
// dir.Name = artist.Name
// dir.ID = artist.ID
// dir.Parent = 0
// var albums []*db.Album
// c.Model(artist).Related(&albums)
// dir.Children = make([]subsonic.Child, len(albums))
// for i, album := range albums {
// c.Model(album).Related(&cover)
// dir.Children[i] = subsonic.Child{
// Artist: artist.Name,
// ID: album.ID,
// IsDir: true,
// Parent: artist.ID,
// Title: album.Title,
// CoverID: cover.AlbumID,
// }
// cover = db.Cover{}
// }
// return &dir
// }
// func browseAlbum(c *gorm.DB, album *db.Album) *subsonic.Directory {
// var artist db.Artist
// c.Model(album).Related(&artist)
// var tracks []*db.Track
// c.Model(album).Related(&tracks)
// var cover db.Cover
// c.Model(album).Related(&cover)
// var dir subsonic.Directory
// dir.Name = album.Title
// dir.ID = album.ID
// dir.Parent = artist.ID
// dir.Children = make([]subsonic.Child, len(tracks))
// for i, track := range tracks {
// dir.Children[i] = subsonic.Child{
// ID: track.ID,
// Title: track.Title,
// Parent: album.ID,
// Artist: artist.Name,
// ArtistID: artist.ID,
// Album: album.Title,
// AlbumID: album.ID,
// IsDir: false,
// Path: track.Path,
// CoverArt: cover.ID,
// ContentType: track.ContentType,
// Suffix: track.Suffix,
// Duration: 0,
// }
// }
// return &dir
// }
// func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
// idStr := r.URL.Query().Get("id")
// if idStr == "" {
// respondError(w, r, 10, "please provide an `id` parameter")
// return
// }
// id, _ := strconv.Atoi(idStr)
// sub := subsonic.NewResponse()
// var artist db.Artist
// c.DB.First(&artist, id)
// if artist.ID != 0 {
// sub.Directory = browseArtist(c.DB, &artist)
// respond(w, r, sub)
// return
// }
// var album db.Album
// c.DB.First(&album, id)
// if album.ID != 0 {
// sub.Directory = browseAlbum(c.DB, &album)
// respond(w, r, sub)
// return
// }
// respondError(w, r,
// 70, fmt.Sprintf("directory with id `%d` was not found", id),
// )
// }

View File

@@ -29,7 +29,7 @@ type Track struct {
Created time.Time `xml:"created,attr" json:"created"`
Duration int `xml:"duration,attr" json:"duration"`
Genre string `xml:"genre,attr" json:"genre"`
BitRate int `xml:"bitRate,attr" json:"bitRate"`
Bitrate int `xml:"bitRate,attr" json:"bitRate"`
Size int `xml:"size,attr" json:"size"`
Suffix string `xml:"suffix,attr" json:"suffix"`
ContentType string `xml:"contentType,attr" json:"contentType"`
@@ -83,8 +83,8 @@ type Child struct {
Size int `xml:"size,attr,omitempty" json:"size,omitempty"`
ContentType string `xml:"contentType,attr,omitempty" json:"contentType,omitempty"`
Suffix string `xml:"suffix,attr,omitempty" json:"suffix,omitempty"`
Duration int `xml:"duration,attr,omitempty" json:"duration"`
BitRate int `xml:"bitRate,attr,omitempty" json:"bitrate,omitempty"`
Duration int `xml:"duration,attr,omitempty" json:"duration,omitempty"`
Bitrate int `xml:"bitRate,attr,omitempty" json:"bitrate,omitempty"`
Path string `xml:"path,attr,omitempty" json:"path,omitempty"`
}