- feat(library): walk each root, plan the changes, read tags with N parallel workers and write from a single goroutine - feat(library): detect renames by size and mtime only when the match is unambiguous; mark vanished files missing instead of deleting them - fix-safe: an unreadable root, an unreadable subdirectory or an interrupted scan never marks files as missing; symlinks are skipped - feat(library): event hub that fans scan progress out to subscribers without ever blocking the scan (ready for a websocket client) - feat(library): TagLib (WASM, no CGO) reader for MP3 and FLAC tags and audio properties, with title and artist fallbacks - feat(storage): library repository for SQLite and PostgreSQL - feat(api): POST/GET /api/v1/library/scan to start a scan and read its status - feat(config): library.scan_workers
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package library
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"go.senan.xyz/taglib"
|
|
)
|
|
|
|
// UnknownArtist is used when a file has no artist tag.
|
|
const UnknownArtist = "Unknown Artist"
|
|
|
|
// TaglibReader reads tags and audio properties with TagLib (compiled to
|
|
// WebAssembly, so there is no CGO). It is safe for concurrent use.
|
|
type TaglibReader struct{}
|
|
|
|
// Read implements TagReader.
|
|
func (TaglibReader) Read(path string) (Metadata, error) {
|
|
tags, err := taglib.ReadTags(path)
|
|
if err != nil {
|
|
return Metadata{}, fmt.Errorf("%w: %v", ErrUnreadable, err)
|
|
}
|
|
props, err := taglib.ReadProperties(path)
|
|
if err != nil {
|
|
return Metadata{}, fmt.Errorf("%w: %v", ErrUnreadable, err)
|
|
}
|
|
// TagLib does not fail on random bytes named .mp3; it just finds no audio.
|
|
if props.SampleRate == 0 || props.Length <= 0 {
|
|
return Metadata{}, fmt.Errorf("%w: no audio stream found", ErrUnreadable)
|
|
}
|
|
|
|
m := Metadata{
|
|
Tags: Tags{
|
|
Title: first(tags, "TITLE"),
|
|
Album: first(tags, "ALBUM"),
|
|
AlbumArtist: first(tags, "ALBUMARTIST"),
|
|
Artists: nonEmpty(tags["ARTIST"]),
|
|
TrackNo: leadingInt(first(tags, "TRACKNUMBER")),
|
|
DiscNo: leadingInt(first(tags, "DISCNUMBER")),
|
|
},
|
|
Properties: Properties{
|
|
Format: strings.ToLower(props.Format),
|
|
DurationMs: props.Length.Milliseconds(),
|
|
SampleRate: int(props.SampleRate),
|
|
BitRate: int(props.BitRate),
|
|
},
|
|
}
|
|
|
|
if m.Title == "" {
|
|
base := filepath.Base(path)
|
|
m.Title = strings.TrimSuffix(base, filepath.Ext(base))
|
|
}
|
|
if len(m.Artists) == 0 {
|
|
m.Artists = []string{UnknownArtist}
|
|
}
|
|
if m.AlbumArtist == "" {
|
|
m.AlbumArtist = m.Artists[0]
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func first(tags map[string][]string, key string) string {
|
|
for _, v := range tags[key] {
|
|
if v = strings.TrimSpace(v); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func nonEmpty(values []string) []string {
|
|
var out []string
|
|
for _, v := range values {
|
|
if v = strings.TrimSpace(v); v != "" {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// leadingInt parses "3" or "3/12" and returns 0 when there is no number.
|
|
func leadingInt(s string) int {
|
|
if i := strings.IndexByte(s, '/'); i >= 0 {
|
|
s = s[:i]
|
|
}
|
|
n, err := strconv.Atoi(strings.TrimSpace(s))
|
|
if err != nil || n < 0 {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|