Handle request (context) cancellation
This commit is contained in:
+5
-3
@@ -56,9 +56,8 @@ func (a *artwork) Get(ctx context.Context, id string, size int) (io.ReadCloser,
|
|||||||
key := &artworkKey{a: a, artID: artID, size: size}
|
key := &artworkKey{a: a, artID: artID, size: size}
|
||||||
|
|
||||||
r, err := a.cache.Get(ctx, key)
|
r, err := a.cache.Get(ctx, key)
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
log.Error(ctx, "Error accessing image cache", "id", id, "size", size, err)
|
log.Error(ctx, "Error accessing image cache", "id", id, "size", size, err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
@@ -77,7 +76,7 @@ func (a *artwork) get(ctx context.Context, artID model.ArtworkID, size int) (rea
|
|||||||
default:
|
default:
|
||||||
reader, path = fromPlaceholder()()
|
reader, path = fromPlaceholder()()
|
||||||
}
|
}
|
||||||
return reader, path, nil
|
return reader, path, ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID) (io.ReadCloser, string) {
|
func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID) (io.ReadCloser, string) {
|
||||||
@@ -148,6 +147,9 @@ func (f fromFunc) String() string {
|
|||||||
|
|
||||||
func extractImage(ctx context.Context, artID model.ArtworkID, extractFuncs ...fromFunc) (io.ReadCloser, string) {
|
func extractImage(ctx context.Context, artID model.ArtworkID, extractFuncs ...fromFunc) (io.ReadCloser, string) {
|
||||||
for _, f := range extractFuncs {
|
for _, f := range extractFuncs {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
r, path := f()
|
r, path := f()
|
||||||
if r != nil {
|
if r != nil {
|
||||||
log.Trace(ctx, "Found artwork", "artID", artID, "path", path, "origin", f)
|
log.Trace(ctx, "Found artwork", "artID", artID, "path", path, "origin", f)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package subsonic
|
package subsonic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -57,11 +58,13 @@ func (api *Router) GetCoverArt(w http.ResponseWriter, r *http.Request) (*respons
|
|||||||
w.Header().Set("cache-control", "public, max-age=315360000")
|
w.Header().Set("cache-control", "public, max-age=315360000")
|
||||||
|
|
||||||
imgReader, err := api.artwork.Get(r.Context(), id, size)
|
imgReader, err := api.artwork.Get(r.Context(), id, size)
|
||||||
if errors.Is(err, model.ErrNotFound) {
|
switch {
|
||||||
|
case errors.Is(err, context.Canceled):
|
||||||
|
return nil, nil
|
||||||
|
case errors.Is(err, model.ErrNotFound):
|
||||||
log.Error(r, "Couldn't find coverArt", "id", id, err)
|
log.Error(r, "Couldn't find coverArt", "id", id, err)
|
||||||
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
|
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
|
||||||
}
|
case err != nil:
|
||||||
if err != nil {
|
|
||||||
log.Error(r, "Error retrieving coverArt", "id", id, err)
|
log.Error(r, "Error retrieving coverArt", "id", id, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user