Implemented common parameter validation

This commit is contained in:
Deluan 2016-03-03 14:14:15 -05:00
parent 053f4b72ba
commit 18b784f494
5 changed files with 18 additions and 12 deletions

View File

@ -13,6 +13,15 @@ func (c *BaseAPIController) NewEmpty() responses.Subsonic {
return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")} return responses.Subsonic{Status: "ok", Version: beego.AppConfig.String("apiVersion")}
} }
func (c *BaseAPIController) ValidateParameters(param string, msg string) string {
p := c.Input().Get(param)
if p == "" {
c.SendError(responses.ERROR_MISSING_PARAMETER, msg)
}
c.Data[param] = p
return p
}
func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) { func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
response := responses.Subsonic{Version: beego.AppConfig.String("apiVersion"), Status: "fail"} response := responses.Subsonic{Version: beego.AppConfig.String("apiVersion"), Status: "fail"}
var msg string var msg string

View File

@ -21,10 +21,7 @@ func (c *GetCoverArtController) Prepare() {
} }
func (c *GetCoverArtController) Get() { func (c *GetCoverArtController) Get() {
id := c.Input().Get("id") id := c.ValidateParameters("id", "id parameter required")
if id == "" {
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
}
mf, err := c.repo.Get(id) mf, err := c.repo.Get(id)
if err != nil { if err != nil {

View File

@ -20,7 +20,7 @@ func getCoverArt(params ...string) (*http.Request, *httptest.ResponseRecorder) {
r, _ := http.NewRequest("GET", url, nil) r, _ := http.NewRequest("GET", url, nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r) beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Debug("testing TestGetCoverArtDirectory", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap)) beego.Debug("testing TestGetCoverArt", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
return r, w return r, w
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject" "github.com/karlkfi/inject"
"mime"
) )
type GetMusicDirectoryController struct { type GetMusicDirectoryController struct {
@ -23,11 +22,7 @@ func (c *GetMusicDirectoryController) Prepare() {
} }
func (c *GetMusicDirectoryController) Get() { func (c *GetMusicDirectoryController) Get() {
id := c.Input().Get("id") id := c.ValidateParameters("id", "id parameter required")
if id == "" {
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
}
response := c.NewEmpty() response := c.NewEmpty()
@ -83,7 +78,7 @@ func (c *GetMusicDirectoryController) buildAlbumDir(al *domain.Album, tracks []d
if mf.HasCoverArt { if mf.HasCoverArt {
dir.Child[i].CoverArt = mf.Id dir.Child[i].CoverArt = mf.Id
} }
dir.Child[i].ContentType = mime.TypeByExtension("." + mf.Suffix) dir.Child[i].ContentType = mf.ContentType()
} }
return dir return dir
} }

View File

@ -2,6 +2,7 @@ package domain
import ( import (
"time" "time"
"mime"
) )
type MediaFile struct { type MediaFile struct {
@ -27,6 +28,10 @@ type MediaFile struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
func (mf *MediaFile) ContentType() string {
return mime.TypeByExtension("." + mf.Suffix)
}
type MediaFileRepository interface { type MediaFileRepository interface {
BaseRepository BaseRepository
Put(m *MediaFile) error Put(m *MediaFile) error