add routes
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
@@ -15,38 +16,60 @@ type Controller struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func getStrParam(r *http.Request, key string) string {
|
||||
return r.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func getIntParam(r *http.Request, key string) (int, error) {
|
||||
strVal := r.URL.Query().Get(key)
|
||||
if strVal == "" {
|
||||
return 0, fmt.Errorf("no param with key `%s`", key)
|
||||
}
|
||||
val, err := strconv.Atoi(strVal)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("not an int `%s`", strVal)
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func getIntParamOr(r *http.Request, key string, or int) int {
|
||||
val, err := getIntParam(r, key)
|
||||
if err != nil {
|
||||
return or
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func respondRaw(w http.ResponseWriter, r *http.Request, code int, sub *subsonic.Response) {
|
||||
format := r.URL.Query().Get("f")
|
||||
switch format {
|
||||
res := subsonic.MetaResponse{
|
||||
Response: sub,
|
||||
}
|
||||
switch r.URL.Query().Get("f") {
|
||||
case "json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
data, err := json.Marshal(sub)
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
}
|
||||
w.Write([]byte(`{"subsonic-response":`))
|
||||
w.Write(data)
|
||||
w.Write([]byte("}"))
|
||||
fmt.Println("THE JSON", string(data))
|
||||
case "jsonp":
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
data, err := json.Marshal(sub)
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
}
|
||||
callback := r.URL.Query().Get("callback")
|
||||
w.Write([]byte(fmt.Sprintf(`%s({"subsonic-response":`, callback)))
|
||||
w.Write([]byte(callback))
|
||||
w.Write([]byte("("))
|
||||
w.Write(data)
|
||||
w.Write([]byte("});"))
|
||||
fmt.Println("THE JSONP", string(data))
|
||||
w.Write([]byte(");"))
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
data, err := xml.Marshal(sub)
|
||||
data, err := xml.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to xml: %v\n", err)
|
||||
}
|
||||
w.Write(data)
|
||||
fmt.Println("THE XML", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -15,14 +15,14 @@ var requiredParameters = []string{
|
||||
"u", "v", "c",
|
||||
}
|
||||
|
||||
func checkCredentialsNewWay(password, token, salt string) bool {
|
||||
func checkCredentialsToken(password, token, salt string) bool {
|
||||
toHash := fmt.Sprintf("%s%s", password, salt)
|
||||
hash := md5.Sum([]byte(toHash))
|
||||
expToken := hex.EncodeToString(hash[:])
|
||||
return token == expToken
|
||||
}
|
||||
|
||||
func checkCredentialsOldWay(password, givenPassword string) bool {
|
||||
func checkCredentialsBasic(password, givenPassword string) bool {
|
||||
if givenPassword[:4] == "enc:" {
|
||||
bytes, _ := hex.DecodeString(givenPassword[4:])
|
||||
givenPassword = string(bytes)
|
||||
@@ -71,9 +71,9 @@ func (c *Controller) CheckParameters(next http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
var credsOk bool
|
||||
if tokenAuth {
|
||||
credsOk = checkCredentialsNewWay(user.Password, token, salt)
|
||||
credsOk = checkCredentialsToken(user.Password, token, salt)
|
||||
} else {
|
||||
credsOk = checkCredentialsOldWay(user.Password, password)
|
||||
credsOk = checkCredentialsBasic(user.Password, password)
|
||||
}
|
||||
if !credsOk {
|
||||
respondError(w, r, 40, "invalid password")
|
||||
|
||||
119
handler/oldfolderbase
Normal file
119
handler/oldfolderbase
Normal file
@@ -0,0 +1,119 @@
|
||||
|
||||
// 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),
|
||||
// )
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user