fix: go back to synchronous image processing

This commit is contained in:
Gabe Farrell 2025-06-12 00:30:01 -04:00
parent aba2b76def
commit 1a5a6acc95
10 changed files with 58 additions and 168 deletions

View file

@ -17,7 +17,7 @@ import (
"github.com/google/uuid"
)
func ImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
func ImageHandler(store db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logger.FromContext(r.Context())
size := chi.URLParam(r, "size")
@ -31,7 +31,7 @@ func ImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
imgid, err := uuid.Parse(filename)
if err != nil {
serveDefaultImage(w, r, imageSize, ip)
serveDefaultImage(w, r, imageSize)
return
}
@ -51,7 +51,7 @@ func ImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
if _, err = os.Stat(fullSizePath); os.IsNotExist(err) {
if _, err = os.Stat(largeSizePath); os.IsNotExist(err) {
l.Warn().Msgf("Could not find requested image %s. If this image is tied to an album or artist, it should be replaced", imgid.String())
serveDefaultImage(w, r, imageSize, ip)
serveDefaultImage(w, r, imageSize)
return
} else if err != nil {
// non-not found error for full file
@ -80,7 +80,7 @@ func ImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
return
}
err = ip.EnqueueCompressAndSave(r.Context(), imgid.String(), imageSize, bytes.NewReader(imageBuf))
err = catalog.CompressAndSaveImage(r.Context(), imgid.String(), imageSize, bytes.NewReader(imageBuf))
if err != nil {
l.Err(err).Msg("Failed to save compressed image to cache")
}
@ -96,7 +96,7 @@ func ImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
}
}
func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.ImageSize, ip *catalog.ImageProcessor) {
func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.ImageSize) {
var lock sync.Mutex
l := logger.FromContext(r.Context())
defaultImagePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, string(size), "default_img")
@ -127,7 +127,7 @@ func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.Imag
w.WriteHeader(http.StatusInternalServerError)
return
}
err = ip.EnqueueCompressAndSave(r.Context(), "default_img", size, file)
err = catalog.CompressAndSaveImage(r.Context(), "default_img", size, file)
if err != nil {
l.Err(err).Msg("Error when caching default img at desired size")
w.WriteHeader(http.StatusInternalServerError)

View file

@ -20,7 +20,7 @@ type ReplaceImageResponse struct {
Message string `json:"message,omitempty"`
}
func ReplaceImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFunc {
func ReplaceImageHandler(store db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
l := logger.FromContext(ctx)
@ -80,7 +80,7 @@ func ReplaceImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFu
dlSize = catalog.ImageSizeLarge
}
l.Debug().Msg("Downloading album image from source...")
err = ip.EnqueueDownloadAndCache(ctx, id, fileUrl, dlSize)
err = catalog.DownloadAndCacheImage(ctx, id, fileUrl, dlSize)
if err != nil {
l.Err(err).Msg("Failed to cache image")
}
@ -120,7 +120,7 @@ func ReplaceImageHandler(store db.DB, ip *catalog.ImageProcessor) http.HandlerFu
dlSize = catalog.ImageSizeLarge
}
err = ip.EnqueueCompressAndSave(ctx, id.String(), dlSize, file)
err = catalog.CompressAndSaveImage(ctx, id.String(), dlSize, file)
if err != nil {
utils.WriteError(w, "Could not save file", http.StatusInternalServerError)
return