add routes
This commit is contained in:
309
handler/media.go
309
handler/media.go
@@ -4,179 +4,212 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"unicode"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
|
||||
"github.com/mozillazg/go-unidecode"
|
||||
)
|
||||
|
||||
func (c *Controller) Ping(w http.ResponseWriter, req *http.Request) {
|
||||
var orderExpr = map[string]interface{}{
|
||||
"random": gorm.Expr("random()"),
|
||||
"newest": "updated_at desc",
|
||||
"alphabeticalByName": "title",
|
||||
"alphabeticalByArtist": "album_artist.name",
|
||||
}
|
||||
|
||||
func indexOf(s string) rune {
|
||||
first := string(s[0])
|
||||
c := rune(unidecode.Unidecode(first)[0])
|
||||
if !unicode.IsLetter(c) {
|
||||
return '#'
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Controller) Ping(w http.ResponseWriter, r *http.Request) {
|
||||
sub := subsonic.NewResponse()
|
||||
respond(w, req, sub)
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetIndexes(w http.ResponseWriter, req *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, req, 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,
|
||||
CoverArt: cover.ID,
|
||||
}
|
||||
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, req *http.Request) {
|
||||
idStr := req.URL.Query().Get("id")
|
||||
if idStr == "" {
|
||||
respondError(w, req, 10, "please provide an `id` parameter")
|
||||
func (c *Controller) GetCoverArt(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getIntParam(r, "id")
|
||||
if err != nil {
|
||||
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.MusicDirectory = browseArtist(c.DB, &artist)
|
||||
respond(w, req, sub)
|
||||
return
|
||||
}
|
||||
var album db.Album
|
||||
c.DB.First(&album, id)
|
||||
if album.ID != 0 {
|
||||
sub.MusicDirectory = browseAlbum(c.DB, &album)
|
||||
respond(w, req, sub)
|
||||
return
|
||||
}
|
||||
respondError(w, req,
|
||||
70, fmt.Sprintf("directory with id `%d` was not found", id),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Controller) GetCoverArt(w http.ResponseWriter, req *http.Request) {
|
||||
idStr := req.URL.Query().Get("id")
|
||||
if idStr == "" {
|
||||
respondError(w, req, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
var cover db.Cover
|
||||
c.DB.First(&cover, id)
|
||||
w.Write(cover.Image)
|
||||
}
|
||||
|
||||
func (c *Controller) Stream(w http.ResponseWriter, req *http.Request) {
|
||||
idStr := req.URL.Query().Get("id")
|
||||
if idStr == "" {
|
||||
respondError(w, req, 10, "please provide an `id` parameter")
|
||||
func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
|
||||
var artists []*db.AlbumArtist
|
||||
c.DB.Find(&artists)
|
||||
var indexMap = make(map[rune]*subsonic.Index)
|
||||
var indexes []*subsonic.Index
|
||||
for _, artist := range artists {
|
||||
i := indexOf(artist.Name)
|
||||
index, ok := indexMap[i]
|
||||
if !ok {
|
||||
index = &subsonic.Index{
|
||||
Name: string(i),
|
||||
Artists: []*subsonic.Artist{},
|
||||
}
|
||||
indexMap[i] = index
|
||||
indexes = append(indexes, index)
|
||||
}
|
||||
index.Artists = append(index.Artists, &subsonic.Artist{
|
||||
ID: artist.ID,
|
||||
Name: artist.Name,
|
||||
})
|
||||
}
|
||||
sub := subsonic.NewResponse()
|
||||
sub.Artists = indexes
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetArtist(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getIntParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, r, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
var artist db.AlbumArtist
|
||||
c.DB.
|
||||
Preload("Albums").
|
||||
First(&artist, id)
|
||||
sub := subsonic.NewResponse()
|
||||
sub.Artist = &subsonic.Artist{
|
||||
ID: artist.ID,
|
||||
Name: artist.Name,
|
||||
}
|
||||
for _, album := range artist.Albums {
|
||||
sub.Artist.Albums = append(sub.Artist.Albums, &subsonic.Album{
|
||||
ID: album.ID,
|
||||
Name: album.Title,
|
||||
Created: album.CreatedAt,
|
||||
Artist: artist.Name,
|
||||
ArtistID: artist.ID,
|
||||
CoverID: album.ID,
|
||||
})
|
||||
}
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getIntParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, r, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
var album db.Album
|
||||
c.DB.
|
||||
Preload("AlbumArtist").
|
||||
Preload("Tracks").
|
||||
First(&album, id)
|
||||
sub := subsonic.NewResponse()
|
||||
sub.Album = &subsonic.Album{
|
||||
ID: album.ID,
|
||||
Name: album.Title,
|
||||
CoverID: album.ID,
|
||||
Created: album.CreatedAt,
|
||||
Artist: album.AlbumArtist.Name,
|
||||
}
|
||||
for _, track := range album.Tracks {
|
||||
sub.Album.Tracks = append(sub.Album.Tracks, &subsonic.Track{
|
||||
ID: track.ID,
|
||||
Title: track.Title,
|
||||
Artist: track.Artist, // track artist
|
||||
TrackNo: track.TrackNumber,
|
||||
ContentType: track.ContentType,
|
||||
Path: track.Path,
|
||||
Suffix: track.Suffix,
|
||||
Created: track.CreatedAt,
|
||||
Size: track.Size,
|
||||
Album: album.Title,
|
||||
AlbumID: album.ID,
|
||||
ArtistID: album.AlbumArtist.ID, // album artist
|
||||
CoverID: album.ID,
|
||||
Type: "music",
|
||||
})
|
||||
}
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetMusicFolders(w http.ResponseWriter, r *http.Request) {
|
||||
sub := subsonic.NewResponse()
|
||||
sub.MusicFolders = []*subsonic.MusicFolder{
|
||||
{ID: 0, Name: "music"},
|
||||
}
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
||||
listType := getStrParam(r, "type")
|
||||
if listType == "" {
|
||||
respondError(w, r, 10, "please provide a `type` parameter")
|
||||
return
|
||||
}
|
||||
orderType, ok := orderExpr[listType]
|
||||
if !ok {
|
||||
respondError(w, r, 10, fmt.Sprintf(
|
||||
"unknown value `%s` for parameter 'type'", listType,
|
||||
))
|
||||
return
|
||||
}
|
||||
size := getIntParamOr(r, "size", 10)
|
||||
var albums []*db.Album
|
||||
c.DB.
|
||||
Preload("AlbumArtist").
|
||||
Order(orderType).
|
||||
Limit(size).
|
||||
Find(&albums)
|
||||
sub := subsonic.NewResponse()
|
||||
for _, album := range albums {
|
||||
sub.Albums = append(sub.Albums, &subsonic.Album{
|
||||
ID: album.ID,
|
||||
Name: album.Title,
|
||||
Created: album.CreatedAt,
|
||||
CoverID: album.ID,
|
||||
Artist: album.AlbumArtist.Name,
|
||||
ArtistID: album.AlbumArtist.ID,
|
||||
})
|
||||
}
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getIntParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, r, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
var track db.Track
|
||||
c.DB.First(&track, id)
|
||||
if track.Path == "" {
|
||||
respondError(w, req, 70, fmt.Sprintf("media with id `%d` was not found", id))
|
||||
respondError(w, r, 70, fmt.Sprintf("media with id `%d` was not found", id))
|
||||
return
|
||||
}
|
||||
file, err := os.Open(track.Path)
|
||||
if err != nil {
|
||||
respondError(w, req, 0, fmt.Sprintf("error while streaming media: %v", err))
|
||||
respondError(w, r, 0, fmt.Sprintf("error while streaming media: %v", err))
|
||||
return
|
||||
}
|
||||
stat, _ := file.Stat()
|
||||
http.ServeContent(w, req, track.Path, stat.ModTime(), file)
|
||||
http.ServeContent(w, r, track.Path, stat.ModTime(), file)
|
||||
}
|
||||
|
||||
func (c *Controller) GetLicence(w http.ResponseWriter, req *http.Request) {
|
||||
func (c *Controller) GetLicence(w http.ResponseWriter, r *http.Request) {
|
||||
sub := subsonic.NewResponse()
|
||||
sub.Licence = &subsonic.Licence{
|
||||
Valid: true,
|
||||
}
|
||||
respond(w, req, sub)
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) NotFound(w http.ResponseWriter, req *http.Request) {
|
||||
respondError(w, req, 0, "unknown route")
|
||||
func (c *Controller) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, r, 0, "unknown route")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user