mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* feat(ui): add Share button to artist detail page * feat(ui): add Download button to artist detail page * fix(ui): scope artist share/download to album-artist content Gate the artist Share/Download actions on album-artist stats and show the album-artist size, since ZipArtist and the share query only cover album_artist_id songs. Previously the total (role-inclusive) size was shown and guest-only artists could produce an empty archive. Applies to the artist toolbar, the shared context menu, and the download dialog title. * fix: match artist download/share to album-artist participation ZipArtist and the artist share query filtered the deprecated album_artist_id column, which only stores the first album artist of a track. Secondary album-artists (co-credited but not first) got an empty download/share even though the UI offered it. Filter by the album-artist role participation instead, matching the artist's album-artist stats used to gate the actions. Also cover the artist-specific size branch of the download dialog. * fix(share): scope artist shares to the owner's libraries The artist share query broadened to album-artist participation, which could pull a secondary album artist's tracks from libraries the (non-admin) share owner cannot access into the public share. Load the artist share as the owner so their library access is applied, mirroring how playlist shares already work. Adds a repository test covering co-album-artist inclusion and library scoping. * test(share): assert album participation branch of artist shares Link the co-album-artist fixtures to albums and assert share.Albums (used by Subsonic getShares) includes the accessible album and excludes the one in a library the owner cannot access, so the album participation + scoping branch is covered too. * fix: exclude missing files from artist download/share actions An artist's stats still count files that went missing, so the toolbar/context menu could offer Download/Share for an artist whose files are all gone, while the share query (missing=false) returns nothing and downloads open dead paths. Hide the actions when the artist is missing and exclude missing files from ZipArtist, matching the share semantics. * refactor: dedupe artist download-size and share-owner lookups Extract the 'album-artist download size (or none when missing)' rule into a single artistDownloadSize() helper shared by the toolbar, context menu, and download dialog, and factor the duplicated share-owner context lookup into a shareRepository.ownerContext() method used by both the artist and playlist share cases. * refactor(ui): move artistDownloadSize helper to common utils is for domain-agnostic, potentially portable code; this helper is Navidrome-specific (artist stats shape), so it belongs in common. Consumers import it directly from common/artist to avoid pulling in the common barrel.
223 lines
7.3 KiB
Go
223 lines
7.3 KiB
Go
package core
|
|
|
|
import (
|
|
"archive/zip"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/core/stream"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"github.com/navidrome/navidrome/utils/str"
|
|
)
|
|
|
|
type Archiver interface {
|
|
ZipAlbum(ctx context.Context, id string, format string, bitrate int, w io.Writer) error
|
|
ZipArtist(ctx context.Context, id string, format string, bitrate int, w io.Writer) error
|
|
ZipShare(ctx context.Context, s *model.Share, w io.Writer) error
|
|
ZipPlaylist(ctx context.Context, id string, format string, bitrate int, w io.Writer) error
|
|
}
|
|
|
|
func NewArchiver(ms stream.MediaStreamer, ds model.DataStore, shares Share) Archiver {
|
|
return &archiver{ds: ds, ms: ms, shares: shares}
|
|
}
|
|
|
|
type archiver struct {
|
|
ds model.DataStore
|
|
ms stream.MediaStreamer
|
|
shares Share
|
|
}
|
|
|
|
func (a *archiver) ZipAlbum(ctx context.Context, id string, format string, bitrate int, out io.Writer) error {
|
|
return a.zipAlbums(ctx, id, format, bitrate, out, squirrel.Eq{"album_id": id})
|
|
}
|
|
|
|
func (a *archiver) ZipArtist(ctx context.Context, id string, format string, bitrate int, out io.Writer) error {
|
|
// Match by album-artist participation, not the deprecated album_artist_id
|
|
// column (first album artist only), so co-album-artists are included too.
|
|
filter := squirrel.And{
|
|
persistence.ParticipantIDFilter("media_file", id, model.RoleAlbumArtist),
|
|
squirrel.Eq{"missing": false},
|
|
}
|
|
return a.zipAlbums(ctx, id, format, bitrate, out, filter)
|
|
}
|
|
|
|
func (a *archiver) zipAlbums(ctx context.Context, id string, format string, bitrate int, out io.Writer, filters squirrel.Sqlizer) error {
|
|
mfs, err := a.ds.MediaFile(ctx).GetAll(model.QueryOptions{Filters: filters, Sort: "album"})
|
|
if err != nil {
|
|
log.Error(ctx, "Error loading mediafiles from artist", "id", id, err)
|
|
return err
|
|
}
|
|
|
|
z := createZipWriter(out, format, bitrate)
|
|
albums := slice.Group(mfs, func(mf model.MediaFile) string {
|
|
return mf.AlbumID
|
|
})
|
|
for _, album := range albums {
|
|
discs := slice.Group(album, func(mf model.MediaFile) int { return mf.DiscNumber })
|
|
isMultiDisc := len(discs) > 1
|
|
log.Debug(ctx, "Zipping album", "name", album[0].Album, "artist", album[0].AlbumArtist,
|
|
"format", format, "bitrate", bitrate, "isMultiDisc", isMultiDisc, "numTracks", len(album))
|
|
for _, mf := range album {
|
|
file := a.albumFilename(mf, format, isMultiDisc)
|
|
if addErr := a.addFileToZip(ctx, z, mf, format, bitrate, file); errors.Is(addErr, stream.ErrTooManyTranscodes) {
|
|
// Stop iterating: continuing would just rack up more
|
|
// rejections from the limiter. Close finalises whatever
|
|
// tracks were already written; the rejected one is not
|
|
// present in the archive (addFileToZip aborts before
|
|
// writing its entry header).
|
|
_ = z.Close()
|
|
return addErr
|
|
}
|
|
}
|
|
}
|
|
err = z.Close()
|
|
if err != nil {
|
|
log.Error(ctx, "Error closing zip file", "id", id, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func createZipWriter(out io.Writer, format string, bitrate int) *zip.Writer {
|
|
z := zip.NewWriter(out)
|
|
comment := "Downloaded from Navidrome"
|
|
if format != "raw" && format != "" {
|
|
comment = fmt.Sprintf("%s, transcoded to %s %dbps", comment, format, bitrate)
|
|
}
|
|
_ = z.SetComment(comment)
|
|
return z
|
|
}
|
|
|
|
func (a *archiver) albumFilename(mf model.MediaFile, format string, isMultiDisc bool) string {
|
|
_, file := filepath.Split(mf.Path)
|
|
if format != "raw" {
|
|
file = strings.TrimSuffix(file, mf.Suffix) + format
|
|
}
|
|
if isMultiDisc {
|
|
file = fmt.Sprintf("Disc %02d/%s", mf.DiscNumber, file)
|
|
}
|
|
return fmt.Sprintf("%s/%s", str.SanitizeFilename(mf.Album), file)
|
|
}
|
|
|
|
// ZipShare takes an already-loaded share: Share.Load records a visit, so
|
|
// loading it again here would count every download twice.
|
|
func (a *archiver) ZipShare(ctx context.Context, s *model.Share, out io.Writer) error {
|
|
if !s.Downloadable {
|
|
return model.ErrNotAuthorized
|
|
}
|
|
log.Debug(ctx, "Zipping share", "name", s.ID, "format", s.Format, "bitrate", s.MaxBitRate, "numTracks", len(s.Tracks))
|
|
return a.zipMediaFiles(ctx, s.ID, s.ID, s.Format, s.MaxBitRate, out, s.Tracks, false)
|
|
}
|
|
|
|
func (a *archiver) ZipPlaylist(ctx context.Context, id string, format string, bitrate int, out io.Writer) error {
|
|
pls, err := a.ds.Playlist(ctx).GetWithTracks(id, true, false)
|
|
if err != nil {
|
|
log.Error(ctx, "Error loading mediafiles from playlist", "id", id, err)
|
|
return err
|
|
}
|
|
mfs := pls.MediaFiles()
|
|
log.Debug(ctx, "Zipping playlist", "name", pls.Name, "format", format, "bitrate", bitrate, "numTracks", len(mfs))
|
|
return a.zipMediaFiles(ctx, id, pls.Name, format, bitrate, out, mfs, true)
|
|
}
|
|
|
|
func (a *archiver) zipMediaFiles(ctx context.Context, id, name string, format string, bitrate int, out io.Writer, mfs model.MediaFiles, addM3U bool) error {
|
|
z := createZipWriter(out, format, bitrate)
|
|
|
|
zippedMfs := make(model.MediaFiles, len(mfs))
|
|
for idx, mf := range mfs {
|
|
file := a.playlistFilename(mf, format, idx)
|
|
if addErr := a.addFileToZip(ctx, z, mf, format, bitrate, file); errors.Is(addErr, stream.ErrTooManyTranscodes) {
|
|
// Abort the whole archive: continuing would silently emit
|
|
// empty zip entries since the headers are already written.
|
|
_ = z.Close()
|
|
return addErr
|
|
}
|
|
mf.Path = file
|
|
zippedMfs[idx] = mf
|
|
}
|
|
|
|
// Add M3U file if requested
|
|
if addM3U && len(zippedMfs) > 0 {
|
|
plsName := str.SanitizeFilename(name)
|
|
w, err := z.CreateHeader(&zip.FileHeader{
|
|
Name: plsName + ".m3u",
|
|
Modified: mfs[0].UpdatedAt,
|
|
Method: zip.Store,
|
|
})
|
|
if err != nil {
|
|
log.Error(ctx, "Error creating playlist zip entry", err)
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write([]byte(zippedMfs.ToM3U8(plsName, false)))
|
|
if err != nil {
|
|
log.Error(ctx, "Error writing m3u in zip", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
err := z.Close()
|
|
if err != nil {
|
|
log.Error(ctx, "Error closing zip file", "id", id, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (a *archiver) playlistFilename(mf model.MediaFile, format string, idx int) string {
|
|
ext := mf.Suffix
|
|
if format != "" && format != "raw" {
|
|
ext = format
|
|
}
|
|
return fmt.Sprintf("%02d - %s - %s.%s", idx+1, str.SanitizeFilename(mf.Artist), str.SanitizeFilename(mf.Title), ext)
|
|
}
|
|
|
|
func (a *archiver) addFileToZip(ctx context.Context, z *zip.Writer, mf model.MediaFile, format string, bitrate int, filename string) error {
|
|
path := mf.AbsolutePath()
|
|
|
|
// Open the source before writing the zip entry header so a rejection
|
|
// (limiter, missing file, etc.) does not leave an empty entry in the
|
|
// archive.
|
|
var r io.ReadCloser
|
|
var err error
|
|
if format != "raw" && format != "" {
|
|
r, err = a.ms.NewStream(ctx, &mf, stream.Request{Format: format, BitRate: bitrate})
|
|
} else {
|
|
r, err = os.Open(path)
|
|
}
|
|
if err != nil {
|
|
log.Error(ctx, "Error opening file for zipping", "file", path, "format", format, err)
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err := r.Close(); err != nil && log.IsGreaterOrEqualTo(log.LevelDebug) {
|
|
log.Error(ctx, "Error closing stream", "id", mf.ID, "file", path, err)
|
|
}
|
|
}()
|
|
|
|
w, err := z.CreateHeader(&zip.FileHeader{
|
|
Name: filename,
|
|
Modified: mf.UpdatedAt,
|
|
Method: zip.Store,
|
|
})
|
|
if err != nil {
|
|
log.Error(ctx, "Error creating zip entry", "file", path, err)
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(w, r)
|
|
if err != nil {
|
|
log.Error(ctx, "Error zipping file", "file", path, err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|