feat(api): scaffold media helpers for cover + stream endpoints

This commit is contained in:
2026-04-21 22:30:25 -04:00
parent 8da51a1d13
commit c799e5c1c9
3 changed files with 276 additions and 0 deletions
+35
View File
@@ -2,6 +2,9 @@ package api
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -85,3 +88,35 @@ func seedTrack(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID,
}
return tr
}
// seedTrackWithFile creates a fresh temp directory, writes fileBody to
// <dir>/<title>.<ext>, and inserts a Track row whose file_path points at it.
// Returns the inserted Track and the directory (callers drop sidecar covers
// into this dir when a test needs them). FileFormat is ext lowercased.
func seedTrackWithFile(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string, fileBody []byte, ext string) (dbq.Track, string) {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, title+"."+ext)
if err := os.WriteFile(path, fileBody, 0o644); err != nil {
t.Fatalf("WriteFile(%s): %v", path, err)
}
one := int32(1)
tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: title,
AlbumID: albumID,
ArtistID: artistID,
TrackNumber: &one,
DiscNumber: nil,
DurationMs: int32(len(fileBody)),
FilePath: path,
FileSize: int64(len(fileBody)),
FileFormat: strings.ToLower(ext),
Bitrate: nil,
Mbid: nil,
Genre: nil,
})
if err != nil {
t.Fatalf("UpsertTrack(%s): %v", title, err)
}
return tr, dir
}