mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-06 14:07:36 +03:00
Implemented common parameter validation
This commit is contained in:
parent
053f4b72ba
commit
18b784f494
5 changed files with 18 additions and 12 deletions
|
@ -13,6 +13,15 @@ func (c *BaseAPIController) NewEmpty() responses.Subsonic {
|
|||
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{}) {
|
||||
response := responses.Subsonic{Version: beego.AppConfig.String("apiVersion"), Status: "fail"}
|
||||
var msg string
|
||||
|
|
|
@ -21,10 +21,7 @@ func (c *GetCoverArtController) Prepare() {
|
|||
}
|
||||
|
||||
func (c *GetCoverArtController) Get() {
|
||||
id := c.Input().Get("id")
|
||||
if id == "" {
|
||||
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
|
||||
}
|
||||
id := c.ValidateParameters("id", "id parameter required")
|
||||
|
||||
mf, err := c.repo.Get(id)
|
||||
if err != nil {
|
||||
|
|
|
@ -20,7 +20,7 @@ func getCoverArt(params ...string) (*http.Request, *httptest.ResponseRecorder) {
|
|||
r, _ := http.NewRequest("GET", url, nil)
|
||||
w := httptest.NewRecorder()
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"github.com/karlkfi/inject"
|
||||
"mime"
|
||||
)
|
||||
|
||||
type GetMusicDirectoryController struct {
|
||||
|
@ -23,11 +22,7 @@ func (c *GetMusicDirectoryController) Prepare() {
|
|||
}
|
||||
|
||||
func (c *GetMusicDirectoryController) Get() {
|
||||
id := c.Input().Get("id")
|
||||
|
||||
if id == "" {
|
||||
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
|
||||
}
|
||||
id := c.ValidateParameters("id", "id parameter required")
|
||||
|
||||
response := c.NewEmpty()
|
||||
|
||||
|
@ -83,7 +78,7 @@ func (c *GetMusicDirectoryController) buildAlbumDir(al *domain.Album, tracks []d
|
|||
if mf.HasCoverArt {
|
||||
dir.Child[i].CoverArt = mf.Id
|
||||
}
|
||||
dir.Child[i].ContentType = mime.TypeByExtension("." + mf.Suffix)
|
||||
dir.Child[i].ContentType = mf.ContentType()
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package domain
|
|||
|
||||
import (
|
||||
"time"
|
||||
"mime"
|
||||
)
|
||||
|
||||
type MediaFile struct {
|
||||
|
@ -27,6 +28,10 @@ type MediaFile struct {
|
|||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (mf *MediaFile) ContentType() string {
|
||||
return mime.TypeByExtension("." + mf.Suffix)
|
||||
}
|
||||
|
||||
type MediaFileRepository interface {
|
||||
BaseRepository
|
||||
Put(m *MediaFile) error
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue