- feat(streaming): open a track's file only after checking it against the
configured library roots: relative paths only, no "..", symlinks resolved on
both sides so none can leave the root, regular files only
- feat(api): GET and HEAD /api/v1/tracks/{id}/stream via http.ServeContent
(206, 416, Content-Length, conditional requests), MIME type from the
extension, nosniff, no-referrer and private caching headers
- feat(api): the stream route also accepts the access token as access_token in
the query string, because an audio element cannot send headers; no other
route does, and the query string is never logged
- feat(storage): PlayableFile picks the oldest file of a track that is not
flagged missing
- fix(api): internal errors set by handlers are now written to the request log
- test: range variants, HEAD, conditional requests, auth variants, traversal,
symlink escape, symlinked root, missing file, directory instead of file
328 lines
10 KiB
Go
328 lines
10 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
"codeberg.org/squidd/sonda/internal/streaming"
|
|
)
|
|
|
|
type artistModel struct {
|
|
bun.BaseModel `bun:"table:artists"`
|
|
|
|
ID int64 `bun:"id,pk,autoincrement"`
|
|
Name string `bun:"name,notnull"`
|
|
NameKey string `bun:"name_key,notnull"`
|
|
}
|
|
|
|
type albumModel struct {
|
|
bun.BaseModel `bun:"table:albums"`
|
|
|
|
ID int64 `bun:"id,pk,autoincrement"`
|
|
Title string `bun:"title,notnull"`
|
|
TitleKey string `bun:"title_key,notnull"`
|
|
ArtistID int64 `bun:"artist_id,nullzero"`
|
|
}
|
|
|
|
type trackModel struct {
|
|
bun.BaseModel `bun:"table:tracks"`
|
|
|
|
ID int64 `bun:"id,pk,autoincrement"`
|
|
Title string `bun:"title,notnull"`
|
|
DurationMs int64 `bun:"duration_ms,nullzero"`
|
|
TrackNo int `bun:"track_no,nullzero"`
|
|
DiscNo int `bun:"disc_no,nullzero"`
|
|
AlbumID int64 `bun:"album_id,nullzero"`
|
|
}
|
|
|
|
type trackArtistModel struct {
|
|
bun.BaseModel `bun:"table:track_artists"`
|
|
|
|
TrackID int64 `bun:"track_id,pk"`
|
|
ArtistID int64 `bun:"artist_id,pk"`
|
|
Role string `bun:"role,pk"`
|
|
}
|
|
|
|
type audioFileModel struct {
|
|
bun.BaseModel `bun:"table:audio_files"`
|
|
|
|
ID int64 `bun:"id,pk,autoincrement"`
|
|
TrackID int64 `bun:"track_id,notnull"`
|
|
Root string `bun:"root,notnull"`
|
|
RelPath string `bun:"rel_path,notnull"`
|
|
Format string `bun:"format,notnull"`
|
|
SizeBytes int64 `bun:"size_bytes,notnull"`
|
|
MTimeUnix int64 `bun:"mtime_unix,notnull"`
|
|
SampleRate int `bun:"sample_rate,nullzero"`
|
|
BitRate int `bun:"bitrate,nullzero"`
|
|
MissingAt sql.NullInt64 `bun:"missing_at"`
|
|
}
|
|
|
|
// LibraryRepo stores the scanned catalog. It implements library.Store.
|
|
type LibraryRepo struct{ db *bun.DB }
|
|
|
|
// NewLibraryRepo builds a LibraryRepo.
|
|
func NewLibraryRepo(db *bun.DB) *LibraryRepo { return &LibraryRepo{db: db} }
|
|
|
|
// ExistingFiles lists the files indexed under root, including missing ones.
|
|
func (r *LibraryRepo) ExistingFiles(ctx context.Context, root string) ([]library.ExistingFile, error) {
|
|
var rows []audioFileModel
|
|
if err := r.db.NewSelect().Model(&rows).Where("root = ?", root).Scan(ctx); err != nil {
|
|
return nil, fmt.Errorf("select audio files: %w", err)
|
|
}
|
|
out := make([]library.ExistingFile, len(rows))
|
|
for i, row := range rows {
|
|
out[i] = library.ExistingFile{
|
|
ID: row.ID,
|
|
FileInfo: library.FileInfo{RelPath: row.RelPath, Size: row.SizeBytes, MTimeUnix: row.MTimeUnix},
|
|
Missing: row.MissingAt.Valid,
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// SaveFile creates or updates one file with its track, album and artists in a
|
|
// single transaction. MusicBrainz ids are never invented: they stay NULL.
|
|
func (r *LibraryRepo) SaveFile(ctx context.Context, root string, f library.ParsedFile, existingID int64) (bool, error) {
|
|
var added bool
|
|
err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
artistIDs := make([]int64, 0, len(f.Artists))
|
|
seen := map[int64]bool{}
|
|
for _, name := range f.Artists {
|
|
id, err := findOrCreateArtist(ctx, tx, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !seen[id] {
|
|
seen[id] = true
|
|
artistIDs = append(artistIDs, id)
|
|
}
|
|
}
|
|
|
|
var albumID int64
|
|
if f.Album != "" {
|
|
albumArtistID, err := findOrCreateArtist(ctx, tx, firstNonEmpty(f.AlbumArtist, library.UnknownArtist))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if albumID, err = findOrCreateAlbum(ctx, tx, f.Album, albumArtistID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
track := &trackModel{
|
|
Title: f.Title, DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID,
|
|
}
|
|
|
|
if existingID == 0 {
|
|
if _, err := tx.NewInsert().Model(track).Exec(ctx); err != nil {
|
|
return fmt.Errorf("insert track: %w", err)
|
|
}
|
|
file := newAudioFile(root, f, track.ID)
|
|
if _, err := tx.NewInsert().Model(file).Exec(ctx); err != nil {
|
|
return fmt.Errorf("insert audio file: %w", err)
|
|
}
|
|
added = true
|
|
return replaceCredits(ctx, tx, track.ID, artistIDs)
|
|
}
|
|
|
|
var file audioFileModel
|
|
if err := tx.NewSelect().Model(&file).Where("id = ?", existingID).Scan(ctx); err != nil {
|
|
return fmt.Errorf("load audio file %d: %w", existingID, err)
|
|
}
|
|
track.ID = file.TrackID
|
|
if _, err := tx.NewUpdate().Model(track).
|
|
Column("title", "duration_ms", "track_no", "disc_no", "album_id").
|
|
WherePK().Exec(ctx); err != nil {
|
|
return fmt.Errorf("update track: %w", err)
|
|
}
|
|
file.Format, file.SizeBytes, file.MTimeUnix = f.Format, f.Size, f.MTimeUnix
|
|
file.SampleRate, file.BitRate = f.SampleRate, f.BitRate
|
|
file.MissingAt = sql.NullInt64{}
|
|
if _, err := tx.NewUpdate().Model(&file).
|
|
Column("format", "size_bytes", "mtime_unix", "sample_rate", "bitrate", "missing_at").
|
|
WherePK().Exec(ctx); err != nil {
|
|
return fmt.Errorf("update audio file: %w", err)
|
|
}
|
|
return replaceCredits(ctx, tx, track.ID, artistIDs)
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return added, nil
|
|
}
|
|
|
|
func newAudioFile(root string, f library.ParsedFile, trackID int64) *audioFileModel {
|
|
return &audioFileModel{
|
|
TrackID: trackID, Root: root, RelPath: f.RelPath, Format: f.Format,
|
|
SizeBytes: f.Size, MTimeUnix: f.MTimeUnix, SampleRate: f.SampleRate, BitRate: f.BitRate,
|
|
}
|
|
}
|
|
|
|
// RenameFile points an indexed file at a new relative path and clears its missing flag.
|
|
func (r *LibraryRepo) RenameFile(ctx context.Context, id int64, newRelPath string) error {
|
|
_, err := r.db.NewUpdate().Model((*audioFileModel)(nil)).
|
|
Set("rel_path = ?", newRelPath).
|
|
Set("missing_at = NULL").
|
|
Where("id = ?", id).Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("rename audio file: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MarkMissing flags files that were not found. Rows and tracks are kept.
|
|
func (r *LibraryRepo) MarkMissing(ctx context.Context, ids []int64, at time.Time) error {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
_, err := r.db.NewUpdate().Model((*audioFileModel)(nil)).
|
|
Set("missing_at = ?", at.Unix()).
|
|
Where("id IN (?)", bun.In(ids)).Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("mark missing: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Restore clears the missing flag of files that reappeared.
|
|
func (r *LibraryRepo) Restore(ctx context.Context, ids []int64) error {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
_, err := r.db.NewUpdate().Model((*audioFileModel)(nil)).
|
|
Set("missing_at = NULL").
|
|
Where("id IN (?)", bun.In(ids)).Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("restore files: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PlayableFile returns the file to play for a track: the oldest one that was
|
|
// not flagged missing. It returns streaming.ErrNotFound when the track does not
|
|
// exist or has no such file. It implements streaming.Source.
|
|
func (r *LibraryRepo) PlayableFile(ctx context.Context, trackID int64) (streaming.File, error) {
|
|
var row audioFileModel
|
|
err := r.db.NewSelect().Model(&row).
|
|
Where("track_id = ?", trackID).
|
|
Where("missing_at IS NULL").
|
|
OrderExpr("id").Limit(1).Scan(ctx)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return streaming.File{}, streaming.ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return streaming.File{}, fmt.Errorf("select playable file: %w", err)
|
|
}
|
|
return streaming.File{Root: row.Root, RelPath: row.RelPath}, nil
|
|
}
|
|
|
|
// RefreshMatchKeys recomputes name_key and title_key for every artist and album
|
|
// whose stored key differs from the current rule. It is idempotent and cheap
|
|
// when nothing changed, so it can run at every start: rows created before
|
|
// migration 0003 only have an approximate key from SQL lower().
|
|
func (r *LibraryRepo) RefreshMatchKeys(ctx context.Context) error {
|
|
return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
var artists []artistModel
|
|
if err := tx.NewSelect().Model(&artists).Column("id", "name", "name_key").Scan(ctx); err != nil {
|
|
return fmt.Errorf("list artists: %w", err)
|
|
}
|
|
for _, a := range artists {
|
|
key := matchKey(a.Name)
|
|
if key == a.NameKey {
|
|
continue
|
|
}
|
|
if _, err := tx.NewUpdate().Model((*artistModel)(nil)).
|
|
Set("name_key = ?", key).Where("id = ?", a.ID).Exec(ctx); err != nil {
|
|
return fmt.Errorf("update artist key: %w", err)
|
|
}
|
|
}
|
|
|
|
var albums []albumModel
|
|
if err := tx.NewSelect().Model(&albums).Column("id", "title", "title_key").Scan(ctx); err != nil {
|
|
return fmt.Errorf("list albums: %w", err)
|
|
}
|
|
for _, al := range albums {
|
|
key := matchKey(al.Title)
|
|
if key == al.TitleKey {
|
|
continue
|
|
}
|
|
if _, err := tx.NewUpdate().Model((*albumModel)(nil)).
|
|
Set("title_key = ?", key).Where("id = ?", al.ID).Exec(ctx); err != nil {
|
|
return fmt.Errorf("update album key: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// ---- helpers (run inside a transaction) ----
|
|
|
|
func findOrCreateArtist(ctx context.Context, tx bun.Tx, name string) (int64, error) {
|
|
var a artistModel
|
|
err := tx.NewSelect().Model(&a).Where("name_key = ?", matchKey(name)).OrderExpr("id").Limit(1).Scan(ctx)
|
|
if err == nil {
|
|
return a.ID, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return 0, fmt.Errorf("find artist: %w", err)
|
|
}
|
|
a = artistModel{Name: name, NameKey: matchKey(name)}
|
|
if _, err := tx.NewInsert().Model(&a).Exec(ctx); err != nil {
|
|
return 0, fmt.Errorf("insert artist: %w", err)
|
|
}
|
|
return a.ID, nil
|
|
}
|
|
|
|
func findOrCreateAlbum(ctx context.Context, tx bun.Tx, title string, artistID int64) (int64, error) {
|
|
var a albumModel
|
|
err := tx.NewSelect().Model(&a).
|
|
Where("title_key = ?", matchKey(title)).Where("artist_id = ?", artistID).
|
|
OrderExpr("id").Limit(1).Scan(ctx)
|
|
if err == nil {
|
|
return a.ID, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return 0, fmt.Errorf("find album: %w", err)
|
|
}
|
|
a = albumModel{Title: title, TitleKey: matchKey(title), ArtistID: artistID}
|
|
if _, err := tx.NewInsert().Model(&a).Exec(ctx); err != nil {
|
|
return 0, fmt.Errorf("insert album: %w", err)
|
|
}
|
|
return a.ID, nil
|
|
}
|
|
|
|
func replaceCredits(ctx context.Context, tx bun.Tx, trackID int64, artistIDs []int64) error {
|
|
if _, err := tx.NewDelete().Model((*trackArtistModel)(nil)).Where("track_id = ?", trackID).Exec(ctx); err != nil {
|
|
return fmt.Errorf("clear credits: %w", err)
|
|
}
|
|
for _, id := range artistIDs {
|
|
row := &trackArtistModel{TrackID: trackID, ArtistID: id, Role: "main"}
|
|
if _, err := tx.NewInsert().Model(row).Exec(ctx); err != nil {
|
|
return fmt.Errorf("insert credit: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// matchKey is the case-insensitive key used to match artist and album names.
|
|
// It is computed here, not in SQL, so SQLite and PostgreSQL agree on Unicode.
|
|
func matchKey(s string) string {
|
|
return strings.ToLower(strings.TrimSpace(s))
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, v := range values {
|
|
if v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|