mirror of
https://github.com/navidrome/navidrome.git
synced 2026-12-02 10:06:00 +00:00
* fix(server): check admin before changing a radio image
The radio image upload and delete handlers wrote or removed the file first,
and only then called Radio().Put, which refuses non-admin users. A regular
user got a 403, but the image file was already replaced or deleted.
Both /radio/{id}/image routes now use adminOnlyMiddleware, so the check runs
before the handlers touch the disk. Admin upload and delete work as before.
* fix(server): keep radio REST writes from setting the uploaded image path
The radio REST create and update wrote every field from the request body,
including uploaded_image. That value is later joined to the artwork folder
and removed on image delete or replace, so a value like ../../file pointed
at any file the server can remove.
Radio Save now drops the client id and uploaded image. Update writes only
name, stream_url and home_page_url, and also clears the uploaded image,
because put inserts the full row when the id does not exist.
model.UploadedImagePath now returns an empty path for any name that is not a
plain file name, so values already stored are harmless for radios, artists
and playlists. SetImage refuses a generated name that fails the same check,
and the artist image cleanup skips empty paths.
* refactor(server): simplify the radio image path checks
Update now has a single Put call, using slice.Filter over the allowed
columns. The two radio image routes share one adminOnlyMiddleware group.
isPlainFileName drops a redundant empty check, since filepath.IsLocal
already rejects it. The route test reuses createAuthenticatedRequest.
Comments are shorter. No behavior change.
* test(server): run the radio image specs on the real test database
Master replaced the shared repository fakes with a per-suite database,
so the specs no longer built after the rebase.
80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package nativeapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"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) addRadioRoute(r chi.Router) {
|
|
repo := api.ds.Radio()
|
|
r.Route("/radio", func(r chi.Router) {
|
|
r.Get("/", rest.GetAll(repo))
|
|
r.Post("/", rest.Post(repo))
|
|
r.Route("/{id}", func(r chi.Router) {
|
|
r.Use(server.URLParamsMiddleware)
|
|
r.Get("/", rest.Get(repo))
|
|
r.Put("/", rest.Put(repo))
|
|
r.Delete("/", rest.Delete(repo))
|
|
// Check admin before the image handlers touch the disk.
|
|
r.With(adminOnlyMiddleware).Route("/image", func(r chi.Router) {
|
|
r.Post("/", api.uploadRadioImage())
|
|
r.Delete("/", api.deleteRadioImage())
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
func (api *Router) uploadRadioImage() http.HandlerFunc {
|
|
return handleImageUpload(func(ctx context.Context, reader io.Reader, ext string) error {
|
|
radioID := chi.URLParamFromCtx(ctx, "id")
|
|
radio, err := api.ds.Radio().Get(ctx, radioID)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return model.ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
oldPath := radio.UploadedImagePath()
|
|
filename, err := api.imgUpload.SetImage(ctx, consts.EntityRadio, radio.ID, radio.Name, oldPath, reader, ext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
radio.UploadedImage = filename
|
|
if err := api.ds.Radio().Put(ctx, radio, "UploadedImage"); err != nil {
|
|
return err
|
|
}
|
|
api.imgUpload.EnqueueArtwork(ctx, consts.EntityRadio, radio.ID)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (api *Router) deleteRadioImage() http.HandlerFunc {
|
|
return handleImageDelete(func(ctx context.Context) error {
|
|
radioID := chi.URLParamFromCtx(ctx, "id")
|
|
radio, err := api.ds.Radio().Get(ctx, radioID)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return model.ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
if err := api.imgUpload.RemoveImage(ctx, radio.UploadedImagePath()); err != nil {
|
|
return err
|
|
}
|
|
radio.UploadedImage = ""
|
|
if err := api.ds.Radio().Put(ctx, radio, "UploadedImage"); err != nil {
|
|
return err
|
|
}
|
|
api.imgUpload.EnqueueArtwork(ctx, consts.EntityRadio, radio.ID)
|
|
return nil
|
|
})
|
|
}
|