Have nicer 404 errors for covers
This commit is contained in:
@@ -20,6 +20,12 @@ type AlbumCoverCache struct {
|
|||||||
TTL time.Duration
|
TTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ErrorNotRegistered AlbumID
|
||||||
|
|
||||||
|
func (e ErrorNotRegistered) Error() string {
|
||||||
|
return fmt.Sprintf("Cover URL for Album %d is not registered", AlbumID(e))
|
||||||
|
}
|
||||||
|
|
||||||
// NewAlbumCoverCache is creating a new cache at specified location on the fs
|
// NewAlbumCoverCache is creating a new cache at specified location on the fs
|
||||||
func NewAlbumCoverCache(basepath string, maxRequest uint, window time.Duration) *AlbumCoverCache {
|
func NewAlbumCoverCache(basepath string, maxRequest uint, window time.Duration) *AlbumCoverCache {
|
||||||
res := &AlbumCoverCache{
|
res := &AlbumCoverCache{
|
||||||
@@ -37,7 +43,7 @@ func NewAlbumCoverCache(basepath string, maxRequest uint, window time.Duration)
|
|||||||
func (c *AlbumCoverCache) fetch(ID AlbumID) (io.ReadCloser, string, error) {
|
func (c *AlbumCoverCache) fetch(ID AlbumID) (io.ReadCloser, string, error) {
|
||||||
URL, ok := c.URLs[ID]
|
URL, ok := c.URLs[ID]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
return nil, "", fmt.Errorf("Cover URL for Album %d is not registered", ID)
|
return nil, "", ErrorNotRegistered(ID)
|
||||||
}
|
}
|
||||||
ext := AlbumCoverExt(URL)
|
ext := AlbumCoverExt(URL)
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ func (a *appData) buildRouter() http.Handler {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rc, ext, err := a.cover.GetCover(AlbumID(ID))
|
rc, ext, err := a.cover.GetCover(AlbumID(ID))
|
||||||
@@ -104,10 +105,16 @@ func (a *appData) buildRouter() http.Handler {
|
|||||||
defer closeOrPanic(rc, "Album cover "+name)
|
defer closeOrPanic(rc, "Album cover "+name)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if _, ok := err.(ErrorNotRegistered); ok == true {
|
||||||
|
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if ext != requestedExt {
|
if ext != requestedExt {
|
||||||
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
io.Copy(w, rc)
|
io.Copy(w, rc)
|
||||||
|
|||||||
Reference in New Issue
Block a user