Files
navidrome/server/nativeapi/artists.go
Deluan 50ada9ad29 fix(artwork): invalidate artwork when an uploaded image is deleted
Deleting an artist/radio/playlist upload cleared the filename but left the found
item_artwork row and its hash, so lists kept advertising the deleted cover's
hash-suffixed immutable URL and clients could display it indefinitely. Call
EnqueueArtwork after the delete-side Put, symmetric with upload, so the state is
cleared and re-resolved to the next source (or absent).
2026-07-23 14:08:08 -04:00

79 lines
2.1 KiB
Go

package nativeapi
import (
"context"
"errors"
"io"
"net/http"
"time"
"github.com/deluan/rest"
"github.com/go-chi/chi/v5"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
)
func (api *Router) addArtistRoute(r chi.Router) {
constructor := func(ctx context.Context) rest.Repository {
return api.ds.Resource(ctx, model.Artist{})
}
r.Route("/artist", func(r chi.Router) {
r.Get("/", rest.GetAll(constructor))
r.Route("/{id}", func(r chi.Router) {
r.Use(server.URLParamsMiddleware)
r.Get("/", rest.Get(constructor))
r.Post("/image", api.uploadArtistImage())
r.Delete("/image", api.deleteArtistImage())
})
})
}
func (api *Router) uploadArtistImage() http.HandlerFunc {
return handleImageUpload(func(ctx context.Context, reader io.Reader, ext string) error {
artistID := chi.URLParamFromCtx(ctx, "id")
ar, err := api.ds.Artist(ctx).Get(artistID)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return model.ErrNotFound
}
return err
}
oldPath := ar.UploadedImagePath()
filename, err := api.imgUpload.SetImage(ctx, consts.EntityArtist, ar.ID, ar.Name, oldPath, reader, ext)
if err != nil {
return err
}
ar.UploadedImage = filename
ar.UpdatedAt = new(time.Now())
if err := api.ds.Artist(ctx).Put(ar, "uploaded_image", "updated_at"); err != nil {
return err
}
api.imgUpload.EnqueueArtwork(ctx, consts.EntityArtist, ar.ID)
return nil
})
}
func (api *Router) deleteArtistImage() http.HandlerFunc {
return handleImageDelete(func(ctx context.Context) error {
artistID := chi.URLParamFromCtx(ctx, "id")
ar, err := api.ds.Artist(ctx).Get(artistID)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return model.ErrNotFound
}
return err
}
if err := api.imgUpload.RemoveImage(ctx, ar.UploadedImagePath()); err != nil {
return err
}
ar.UploadedImage = ""
ar.UpdatedAt = new(time.Now())
if err := api.ds.Artist(ctx).Put(ar, "uploaded_image", "updated_at"); err != nil {
return err
}
api.imgUpload.EnqueueArtwork(ctx, consts.EntityArtist, ar.ID)
return nil
})
}