add getAlbumList2
This commit is contained in:
@@ -10,12 +10,6 @@ import (
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
)
|
||||
|
||||
var orderExpr = map[string]interface{}{
|
||||
"random": gorm.Expr("random()"),
|
||||
"newest": "updated_at desc",
|
||||
"alphabeticalByName": "title",
|
||||
}
|
||||
|
||||
func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
|
||||
var artists []*db.AlbumArtist
|
||||
c.DB.Find(&artists)
|
||||
@@ -110,14 +104,37 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *Controller) GetAlbumListTwo(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 {
|
||||
query := c.DB
|
||||
switch listType {
|
||||
case "alphabeticalByArtist":
|
||||
query = query.
|
||||
Joins("JOIN album_artists ON albums.album_artist_id=album_artists.id").
|
||||
Order("album_artists.name")
|
||||
case "alphabeticalByName":
|
||||
query = query.Order("title")
|
||||
case "byYear":
|
||||
query = query.Order("year")
|
||||
case "frequent":
|
||||
user := r.Context().Value(contextUserKey).(*db.User)
|
||||
query = query.
|
||||
Joins("JOIN plays ON albums.id=plays.album_id AND plays.user_id=?", user.ID).
|
||||
Order("plays.count desc")
|
||||
case "newest":
|
||||
query = query.Order("updated_at desc")
|
||||
case "random":
|
||||
query = query.Order(gorm.Expr("random()"))
|
||||
case "recent":
|
||||
user := r.Context().Value(contextUserKey).(*db.User)
|
||||
query = query.
|
||||
Joins("JOIN plays ON albums.id=plays.album_id AND plays.user_id=?", user.ID).
|
||||
Order("plays.time desc")
|
||||
default:
|
||||
respondError(w, r, 10, fmt.Sprintf(
|
||||
"unknown value `%s` for parameter 'type'", listType,
|
||||
))
|
||||
@@ -125,10 +142,9 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
size := getIntParamOr(r, "size", 10)
|
||||
var albums []*db.Album
|
||||
c.DB.
|
||||
Preload("AlbumArtist").
|
||||
Order(orderType).
|
||||
query.
|
||||
Limit(size).
|
||||
Preload("AlbumArtist").
|
||||
Find(&albums)
|
||||
sub := subsonic.NewResponse()
|
||||
for _, album := range albums {
|
||||
|
||||
@@ -30,7 +30,9 @@ func (c *Controller) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
var track db.Track
|
||||
c.DB.First(&track, id)
|
||||
c.DB.
|
||||
Preload("Album").
|
||||
First(&track, id)
|
||||
if track.Path == "" {
|
||||
respondError(w, r, 70, fmt.Sprintf("media with id `%d` was not found", id))
|
||||
return
|
||||
@@ -42,6 +44,17 @@ func (c *Controller) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
stat, _ := file.Stat()
|
||||
http.ServeContent(w, r, track.Path, stat.ModTime(), file)
|
||||
//
|
||||
// after we've served the file, mark the album as played
|
||||
user := r.Context().Value(contextUserKey).(*db.User)
|
||||
play := db.Play{
|
||||
AlbumID: track.Album.ID,
|
||||
UserID: user.ID,
|
||||
}
|
||||
c.DB.Where(play).First(&play)
|
||||
play.Time = time.Now() // for getAlbumList?type=recent
|
||||
play.Count++ // for getAlbumList?type=frequent
|
||||
c.DB.Save(&play)
|
||||
}
|
||||
|
||||
func (c *Controller) GetCoverArt(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -75,12 +88,7 @@ func (c *Controller) Scrobble(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
// fetch user to get lastfm session
|
||||
username := getStrParam(r, "u")
|
||||
user := c.GetUserFromName(username)
|
||||
if user == nil {
|
||||
respondError(w, r, 10, "could not find a user with that name")
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(contextUserKey).(*db.User)
|
||||
if user.LastFMSession == "" {
|
||||
respondError(w, r, 0, fmt.Sprintf("no last.fm session for this user: %v", err))
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -75,7 +76,8 @@ func (c *Controller) WithValidSubsonicArgs(next http.HandlerFunc) http.HandlerFu
|
||||
respondError(w, r, 40, "invalid password")
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
withUser := context.WithValue(r.Context(), contextUserKey, user)
|
||||
next.ServeHTTP(w, r.WithContext(withUser))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user