mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 21:48:18 -08:00
feat: re-download missing images on request
This commit is contained in:
parent
3af969b25c
commit
4c4ebc593d
2 changed files with 32 additions and 17 deletions
15
CHANGELOG.md
15
CHANGELOG.md
|
|
@ -1,14 +1,3 @@
|
||||||
# v0.0.3
|
# v0.0.4
|
||||||
## Features
|
|
||||||
- Delete listens from the UI
|
|
||||||
|
|
||||||
## Enhancements
|
## Enhancements
|
||||||
- Better behaved mobile UI
|
- Re-download images missing from cache on request
|
||||||
- Search now returns 8 items per category instead of 5
|
|
||||||
|
|
||||||
## Fixes
|
|
||||||
- Many mobile UI fixes
|
|
||||||
|
|
||||||
## Updates
|
|
||||||
- Refuses a config that changes the MusicBrainz rate limit while using the official MusicBrainz URL
|
|
||||||
- Warns when enabling ListenBrainz relay with missing configuration
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -47,18 +49,23 @@ func ImageHandler(store db.DB) http.HandlerFunc {
|
||||||
fullSizePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(catalog.ImageSizeFull), filepath.Clean(filename))
|
fullSizePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(catalog.ImageSizeFull), filepath.Clean(filename))
|
||||||
largeSizePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(catalog.ImageSizeLarge), filepath.Clean(filename))
|
largeSizePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(catalog.ImageSizeLarge), filepath.Clean(filename))
|
||||||
|
|
||||||
|
// this if statement flow is terrible but whatever
|
||||||
var sourcePath string
|
var sourcePath string
|
||||||
if _, err = os.Stat(fullSizePath); os.IsNotExist(err) {
|
if _, err = os.Stat(fullSizePath); os.IsNotExist(err) {
|
||||||
if _, err = os.Stat(largeSizePath); os.IsNotExist(err) {
|
if _, err = os.Stat(largeSizePath); os.IsNotExist(err) {
|
||||||
l.Warn().Msgf("ImageHandler: Could not find requested image %s. Serving default image", imgid.String())
|
l.Warn().Msgf("ImageHandler: Could not find requested image %s. Attempting to download from source", imgid.String())
|
||||||
serveDefaultImage(w, r, imageSize)
|
sourcePath, err = downloadMissingImage(r.Context(), store, imgid)
|
||||||
return
|
if err != nil {
|
||||||
|
l.Err(err).Msg("ImageHandler: Failed to redownload missing image")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
l.Err(err).Msg("ImageHandler: Failed to access source image file at large size")
|
l.Err(err).Msg("ImageHandler: Failed to access source image file at large size")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
} else {
|
||||||
sourcePath = largeSizePath
|
sourcePath = largeSizePath
|
||||||
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
l.Err(err).Msg("ImageHandler: Failed to access source image file at full size")
|
l.Err(err).Msg("ImageHandler: Failed to access source image file at full size")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
@ -139,3 +146,22 @@ func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.Imag
|
||||||
l.Debug().Msgf("serveDefaultImage: Successfully serving default image at size '%s'", size)
|
l.Debug().Msgf("serveDefaultImage: Successfully serving default image at size '%s'", size)
|
||||||
http.ServeFile(w, r, path.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(size), "default_img"))
|
http.ServeFile(w, r, path.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(size), "default_img"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// finds the item associated with the image id, downloads it, and saves it in the source path, returning the path to the image
|
||||||
|
func downloadMissingImage(ctx context.Context, store db.DB, id uuid.UUID) (string, error) {
|
||||||
|
src, err := store.GetImageSource(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("downloadMissingImage: store.GetImageSource: %w", err)
|
||||||
|
}
|
||||||
|
var size catalog.ImageSize
|
||||||
|
if cfg.FullImageCacheEnabled() {
|
||||||
|
size = catalog.ImageSizeFull
|
||||||
|
} else {
|
||||||
|
size = catalog.ImageSizeLarge
|
||||||
|
}
|
||||||
|
err = catalog.DownloadAndCacheImage(ctx, id, src, size)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("downloadMissingImage: catalog.DownloadAndCacheImage: %w", err)
|
||||||
|
}
|
||||||
|
return path.Join(catalog.SourceImageDir(), id.String()), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue