mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +00:00
Entity-level artwork queries now take a typed model.Kind instead of a bare prefix string. GetItemArtwork, DeleteForItem(s), GetInfoForItems, EnqueueStaleAbsent, hydrateItemImages, enqueueBackfillKind and artwork.Refresh convert to the prefix string only at the two real boundaries: the SQL item_kind column (kind.Prefix() inside each repo) and external string inputs (a new model.ParseKind for the nativeapi URL param, which also validates it). The Backfill/stale-absent kind slices, the resolve.go dispatch switch, and the kind→resource / kind→table lookup maps now use the Kind vars directly. The queue lifecycle methods (MarkFailed/Delete*) keep string kinds — they operate on a dequeued item's raw ItemKind column, which stays a string field, always populated via kind.Prefix(). Removes every bare "al"/"ar"/… prefix literal from non-test code (27 -> 0); behavior is unchanged.
111 lines
3.5 KiB
Go
111 lines
3.5 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils"
|
|
)
|
|
|
|
// MaxImageUploadSize returns the configured MaxImageUploadSize in bytes, or the built-in default
|
|
// when it's unset/invalid. Shared by every API that accepts image uploads.
|
|
func MaxImageUploadSize() int64 {
|
|
if size, err := humanize.ParseBytes(conf.Server.MaxImageUploadSize); err == nil && size > 0 {
|
|
return int64(size)
|
|
}
|
|
size, _ := humanize.ParseBytes(consts.DefaultMaxImageUploadSize)
|
|
return int64(size)
|
|
}
|
|
|
|
// Uploader stores a user-uploaded entity image and invalidates that entity's artwork state so the
|
|
// upload becomes the served cover.
|
|
type Uploader interface {
|
|
SetImage(ctx context.Context, entityType string, entityID string, name string, oldPath string, reader io.Reader, ext string) (filename string, err error)
|
|
RemoveImage(ctx context.Context, path string) error
|
|
// EnqueueArtwork clears an item's resolved state and re-queues it at Bump priority. Callers
|
|
// must invoke it AFTER persisting the new filename, so the worker never resolves the old one.
|
|
EnqueueArtwork(ctx context.Context, entityType, entityID string)
|
|
}
|
|
|
|
// uploadEntityKind maps an upload's entity type to its artwork kind prefix, so a
|
|
// successful upload can clear and re-queue that item's artwork state.
|
|
var uploadEntityKind = map[string]model.Kind{
|
|
consts.EntityArtist: model.KindArtistArtwork,
|
|
consts.EntityPlaylist: model.KindPlaylistArtwork,
|
|
consts.EntityRadio: model.KindRadioArtwork,
|
|
}
|
|
|
|
type uploader struct {
|
|
ds model.DataStore
|
|
}
|
|
|
|
func NewUploader(ds model.DataStore) Uploader {
|
|
return &uploader{ds: ds}
|
|
}
|
|
|
|
func (s *uploader) SetImage(ctx context.Context, entityType string, entityID string, name string, oldPath string, reader io.Reader, ext string) (string, error) {
|
|
filename := imageFilename(entityID, name, ext)
|
|
absPath := model.UploadedImagePath(entityType, filename)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
|
|
return "", fmt.Errorf("creating image directory: %w", err)
|
|
}
|
|
|
|
// Remove old image if it exists
|
|
if oldPath != "" {
|
|
if err := os.Remove(oldPath); err != nil && !os.IsNotExist(err) {
|
|
log.Warn(ctx, "Failed to remove old image", "path", oldPath, err)
|
|
}
|
|
}
|
|
|
|
// Save new image
|
|
f, err := os.Create(absPath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("creating image file: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if _, err := io.Copy(f, reader); err != nil {
|
|
return "", fmt.Errorf("writing image file: %w", err)
|
|
}
|
|
return filename, nil
|
|
}
|
|
|
|
// EnqueueArtwork clears the item's resolved state and re-queues it at Bump priority: the
|
|
// upload is now the top-priority source, so the worker re-resolves and the UI swaps.
|
|
func (s *uploader) EnqueueArtwork(ctx context.Context, entityType, id string) {
|
|
kind, ok := uploadEntityKind[entityType]
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := Refresh(ctx, s.ds, kind, id); err != nil {
|
|
log.Warn(ctx, "Could not refresh artwork after upload", "kind", kind, "id", id, err)
|
|
}
|
|
}
|
|
|
|
func (s *uploader) RemoveImage(ctx context.Context, path string) error {
|
|
if path == "" {
|
|
return nil
|
|
}
|
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("removing image %q: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func imageFilename(id, name, ext string) string {
|
|
clean := utils.CleanFileName(name)
|
|
if clean == "" {
|
|
return id + ext
|
|
}
|
|
return id + "_" + clean + ext
|
|
}
|