release / web (push) Successful in 1m13s
release / go (push) Successful in 1m46s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m52s
release / android (push) Successful in 4m11s
release / Build signed APK (push) Successful in 4m42s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
A track tagged "Low Tide Choir feat. Ruby Calloway" was filed under an artist of that whole name, with its own page, its own place in the artist list and its own "Songs like" mix. It is now filed under Low Tide Choir and keeps "Low Tide Choir feat. Ruby Calloway" as its credit, shown wherever the track is listed, sent to Subsonic apps and scrobbled. A collaboration is split only when the file lists its artists one by one (Picard's ARTISTS tag, read from FLAC, Ogg, Opus, MP3 and M4A). "A & B" on its own may be one band, so it stays whole. A Vorbis file's artist id is now its first, not the guest's. Tag-read version 10 re-reads the library once; the scan's tidy-up removes the credit-named artists it leaves empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
6.8 KiB
Go
227 lines
6.8 KiB
Go
package library
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// Why this file exists: Vorbis comments (FLAC, Ogg Vorbis, Opus) say a field
|
|
// has several values by repeating it:
|
|
//
|
|
// GENRE=Boom Bap
|
|
// GENRE=Downtempo
|
|
//
|
|
// dhowden/tag stores comments in a map keyed by field name
|
|
// (vorbis.go: m.c[strings.ToLower(k)] = v), so each repeat overwrites the last
|
|
// and only the final value survives. An album that declares eight genres
|
|
// would be stored under the last alone; nothing would look corrupt, and the
|
|
// other seven would never be seen by browse or the taste profile.
|
|
//
|
|
// The comment block is read here directly, as the ID3v2 genre frame is in
|
|
// id3v2genre.go, for the fields where a repeat matters: GENRE, and the
|
|
// ARTISTS and MUSICBRAINZ_ARTISTID lists an artist credit is read from
|
|
// (artistcredit.go). Every other field still comes from dhowden/tag.
|
|
|
|
// maxVorbisCommentSize caps a comment block or packet. Comments are kilobytes;
|
|
// an embedded METADATA_BLOCK_PICTURE can take them to a few megabytes. The cap
|
|
// only stops a corrupt length from making the scanner allocate wildly.
|
|
const maxVorbisCommentSize = 16 << 20
|
|
|
|
var errMalformedComment = errors.New("library: malformed Vorbis comment")
|
|
|
|
// readFLACGenreValues returns every GENRE value in a FLAC file's comment block.
|
|
// (nil, nil) means the file is readable and declares no genre.
|
|
func readFLACGenreValues(rs io.ReadSeeker) ([]string, error) { return readFLACValues(rs, "GENRE") }
|
|
|
|
// readOggGenreValues returns every GENRE value in an Ogg Vorbis or Opus file.
|
|
func readOggGenreValues(rs io.ReadSeeker) ([]string, error) { return readOggValues(rs, "GENRE") }
|
|
|
|
// readFLACValues returns every value of the comment field key in a FLAC
|
|
// file's comment block, in file order. (nil, nil) means the file is readable
|
|
// and has no such field. rs is seeked to the start, so it is safe to call after
|
|
// dhowden/tag has consumed the reader.
|
|
func readFLACValues(rs io.ReadSeeker, key string) ([]string, error) {
|
|
if _, err := rs.Seek(0, io.SeekStart); err != nil {
|
|
return nil, err
|
|
}
|
|
var magic [4]byte
|
|
if _, err := io.ReadFull(rs, magic[:]); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
// Some taggers prepend an ID3v2 tag to a FLAC. The FLAC stream starts after
|
|
// it.
|
|
if string(magic[:3]) == "ID3" {
|
|
var rest [6]byte
|
|
if _, err := io.ReadFull(rs, rest[:]); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
size := syncsafeInt(rest[2:6])
|
|
if size < 0 {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
skip := int64(10 + size)
|
|
if rest[1]&0x10 != 0 {
|
|
skip += 10 // 2.4 footer
|
|
}
|
|
if _, err := rs.Seek(skip, io.SeekStart); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
if _, err := io.ReadFull(rs, magic[:]); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
}
|
|
if string(magic[:]) != "fLaC" {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
|
|
for {
|
|
var hdr [4]byte
|
|
if _, err := io.ReadFull(rs, hdr[:]); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
last := hdr[0]&0x80 != 0
|
|
blockType := hdr[0] & 0x7F
|
|
size := int64(hdr[1])<<16 | int64(hdr[2])<<8 | int64(hdr[3])
|
|
switch blockType {
|
|
case 4: // VORBIS_COMMENT
|
|
body := make([]byte, size)
|
|
if _, err := io.ReadFull(rs, body); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
return vorbisCommentValues(body, key)
|
|
case 127: // invalid by spec; nothing after it can be trusted
|
|
return nil, errNoGenreFrame
|
|
}
|
|
if last {
|
|
return nil, nil // no comment block at all
|
|
}
|
|
if _, err := rs.Seek(size, io.SeekCurrent); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
}
|
|
}
|
|
|
|
// readOggValues returns every value of the comment field key in an Ogg Vorbis
|
|
// or Opus file. The comments are the stream's second packet, which can span
|
|
// several pages when it carries cover art, so pages are reassembled until it
|
|
// is complete. Only the first logical stream is read; pages of any other are
|
|
// skipped.
|
|
func readOggValues(rs io.ReadSeeker, key string) ([]string, error) {
|
|
if _, err := rs.Seek(0, io.SeekStart); err != nil {
|
|
return nil, err
|
|
}
|
|
br := bufio.NewReader(rs)
|
|
var (
|
|
serial uint32
|
|
haveFirst bool
|
|
packet []byte
|
|
packetIdx int
|
|
)
|
|
for {
|
|
var hdr [27]byte
|
|
if _, err := io.ReadFull(br, hdr[:]); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
if string(hdr[0:4]) != "OggS" {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
pageSerial := binary.LittleEndian.Uint32(hdr[14:18])
|
|
segments := make([]byte, hdr[26])
|
|
if _, err := io.ReadFull(br, segments); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
if !haveFirst {
|
|
serial, haveFirst = pageSerial, true
|
|
}
|
|
if pageSerial != serial {
|
|
var n int64
|
|
for _, l := range segments {
|
|
n += int64(l)
|
|
}
|
|
if _, err := io.CopyN(io.Discard, br, n); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
continue
|
|
}
|
|
for _, l := range segments {
|
|
if len(packet)+int(l) > maxVorbisCommentSize {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
seg := make([]byte, l)
|
|
if _, err := io.ReadFull(br, seg); err != nil {
|
|
return nil, errNoGenreFrame
|
|
}
|
|
packet = append(packet, seg...)
|
|
if l == 255 {
|
|
continue // the packet carries on in the next segment
|
|
}
|
|
if packetIdx == 1 {
|
|
return oggCommentValues(packet, key)
|
|
}
|
|
packetIdx++
|
|
packet = packet[:0]
|
|
}
|
|
}
|
|
}
|
|
|
|
// oggCommentValues strips the codec's comment-packet prefix. Ogg FLAC and Speex
|
|
// are rare enough to leave to dhowden/tag.
|
|
func oggCommentValues(packet []byte, key string) ([]string, error) {
|
|
switch {
|
|
case bytes.HasPrefix(packet, []byte("\x03vorbis")):
|
|
return vorbisCommentValues(packet[7:], key)
|
|
case bytes.HasPrefix(packet, []byte("OpusTags")):
|
|
return vorbisCommentValues(packet[8:], key)
|
|
}
|
|
return nil, errNoGenreFrame
|
|
}
|
|
|
|
// vorbisCommentValues reads a comment block (vendor string, then a count of
|
|
// length-prefixed "KEY=value" entries, little-endian lengths throughout) and
|
|
// returns the values of field key in file order. Field names are
|
|
// case-insensitive by spec. Anything after the last entry, such as Vorbis's
|
|
// framing bit, is ignored.
|
|
func vorbisCommentValues(b []byte, key string) ([]string, error) {
|
|
next := func() ([]byte, bool) {
|
|
if len(b) < 4 {
|
|
return nil, false
|
|
}
|
|
n := binary.LittleEndian.Uint32(b)
|
|
if uint64(n) > uint64(len(b)-4) {
|
|
return nil, false
|
|
}
|
|
field := b[4 : 4+n]
|
|
b = b[4+n:]
|
|
return field, true
|
|
}
|
|
if _, ok := next(); !ok { // vendor string
|
|
return nil, errMalformedComment
|
|
}
|
|
if len(b) < 4 {
|
|
return nil, errMalformedComment
|
|
}
|
|
count := binary.LittleEndian.Uint32(b)
|
|
b = b[4:]
|
|
var out []string
|
|
// Each entry costs at least four bytes, so a corrupt count runs out of
|
|
// input long before it runs up a loop.
|
|
for i := uint32(0); i < count; i++ {
|
|
field, ok := next()
|
|
if !ok {
|
|
return nil, errMalformedComment
|
|
}
|
|
name, value, found := strings.Cut(string(field), "=")
|
|
if !found || !strings.EqualFold(name, key) {
|
|
continue
|
|
}
|
|
if value = strings.TrimSpace(value); value != "" {
|
|
out = append(out, value)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|