Replaced Beego logging

This commit is contained in:
Deluan
2020-01-08 20:45:07 -05:00
committed by Deluan Quintão
parent 6eda38a951
commit 84d69a4f41
28 changed files with 559 additions and 282 deletions
+7 -7
View File
@@ -4,9 +4,9 @@ import (
"errors"
"net/http"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/utils"
)
@@ -42,7 +42,7 @@ func (c *AlbumListController) getAlbumList(r *http.Request) (engine.Entries, err
listFunc, found := c.listFunctions[typ]
if !found {
beego.Error("albumList type", typ, "not implemented!")
log.Error(r, "albumList type not implemented", "type", typ)
return nil, errors.New("Not implemented!")
}
@@ -51,7 +51,7 @@ func (c *AlbumListController) getAlbumList(r *http.Request) (engine.Entries, err
albums, err := listFunc(offset, size)
if err != nil {
beego.Error("Error retrieving albums:", err)
log.Error(r, "Error retrieving albums", "error", err)
return nil, errors.New("Internal Error")
}
@@ -83,7 +83,7 @@ func (c *AlbumListController) GetAlbumList2(w http.ResponseWriter, r *http.Reque
func (c *AlbumListController) GetStarred(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
albums, mediaFiles, err := c.listGen.GetAllStarred()
if err != nil {
beego.Error("Error retrieving starred media:", err)
log.Error(r, "Error retrieving starred media", "error", err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -97,7 +97,7 @@ func (c *AlbumListController) GetStarred(w http.ResponseWriter, r *http.Request)
func (c *AlbumListController) GetStarred2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
albums, mediaFiles, err := c.listGen.GetAllStarred()
if err != nil {
beego.Error("Error retrieving starred media:", err)
log.Error(r, "Error retrieving starred media", "error", err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -111,7 +111,7 @@ func (c *AlbumListController) GetStarred2(w http.ResponseWriter, r *http.Request
func (c *AlbumListController) GetNowPlaying(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
npInfos, err := c.listGen.GetNowPlaying()
if err != nil {
beego.Error("Error retrieving now playing list:", err)
log.Error(r, "Error retrieving now playing list", "error", err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -133,7 +133,7 @@ func (c *AlbumListController) GetRandomSongs(w http.ResponseWriter, r *http.Requ
songs, err := c.listGen.GetRandomSongs(size)
if err != nil {
beego.Error("Error retrieving random songs:", err)
log.Error(r, "Error retrieving random songs", "error", err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
+45 -44
View File
@@ -1,46 +1,47 @@
package api_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/astaxie/beego"
)
const (
testUser = "deluan"
testPassword = "wordpass"
testClient = "test"
testVersion = "1.0.0"
)
func AddParams(endpoint string, params ...string) string {
url := fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s&f=json", endpoint, testUser, testPassword, testClient, testVersion)
if len(params) > 0 {
url = url + "&" + strings.Join(params, "&")
}
return url
}
func Get(url string, testCase string) (*http.Request, *httptest.ResponseRecorder) {
r, _ := http.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
return r, w
}
func GetWithHeader(url string, header, value, testCase string) (*http.Request, *httptest.ResponseRecorder) {
r, _ := http.NewRequest("GET", url, nil)
r.Header.Add(header, value)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
return r, w
}
//
//import (
// "fmt"
// "net/http"
// "net/http/httptest"
// "strings"
//
// "github.com/astaxie/beego"
//)
//
//const (
// testUser = "deluan"
// testPassword = "wordpass"
// testClient = "test"
// testVersion = "1.0.0"
//)
//
//func AddParams(endpoint string, params ...string) string {
// url := fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s&f=json", endpoint, testUser, testPassword, testClient, testVersion)
// if len(params) > 0 {
// url = url + "&" + strings.Join(params, "&")
// }
// return url
//}
//
//func Get(url string, testCase string) (*http.Request, *httptest.ResponseRecorder) {
// r, _ := http.NewRequest("GET", url, nil)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
//
// log.Debug(r, "testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
//
// return r, w
//}
//
//func GetWithHeader(url string, header, value, testCase string) (*http.Request, *httptest.ResponseRecorder) {
// r, _ := http.NewRequest("GET", url, nil)
// r.Header.Add(header, value)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
//
// log.Debug(r, "testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
//
// return r, w
//}
+16 -16
View File
@@ -5,11 +5,11 @@ import (
"net/http"
"time"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/utils"
)
@@ -33,10 +33,10 @@ func (c *BrowsingController) GetMusicFolders(w http.ResponseWriter, r *http.Requ
return response, nil
}
func (c *BrowsingController) getArtistIndex(ifModifiedSince time.Time) (*responses.Indexes, error) {
func (c *BrowsingController) getArtistIndex(r *http.Request, ifModifiedSince time.Time) (*responses.Indexes, error) {
indexes, lastModified, err := c.browser.Indexes(ifModifiedSince)
if err != nil {
beego.Error("Error retrieving Indexes:", err)
log.Error(r, "Error retrieving Indexes", "error", err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -61,7 +61,7 @@ func (c *BrowsingController) getArtistIndex(ifModifiedSince time.Time) (*respons
func (c *BrowsingController) GetIndexes(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ifModifiedSince := ParamTime(r, "ifModifiedSince", time.Time{})
res, err := c.getArtistIndex(ifModifiedSince)
res, err := c.getArtistIndex(r, ifModifiedSince)
if err != nil {
return nil, err
}
@@ -72,7 +72,7 @@ func (c *BrowsingController) GetIndexes(w http.ResponseWriter, r *http.Request)
}
func (c *BrowsingController) GetArtists(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
res, err := c.getArtistIndex(time.Time{})
res, err := c.getArtistIndex(r, time.Time{})
if err != nil {
return nil, err
}
@@ -84,13 +84,13 @@ func (c *BrowsingController) GetArtists(w http.ResponseWriter, r *http.Request)
func (c *BrowsingController) GetMusicDirectory(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := ParamString(r, "id")
dir, err := c.browser.Directory(id)
dir, err := c.browser.Directory(r.Context(), id)
switch {
case err == domain.ErrNotFound:
beego.Error("Requested Id", id, "not found:", err)
log.Error(r, "Requested Id not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
case err != nil:
beego.Error(err)
log.Error(err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -101,13 +101,13 @@ func (c *BrowsingController) GetMusicDirectory(w http.ResponseWriter, r *http.Re
func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := ParamString(r, "id")
dir, err := c.browser.Artist(id)
dir, err := c.browser.Artist(r.Context(), id)
switch {
case err == domain.ErrNotFound:
beego.Error("Requested ArtistId", id, "not found:", err)
log.Error(r, "Requested ArtistId not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Artist not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -118,13 +118,13 @@ func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (
func (c *BrowsingController) GetAlbum(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := ParamString(r, "id")
dir, err := c.browser.Album(id)
dir, err := c.browser.Album(r.Context(), id)
switch {
case err == domain.ErrNotFound:
beego.Error("Requested AlbumId", id, "not found:", err)
log.Error(r, "Requested Id not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Album not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -138,10 +138,10 @@ func (c *BrowsingController) GetSong(w http.ResponseWriter, r *http.Request) (*r
song, err := c.browser.GetSong(id)
switch {
case err == domain.ErrNotFound:
beego.Error("Requested Id", id, "not found:", err)
log.Error(r, "Requested Id not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Song not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
+21 -22
View File
@@ -1,14 +1,13 @@
package api
import (
"fmt"
"net/http"
"time"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
)
type MediaAnnotationController struct {
@@ -33,15 +32,15 @@ func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Req
return nil, err
}
beego.Debug("Setting rating", rating, "for id", id)
err = c.ratings.SetRating(id, rating)
log.Debug(r, "Setting rating", "rating", rating, "id", id)
err = c.ratings.SetRating(r.Context(), id, rating)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -50,7 +49,7 @@ func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Req
func (c *MediaAnnotationController) getIds(r *http.Request) ([]string, error) {
ids := ParamStrings(r, "id")
albumIds := ParamStrings(r,"albumId")
albumIds := ParamStrings(r, "albumId")
if len(ids) == 0 && len(albumIds) == 0 {
return nil, NewError(responses.ErrorMissingParameter, "Required id parameter is missing")
@@ -64,14 +63,14 @@ func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request)
if err != nil {
return nil, err
}
beego.Debug("Starring ids:", ids)
err = c.ratings.SetStar(true, ids...)
log.Debug(r, "Starring items", "ids", ids)
err = c.ratings.SetStar(r.Context(), true, ids...)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -83,14 +82,14 @@ func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Reques
if err != nil {
return nil, err
}
beego.Debug("Unstarring ids:", ids)
err = c.ratings.SetStar(false, ids...)
log.Debug(r, "Unstarring items", "ids", ids)
err = c.ratings.SetStar(r.Context(), false, ids...)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -111,7 +110,7 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Requ
playerName := ParamString(r, "c")
username := ParamString(r, "u")
beego.Debug("Scrobbling ids:", ids, "times:", times, "submission:", submission)
log.Debug(r, "Scrobbling tracks", "ids", ids, "times", times, "submission", submission)
for i, id := range ids {
var t time.Time
if len(times) > 0 {
@@ -120,19 +119,19 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Requ
t = time.Now()
}
if submission {
mf, err := c.scrobbler.Register(playerId, id, t)
mf, err := c.scrobbler.Register(r.Context(), playerId, id, t)
if err != nil {
beego.Error("Error scrobbling", id, "-", err)
log.Error(r, "Error scrobbling track", "id", id, err)
continue
}
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, t))
log.Info(r, "Scrobbled", "id", id, "title", mf.Title, "timestamp", t)
} else {
mf, err := c.scrobbler.NowPlaying(playerId, playerName, id, username)
mf, err := c.scrobbler.NowPlaying(r.Context(), playerId, playerName, id, username)
if err != nil {
beego.Error("Error setting", id, "as current song:", err)
log.Error(r, "Error setting current song", "id", id, err)
continue
}
beego.Info(fmt.Sprintf(`Now Playing (%s) "%s" at %v`, id, mf.Title, t))
log.Info(r, "Now Playing", "id", id, "title", mf.Title, "timestamp", t)
}
}
return NewEmpty(), nil
+4 -4
View File
@@ -5,10 +5,10 @@ import (
"net/http"
"os"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
)
type MediaRetrievalController struct {
@@ -23,7 +23,7 @@ func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Requ
var f *os.File
f, err := os.Open("static/itunes.png")
if err != nil {
beego.Error(err, "Image not found")
log.Error(r, "Image not found", err)
return nil, NewError(responses.ErrorDataNotFound, "Avatar image not found")
}
defer f.Close()
@@ -43,10 +43,10 @@ func (c *MediaRetrievalController) GetCoverArt(w http.ResponseWriter, r *http.Re
switch {
case err == domain.ErrNotFound:
beego.Error(err, "Id:", id)
log.Error(r, err.Error(), "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Cover not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
+2 -1
View File
@@ -1,4 +1,5 @@
package api_test
//
//import (
// "fmt"
@@ -20,7 +21,7 @@ package api_test
// r, _ := http.NewRequest("GET", url, nil)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
// beego.Debug("testing TestGetCoverArt", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
// log.Debug(r, "testing TestGetCoverArt", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
// return r, w
//}
//
+4 -4
View File
@@ -8,9 +8,9 @@ import (
"net/http"
"strings"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
)
func checkRequiredParameters(next http.Handler) http.Handler {
@@ -20,14 +20,14 @@ func checkRequiredParameters(next http.Handler) http.Handler {
for _, p := range requiredParameters {
if ParamString(r, p) == "" {
msg := fmt.Sprintf(`Missing required parameter "%s"`, p)
beego.Warn(msg)
log.Warn(r, msg)
SendError(w, r, NewError(responses.ErrorMissingParameter, msg))
return
}
}
if ParamString(r, "p") == "" && (ParamString(r, "s") == "" || ParamString(r, "t") == "") {
beego.Warn("Missing authentication information")
log.Warn(r, "Missing authentication information")
}
ctx := r.Context()
ctx = context.WithValue(ctx, "user", ParamString(r, "u"))
@@ -63,7 +63,7 @@ func authenticate(next http.Handler) http.Handler {
}
if user != conf.Sonic.User || !valid {
beego.Warn(fmt.Sprintf(`Invalid login for user "%s"`, user))
log.Warn(r, "Invalid login", "user", user)
SendError(w, r, NewError(responses.ErrorAuthenticationFail))
return
}
+13 -13
View File
@@ -4,10 +4,10 @@ import (
"fmt"
"net/http"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
)
type PlaylistsController struct {
@@ -21,7 +21,7 @@ func NewPlaylistsController(pls engine.Playlists) *PlaylistsController {
func (c *PlaylistsController) GetPlaylists(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
allPls, err := c.pls.GetAll()
if err != nil {
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal error")
}
playlists := make([]responses.Playlist, len(allPls))
@@ -47,10 +47,10 @@ func (c *PlaylistsController) GetPlaylist(w http.ResponseWriter, r *http.Request
pinfo, err := c.pls.Get(id)
switch {
case err == domain.ErrNotFound:
beego.Error(err, "Id:", id)
log.Error(r, err.Error(), "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
case err != nil:
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
@@ -68,9 +68,9 @@ func (c *PlaylistsController) CreatePlaylist(w http.ResponseWriter, r *http.Requ
if err != nil {
return nil, err
}
err = c.pls.Create(name, songIds)
err = c.pls.Create(r.Context(), name, songIds)
if err != nil {
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
return NewEmpty(), nil
@@ -81,9 +81,9 @@ func (c *PlaylistsController) DeletePlaylist(w http.ResponseWriter, r *http.Requ
if err != nil {
return nil, err
}
err = c.pls.Delete(id)
err = c.pls.Delete(r.Context(), id)
if err != nil {
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
return NewEmpty(), nil
@@ -103,16 +103,16 @@ func (c *PlaylistsController) UpdatePlaylist(w http.ResponseWriter, r *http.Requ
pname = &s
}
beego.Info(fmt.Sprintf("Updating playlist with id '%s'", playlistId))
log.Info(r, "Updating playlist", "id", playlistId)
if pname != nil {
beego.Debug(fmt.Sprintf("-- New Name: '%s'", *pname))
log.Debug(r, fmt.Sprintf("-- New Name: '%s'", *pname))
}
beego.Debug(fmt.Sprintf("-- Adding: '%v'", songsToAdd))
beego.Debug(fmt.Sprintf("-- Removing: '%v'", songIndexesToRemove))
log.Debug(r, fmt.Sprintf("-- Adding: '%v'", songsToAdd))
log.Debug(r, fmt.Sprintf("-- Removing: '%v'", songIndexesToRemove))
err = c.pls.Update(playlistId, pname, songsToAdd, songIndexesToRemove)
if err != nil {
beego.Error(err)
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
return NewEmpty(), nil
+11 -11
View File
@@ -4,9 +4,9 @@ import (
"fmt"
"net/http"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
)
type SearchingController struct {
@@ -39,21 +39,21 @@ func (c *SearchingController) getParams(r *http.Request) error {
return nil
}
func (c *SearchingController) searchAll() (engine.Entries, engine.Entries, engine.Entries) {
as, err := c.search.SearchArtist(c.query, c.artistOffset, c.artistCount)
func (c *SearchingController) searchAll(r *http.Request) (engine.Entries, engine.Entries, engine.Entries) {
as, err := c.search.SearchArtist(r.Context(), c.query, c.artistOffset, c.artistCount)
if err != nil {
beego.Error("Error searching for Artists:", err)
log.Error(r, "Error searching for Artists", err)
}
als, err := c.search.SearchAlbum(c.query, c.albumOffset, c.albumCount)
als, err := c.search.SearchAlbum(r.Context(), c.query, c.albumOffset, c.albumCount)
if err != nil {
beego.Error("Error searching for Albums:", err)
log.Error(r, "Error searching for Albums", err)
}
mfs, err := c.search.SearchSong(c.query, c.songOffset, c.songCount)
mfs, err := c.search.SearchSong(r.Context(), c.query, c.songOffset, c.songCount)
if err != nil {
beego.Error("Error searching for MediaFiles:", err)
log.Error(r, "Error searching for MediaFiles", err)
}
beego.Debug(fmt.Sprintf("Searching for [%s] resulted in %d songs, %d albums and %d artists", c.query, len(mfs), len(als), len(as)))
log.Debug(r, fmt.Sprintf("Search resulted in %d songs, %d albums and %d artists", len(mfs), len(als), len(as)), "query", c.query)
return mfs, als, as
}
@@ -62,7 +62,7 @@ func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*
if err != nil {
return nil, err
}
mfs, als, as := c.searchAll()
mfs, als, as := c.searchAll(r)
response := NewEmpty()
searchResult2 := &responses.SearchResult2{}
@@ -81,7 +81,7 @@ func (c *SearchingController) Search3(w http.ResponseWriter, r *http.Request) (*
if err != nil {
return nil, err
}
mfs, als, as := c.searchAll()
mfs, als, as := c.searchAll(r)
response := NewEmpty()
searchResult3 := &responses.SearchResult3{}
+12 -13
View File
@@ -3,10 +3,10 @@ package api
import (
"net/http"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/utils"
)
@@ -29,10 +29,10 @@ func (c *StreamController) Prepare(r *http.Request) (err error) {
c.mf, err = c.repo.Get(c.id)
switch {
case err == domain.ErrNotFound:
beego.Error("MediaFile", c.id, "not found!")
log.Error(r, "Mediafile not found", "id", c.id)
return NewError(responses.ErrorDataNotFound)
case err != nil:
beego.Error("Error reading mediafile", c.id, "from the database", ":", err)
log.Error(r, "Error reading mediafile from DB", "id", c.id, err)
return NewError(responses.ErrorGeneric, "Internal error")
}
return nil
@@ -48,8 +48,7 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
maxBitRate := ParamInt(r, "maxBitRate", 0)
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
beego.Debug("Streaming file", c.id, ":", c.mf.Path)
beego.Debug("Bitrate", c.mf.BitRate, "MaxBitRate", maxBitRate)
log.Debug(r, "Streaming file", "id", c.id, "path", c.mf.Path, "bitrate", c.mf.BitRate, "maxBitRate", maxBitRate)
// TODO Send proper estimated content-length
//contentLength := c.mf.Size
@@ -64,16 +63,16 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
h.Set("Pragma", "public")
if r.Method == "HEAD" {
beego.Debug("Just a HEAD. Not streaming", c.mf.Path)
log.Debug(r, "Just a HEAD. Not streaming", "path", c.mf.Path)
return nil, nil
}
err = engine.Stream(c.mf.Path, c.mf.BitRate, maxBitRate, w)
err = engine.Stream(r.Context(), c.mf.Path, c.mf.BitRate, maxBitRate, w)
if err != nil {
beego.Error("Error streaming file", c.id, ":", err)
log.Error(r, "Error streaming file", "id", c.id, err)
}
beego.Debug("Finished streaming of", c.mf.Path)
log.Debug(r, "Finished streaming", "path", c.mf.Path)
return nil, nil
}
@@ -82,14 +81,14 @@ func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*re
if err != nil {
return nil, err
}
beego.Debug("Sending file", c.mf.Path)
log.Debug(r, "Sending file", "path", c.mf.Path)
err = engine.Stream(c.mf.Path, 0, 0, w)
err = engine.Stream(r.Context(), c.mf.Path, 0, 0, w)
if err != nil {
beego.Error("Error downloading file", c.mf.Path, ":", err.Error())
log.Error(r, "Error downloading file", "path", c.mf.Path, err)
}
beego.Debug("Finished sending", c.mf.Path)
log.Debug(r, "Finished sending", "path", c.mf.Path)
return nil, nil
}
+2 -1
View File
@@ -1,4 +1,5 @@
package api_test
//
//import (
// "fmt"
@@ -20,7 +21,7 @@ package api_test
// r, _ := http.NewRequest("GET", url, nil)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
// beego.Debug("testing TestStream", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
// log.Debug(r, "testing TestStream", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
// return r, w
//}
//
+2 -1
View File
@@ -1,4 +1,5 @@
package api_test
//
//import (
// "encoding/xml"
@@ -92,7 +93,7 @@ package api_test
//
//func TestContext(t *testing.T) {
// tests.Init(t, false)
// beego.Router("/rest/mocktest", &mockController{})
// log.Router(r, "/rest/mocktest", &mockController{})
//
// Convey("Subject: Context", t, func() {
// _, w := GetWithHeader("/rest/mocktest?u=deluan&p=wordpass&c=testClient&v=1.0.0", "X-Request-Id", "123123", "TestContext")