Skip to content

🎧 Your Personal Streaming Service

Introduced helper methods for parsing/biding request parameters

Deluan28bef732cf9b
diff --git a/.gitignore b/.gitignore
index 4c8f08beb..345167000 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@
static/Jamstash
devDb
/tmp
-.vendor
\ No newline at end of file
+.vendor
+wiki
diff --git a/api/base_api_controller.go b/api/base_api_controller.go
index 780b45bef..bb01316dc 100644
--- a/api/base_api_controller.go
+++ b/api/base_api_controller.go
@@ -5,6 +5,8 @@ import (
"fmt"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
+ "github.com/deluan/gosonic/utils"
+ "time"
)
type BaseAPIController struct{ beego.Controller }
@@ -13,7 +15,7 @@ func (c *BaseAPIController) NewEmpty() responses.Subsonic {
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
}
-func (c *BaseAPIController) GetParameter(param string, msg string) string {
+func (c *BaseAPIController) RequiredParamString(param string, msg string) string {
p := c.Input().Get(param)
if p == "" {
c.SendError(responses.ERROR_MISSING_PARAMETER, msg)
@@ -21,6 +23,18 @@ func (c *BaseAPIController) GetParameter(param string, msg string) string {
return p
}
+func (c *BaseAPIController) ParamTime(param string) time.Time {
+ var value int64
+ c.Ctx.Input.Bind(&value, param)
+ return utils.ToTime(value)
+}
+
+func (c *BaseAPIController) ParamInt(param string) int {
+ var value int
+ c.Ctx.Input.Bind(&value, param)
+ return value
+}
+
func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
response := responses.Subsonic{Version: beego.AppConfig.String("apiVersion"), Status: "fail"}
var msg string
diff --git a/api/get_album_list.go b/api/get_album_list.go
index 833234e81..3b26240e6 100644
--- a/api/get_album_list.go
+++ b/api/get_album_list.go
@@ -27,7 +27,7 @@ func (c *GetAlbumListController) Prepare() {
}
func (c *GetAlbumListController) Get() {
- typ := c.GetParameter("type", "Required string parameter 'type' is not present")
+ typ := c.RequiredParamString("type", "Required string parameter 'type' is not present")
qo, found := c.types[typ]
if !found {
@@ -35,10 +35,8 @@ func (c *GetAlbumListController) Get() {
c.SendError(responses.ERROR_GENERIC, "Not implemented!")
}
- qo.Size = 10
- c.Ctx.Input.Bind(&qo.Size, "size")
- qo.Size = utils.MinInt(qo.Size, 500)
- c.Ctx.Input.Bind(&qo.Offset, "offset")
+ qo.Size = utils.MinInt(c.ParamInt("size"), 500)
+ qo.Offset = c.ParamInt("offset")
albums, err := c.albumRepo.GetAll(qo)
if err != nil {
diff --git a/api/get_cover_art.go b/api/get_cover_art.go
index 9ba53b183..98236d4aa 100644
--- a/api/get_cover_art.go
+++ b/api/get_cover_art.go
@@ -22,7 +22,7 @@ func (c *GetCoverArtController) Prepare() {
// TODO accept size parameter
func (c *GetCoverArtController) Get() {
- id := c.GetParameter("id", "id parameter required")
+ id := c.RequiredParamString("id", "id parameter required")
mf, err := c.repo.Get(id)
if err != nil {
diff --git a/api/get_indexes.go b/api/get_indexes.go
index 6504537eb..ac3c95a09 100644
--- a/api/get_indexes.go
+++ b/api/get_indexes.go
@@ -23,12 +23,9 @@ func (c *GetIndexesController) Prepare() {
// TODO: Shortcuts amd validate musicFolder parameter
func (c *GetIndexesController) Get() {
- var err error
+ ifModifiedSince := c.ParamTime("ifModifiedSince")
- var ifModifiedSince int64
- c.Ctx.Input.Bind(&ifModifiedSince, "ifModifiedSince")
-
- indexes, lastModified, err := c.browser.Indexes(utils.ToTime(ifModifiedSince))
+ indexes, lastModified, err := c.browser.Indexes(ifModifiedSince)
if err != nil {
beego.Error("Error retrieving Indexes:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
diff --git a/api/get_music_directory.go b/api/get_music_directory.go
index 19422f450..7f5552a98 100644
--- a/api/get_music_directory.go
+++ b/api/get_music_directory.go
@@ -23,7 +23,7 @@ func (c *GetMusicDirectoryController) Prepare() {
}
func (c *GetMusicDirectoryController) Get() {
- id := c.GetParameter("id", "id parameter required")
+ id := c.RequiredParamString("id", "id parameter required")
response := c.NewEmpty()
diff --git a/api/stream.go b/api/stream.go
index 46f120803..7be7be97e 100644
--- a/api/stream.go
+++ b/api/stream.go
@@ -20,7 +20,7 @@ type StreamController struct {
func (c *StreamController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.repo)
- c.id = c.GetParameter("id", "id parameter required")
+ c.id = c.RequiredParamString("id", "id parameter required")
mf, err := c.repo.Get(c.id)
if err != nil {
@@ -39,8 +39,7 @@ func (c *StreamController) Prepare() {
// TODO Still getting the "Conn.Write wrote more than the declared Content-Length" error.
// Don't know if this causes any issues
func (c *StreamController) Stream() {
- var maxBitRate int
- c.Ctx.Input.Bind(&maxBitRate, "maxBitRate")
+ maxBitRate := c.ParamInt("maxBitRate")
maxBitRate = utils.MinInt(c.mf.BitRate, maxBitRate)
beego.Debug("Streaming file", c.id, ":", c.mf.Path)
diff --git a/api/users.go b/api/users.go
index df3d647db..de15bb421 100644
--- a/api/users.go
+++ b/api/users.go
@@ -4,13 +4,12 @@ import "github.com/deluan/gosonic/api/responses"
type UsersController struct{ BaseAPIController }
-// TODO This is a placeholder. The real one has to read this info from a config file
+// TODO This is a placeholder. The real one has to read this info from a config file or the database
func (c *UsersController) GetUser() {
r := c.NewEmpty()
r.User = &responses.User{}
- r.User.Username = c.GetParameter("username", "Required string parameter 'username' is not present")
+ r.User.Username = c.RequiredParamString("username", "Required string parameter 'username' is not present")
r.User.StreamRole = true
r.User.DownloadRole = true
c.SendResponse(r)
}
-