refactor: when resizing, don't buffer the original image "just in case"

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan
2024-11-19 12:44:54 -05:00
parent 2d4f483812
commit 81edef925c
+4 -8
View File
@@ -59,13 +59,9 @@ func (a *resizedArtworkReader) Reader(ctx context.Context) (io.ReadCloser, strin
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
// Keep a copy of the original data. In case we can't resize it, send it as is
buf := new(bytes.Buffer)
r := io.TeeReader(orig, buf)
defer orig.Close() defer orig.Close()
resized, origSize, err := resizeImage(r, a.size, a.square) resized, origSize, err := resizeImage(orig, a.size, a.square)
if resized == nil { if resized == nil {
log.Trace(ctx, "Image smaller than requested size", "artID", a.artID, "original", origSize, "resized", a.size) log.Trace(ctx, "Image smaller than requested size", "artID", a.artID, "original", origSize, "resized", a.size)
} else { } else {
@@ -75,9 +71,9 @@ func (a *resizedArtworkReader) Reader(ctx context.Context) (io.ReadCloser, strin
log.Warn(ctx, "Could not resize image. Will return image as is", "artID", a.artID, "size", a.size, err) log.Warn(ctx, "Could not resize image. Will return image as is", "artID", a.artID, "size", a.size, err)
} }
if err != nil || resized == nil { if err != nil || resized == nil {
// Force finish reading any remaining data // if we couldn't resize the image, return the original
_, _ = io.Copy(io.Discard, r) orig, _, err = a.a.Get(ctx, a.artID, 0, false)
return io.NopCloser(buf), "", nil //nolint:nilerr return orig, "", err
} }
return io.NopCloser(resized), fmt.Sprintf("%s@%d", a.artID, a.size), nil return io.NopCloser(resized), fmt.Sprintf("%s@%d", a.artID, a.size), nil
} }