Files
navidrome/model/artist.go
Deluan Quintão ab8a58157a feat: add artist image uploads and image-folder artwork source (#5198)
* feat: add shared ImageUploadService for entity image management

* feat: add UploadedImage field and methods to Artist model

* feat: add uploaded_image column to artist table

* feat: add ArtistImageFolder config option

* refactor: wire ImageUploadService and delegate playlist file ops to it

Wire ImageUploadService into the DI container and refactor the playlist
service to delegate image file operations (SetImage/RemoveImage) to the
shared ImageUploadService, removing duplicated file I/O logic. A local
ImageUploadService interface is defined in core/playlists to avoid an
import cycle between core and core/playlists.

* feat: artist artwork reader checks uploaded image first

* feat: add image-folder priority source for artist artwork

* feat: cache key invalidation for image-folder and uploaded images

* refactor: extract shared image upload HTTP helpers

* feat: add artist image upload/delete API endpoints

* refactor: playlist handlers use shared image upload helpers

* feat: add shared ImageUploadOverlay component

* feat: add i18n keys for artist image upload

* feat: add image upload overlay to artist detail pages

* refactor: playlist details uses shared ImageUploadOverlay component

* fix: add gosec nolint directive for ParseMultipartForm

* refactor: deduplicate image upload code and optimize dir scanning

- Remove dead ImageFilename methods from Artist and Playlist models
  (production code uses core.imageFilename exclusively)
- Extract shared uploadedImagePath helper in model/image.go
- Extract findImageInArtistFolder to deduplicate dir-scanning logic
  between fromArtistImageFolder and getArtistImageFolderModTime
- Fix fileInputRef in useCallback dependency array

* fix: include artist UpdatedAt in artwork cache key

Without this, uploading or deleting an artist image would not
invalidate the cached artwork because the cache key was only based
on album folder timestamps, not the artist's own UpdatedAt field.

* feat: add Portuguese translations for artist image upload

* refactor: use shared i18n keys for cover art upload messages

Move cover art upload/remove translations from per-entity sections
(artist, playlist) to a shared top-level "message" section, avoiding
duplication across entity types and translation files.

* refactor: move cover art i18n keys to shared message section for all languages

* refactor: simplify image upload code and eliminate redundancies

Extracted duplicate image loading/lightbox state logic from
DesktopArtistDetails and MobileArtistDetails into a shared
useArtistImageState hook. Moved entity type constants to the consts
package and replaced raw string literals throughout model, core, and
nativeapi packages. Exported model.UploadedImagePath and reused it in
core/image_upload.go to consolidate path construction. Cached the
ArtistImageFolder lookup result in artistReader to eliminate a redundant
os.ReadDir call on every artwork request.

Signed-off-by: Deluan <deluan@navidrome.org>

* style: fix prettier formatting in ImageUploadOverlay

* fix: address code review feedback on image upload error handling

- RemoveImage now returns errors instead of swallowing them
- Artist handlers distinguish not-found from other DB errors
- Defer multipart temp file cleanup after parsing

* fix: enforce hard request size limit with MaxBytesReader for image uploads

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2026-03-15 22:19:55 -04:00

98 lines
3.1 KiB
Go

package model
import (
"maps"
"slices"
"time"
"github.com/navidrome/navidrome/consts"
)
type Artist struct {
Annotations `structs:"-"`
ID string `structs:"id" json:"id"`
// Data based on tags
Name string `structs:"name" json:"name"`
SortArtistName string `structs:"sort_artist_name" json:"sortArtistName,omitempty"`
OrderArtistName string `structs:"order_artist_name" json:"orderArtistName,omitempty"`
MbzArtistID string `structs:"mbz_artist_id" json:"mbzArtistId,omitempty"`
// Data calculated from files
Stats map[Role]ArtistStats `structs:"-" json:"stats,omitempty"`
Size int64 `structs:"-" json:"size,omitempty"`
AlbumCount int `structs:"-" json:"albumCount,omitempty"`
SongCount int `structs:"-" json:"songCount,omitempty"`
// Data imported from external sources
Biography string `structs:"biography" json:"biography,omitempty"`
SmallImageUrl string `structs:"small_image_url" json:"smallImageUrl,omitempty"`
MediumImageUrl string `structs:"medium_image_url" json:"mediumImageUrl,omitempty"`
LargeImageUrl string `structs:"large_image_url" json:"largeImageUrl,omitempty"`
ExternalUrl string `structs:"external_url" json:"externalUrl,omitempty"`
SimilarArtists Artists `structs:"similar_artists" json:"-"`
ExternalInfoUpdatedAt *time.Time `structs:"external_info_updated_at" json:"externalInfoUpdatedAt,omitempty"`
Missing bool `structs:"missing" json:"missing"`
UploadedImage string `structs:"uploaded_image" json:"uploadedImage,omitempty"`
CreatedAt *time.Time `structs:"created_at" json:"createdAt,omitempty"`
UpdatedAt *time.Time `structs:"updated_at" json:"updatedAt,omitempty"`
}
type ArtistStats struct {
SongCount int `json:"songCount"`
AlbumCount int `json:"albumCount"`
Size int64 `json:"size"`
}
func (a Artist) ArtistImageUrl() string {
if a.LargeImageUrl != "" {
return a.LargeImageUrl
}
if a.MediumImageUrl != "" {
return a.MediumImageUrl
}
return a.SmallImageUrl
}
func (a Artist) CoverArtID() ArtworkID {
return artworkIDFromArtist(a)
}
func (a Artist) UploadedImagePath() string {
return UploadedImagePath(consts.EntityArtist, a.UploadedImage)
}
// Roles returns the roles this artist has participated in., based on the Stats field
func (a Artist) Roles() []Role {
return slices.Collect(maps.Keys(a.Stats))
}
type Artists []Artist
type ArtistIndex struct {
ID string
Artists Artists
}
type ArtistIndexes []ArtistIndex
type ArtistRepository interface {
CountAll(options ...QueryOptions) (int64, error)
Exists(id string) (bool, error)
Put(m *Artist, colsToUpdate ...string) error
UpdateExternalInfo(a *Artist) error
Get(id string) (*Artist, error)
GetAll(options ...QueryOptions) (Artists, error)
GetIndex(includeMissing bool, libraryIds []int, roles ...Role) (ArtistIndexes, error)
// The following methods are used exclusively by the scanner:
RefreshPlayCounts() (int64, error)
RefreshStats(allArtists bool) (int64, error)
AnnotatedRepository
SearchableRepository[Artists]
}