Add album/track releases with audio analysis, AnalyzeAlbumRelease RPC, Docker path auto-resolution, release parsing decision tree
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type AlbumRelease struct {
|
||||
ID string
|
||||
AlbumID string
|
||||
DownloadID string
|
||||
Format string
|
||||
BitDepth *int
|
||||
SampleRate *int
|
||||
Channels int
|
||||
IsLossless bool
|
||||
Source *string
|
||||
TotalSize int64
|
||||
TotalDurationMs int
|
||||
TrackCount int
|
||||
HasCoverArt bool
|
||||
HasCueSheet bool
|
||||
HasRipLog bool
|
||||
Path string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type AlbumReleaseRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAlbumReleaseRepository(pool *pgxpool.Pool) *AlbumReleaseRepository {
|
||||
return &AlbumReleaseRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *AlbumReleaseRepository) Create(ctx context.Context, ar *AlbumRelease) error {
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`INSERT INTO album_releases (album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
||||
RETURNING id, created_at`,
|
||||
ar.AlbumID, ar.DownloadID, ar.Format, ar.BitDepth, ar.SampleRate, ar.Channels, ar.IsLossless, ar.Source, ar.TotalSize, ar.TotalDurationMs, ar.TrackCount, ar.HasCoverArt, ar.HasCueSheet, ar.HasRipLog, ar.Path,
|
||||
).Scan(&ar.ID, &ar.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating album release: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AlbumReleaseRepository) GetByAlbumID(ctx context.Context, albumID string) ([]*AlbumRelease, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path, created_at
|
||||
FROM album_releases WHERE album_id = $1 ORDER BY created_at DESC`, albumID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing album releases: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var releases []*AlbumRelease
|
||||
for rows.Next() {
|
||||
ar := &AlbumRelease{}
|
||||
if err := rows.Scan(&ar.ID, &ar.AlbumID, &ar.DownloadID, &ar.Format, &ar.BitDepth, &ar.SampleRate, &ar.Channels, &ar.IsLossless, &ar.Source, &ar.TotalSize, &ar.TotalDurationMs, &ar.TrackCount, &ar.HasCoverArt, &ar.HasCueSheet, &ar.HasRipLog, &ar.Path, &ar.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning album release: %w", err)
|
||||
}
|
||||
releases = append(releases, ar)
|
||||
}
|
||||
return releases, nil
|
||||
}
|
||||
|
||||
func (r *AlbumReleaseRepository) GetByDownloadID(ctx context.Context, downloadID string) (*AlbumRelease, error) {
|
||||
ar := &AlbumRelease{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path, created_at
|
||||
FROM album_releases WHERE download_id = $1`, downloadID,
|
||||
).Scan(&ar.ID, &ar.AlbumID, &ar.DownloadID, &ar.Format, &ar.BitDepth, &ar.SampleRate, &ar.Channels, &ar.IsLossless, &ar.Source, &ar.TotalSize, &ar.TotalDurationMs, &ar.TrackCount, &ar.HasCoverArt, &ar.HasCueSheet, &ar.HasRipLog, &ar.Path, &ar.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting album release by download: %w", err)
|
||||
}
|
||||
return ar, nil
|
||||
}
|
||||
|
||||
func (r *AlbumReleaseRepository) DeleteByDownloadID(ctx context.Context, downloadID string) error {
|
||||
_, err := r.pool.Exec(ctx, `DELETE FROM album_releases WHERE download_id = $1`, downloadID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleting album release by download: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -141,6 +141,18 @@ func (r *DownloadRepository) GetActive(ctx context.Context) ([]*Download, error)
|
||||
return downloads, nil
|
||||
}
|
||||
|
||||
func (r *DownloadRepository) GetByID(ctx context.Context, id string) (*Download, error) {
|
||||
d := &Download{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, torrent_id, album_id, format, quality, state, qbit_hash, save_path, error_message, queued_at, started_at, completed_at, created_at, updated_at
|
||||
FROM downloads WHERE id = $1`, id,
|
||||
).Scan(&d.ID, &d.TorrentID, &d.AlbumID, &d.Format, &d.Quality, &d.State, &d.QbitHash, &d.SavePath, &d.ErrorMessage, &d.QueuedAt, &d.StartedAt, &d.CompletedAt, &d.CreatedAt, &d.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting download by id: %w", err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (r *DownloadRepository) HasAlbumInQuality(ctx context.Context, albumID string, format string, quality string) (bool, error) {
|
||||
var exists bool
|
||||
err := r.pool.QueryRow(ctx,
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type TrackRelease struct {
|
||||
ID string
|
||||
AlbumReleaseID string
|
||||
TrackID *string
|
||||
DownloadFileID *string
|
||||
Title string
|
||||
TrackNumber int
|
||||
DiscNumber int
|
||||
DurationMs *int
|
||||
Format string
|
||||
BitDepth *int
|
||||
SampleRate *int
|
||||
Channels int
|
||||
BitrateKbps *int
|
||||
FileSize int64
|
||||
FilePath string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type TrackReleaseRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewTrackReleaseRepository(pool *pgxpool.Pool) *TrackReleaseRepository {
|
||||
return &TrackReleaseRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *TrackReleaseRepository) Create(ctx context.Context, tr *TrackRelease) error {
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`INSERT INTO track_releases (album_release_id, track_id, download_file_id, title, track_number, disc_number, duration_ms, format, bit_depth, sample_rate, channels, bitrate_kbps, file_size, file_path)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
RETURNING id, created_at`,
|
||||
tr.AlbumReleaseID, tr.TrackID, tr.DownloadFileID, tr.Title, tr.TrackNumber, tr.DiscNumber, tr.DurationMs, tr.Format, tr.BitDepth, tr.SampleRate, tr.Channels, tr.BitrateKbps, tr.FileSize, tr.FilePath,
|
||||
).Scan(&tr.ID, &tr.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating track release: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TrackReleaseRepository) CreateBatch(ctx context.Context, tracks []*TrackRelease) error {
|
||||
for _, tr := range tracks {
|
||||
if err := r.Create(ctx, tr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TrackReleaseRepository) GetByAlbumReleaseID(ctx context.Context, albumReleaseID string) ([]*TrackRelease, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, album_release_id, track_id, download_file_id, title, track_number, disc_number, duration_ms, format, bit_depth, sample_rate, channels, bitrate_kbps, file_size, file_path, created_at
|
||||
FROM track_releases WHERE album_release_id = $1 ORDER BY disc_number, track_number`, albumReleaseID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing track releases: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tracks []*TrackRelease
|
||||
for rows.Next() {
|
||||
tr := &TrackRelease{}
|
||||
if err := rows.Scan(&tr.ID, &tr.AlbumReleaseID, &tr.TrackID, &tr.DownloadFileID, &tr.Title, &tr.TrackNumber, &tr.DiscNumber, &tr.DurationMs, &tr.Format, &tr.BitDepth, &tr.SampleRate, &tr.Channels, &tr.BitrateKbps, &tr.FileSize, &tr.FilePath, &tr.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning track release: %w", err)
|
||||
}
|
||||
tracks = append(tracks, tr)
|
||||
}
|
||||
return tracks, nil
|
||||
}
|
||||
Reference in New Issue
Block a user