/rest speaks the OpenSubsonic API (Subsonic 1.16.1) so that other apps can browse the music of a server. A call logs in with the password of a device: an apiKey, Subsonic's token (u, t, s) or the password itself (u, p). The account password is never accepted there. - devices: ByKey, ByToken, ByPassword and the note of last use - opensubsonic package: JSON and XML answers, ping, getLicense, getOpenSubsonicExtensions, tokenInfo, getMusicFolders, getArtists, getArtist, getAlbum, getSong, getGenres - storage.SubsonicRepo: year, genres, size, bitrate, suffix and added date
149 lines
4.5 KiB
Go
149 lines
4.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/opensubsonic"
|
|
)
|
|
|
|
// SubsonicRepo reads what the OpenSubsonic API needs and the catalogue does not carry. It implements
|
|
// opensubsonic.Extras. Only files that are not missing count, like everywhere else.
|
|
type SubsonicRepo struct{ db *bun.DB }
|
|
|
|
// NewSubsonicRepo builds a SubsonicRepo.
|
|
func NewSubsonicRepo(db *bun.DB) *SubsonicRepo { return &SubsonicRepo{db: db} }
|
|
|
|
// Tracks returns the extras of the songs that can be played, with three queries whatever the number of songs.
|
|
func (r *SubsonicRepo) Tracks(ctx context.Context, ids []int64) (map[int64]opensubsonic.TrackExtra, error) {
|
|
out := map[int64]opensubsonic.TrackExtra{}
|
|
if len(ids) == 0 {
|
|
return out, nil
|
|
}
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT f.track_id, f.id, f.rel_path, f.size_bytes, COALESCE(f.bitrate, 0), f.mtime_unix, COALESCE(t.year, 0)
|
|
FROM audio_files f JOIN tracks t ON t.id = f.track_id
|
|
WHERE f.track_id IN (?) AND f.missing_at IS NULL ORDER BY f.id`, bun.In(ids))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read the files of songs: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var (
|
|
trackID, fileID, size, mtime int64
|
|
rel string
|
|
bitrate, year int
|
|
)
|
|
if err := rows.Scan(&trackID, &fileID, &rel, &size, &bitrate, &mtime, &year); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, seen := out[trackID]; seen { // the first file that can be played says it
|
|
continue
|
|
}
|
|
out[trackID] = opensubsonic.TrackExtra{
|
|
Year: year, Size: size, BitrateKbps: bitrate,
|
|
Suffix: strings.TrimPrefix(strings.ToLower(path.Ext(rel)), "."), AddedAt: time.Unix(mtime, 0),
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
genres, err := r.genresOf(ctx, `SELECT g.track_id, g.genre FROM track_genres g WHERE g.track_id IN (?) ORDER BY g.track_id, g.genre`, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for id, g := range genres {
|
|
if e, ok := out[id]; ok {
|
|
e.Genres = g
|
|
out[id] = e
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Albums returns the extras of the albums that can be played.
|
|
func (r *SubsonicRepo) Albums(ctx context.Context, ids []int64) (map[int64]opensubsonic.AlbumExtra, error) {
|
|
out := map[int64]opensubsonic.AlbumExtra{}
|
|
if len(ids) == 0 {
|
|
return out, nil
|
|
}
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT t.album_id, COALESCE(MIN(CASE WHEN t.year > 0 THEN t.year END), 0), MIN(f.mtime_unix)
|
|
FROM tracks t JOIN audio_files f ON f.track_id = t.id AND f.missing_at IS NULL
|
|
WHERE t.album_id IN (?) GROUP BY t.album_id`, bun.In(ids))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read the albums: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id, year, mtime int64
|
|
if err := rows.Scan(&id, &year, &mtime); err != nil {
|
|
return nil, err
|
|
}
|
|
out[id] = opensubsonic.AlbumExtra{Year: int(year), AddedAt: time.Unix(mtime, 0)}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
genres, err := r.genresOf(ctx,
|
|
`SELECT DISTINCT t.album_id, g.genre FROM track_genres g JOIN tracks t ON t.id = g.track_id
|
|
WHERE t.album_id IN (?) ORDER BY t.album_id, g.genre`, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for id, g := range genres {
|
|
if e, ok := out[id]; ok {
|
|
e.Genres = g
|
|
out[id] = e
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// genresOf runs a query of (id, genre) rows, in order, and gathers the genres of each id.
|
|
func (r *SubsonicRepo) genresOf(ctx context.Context, query string, ids []int64) (map[int64][]string, error) {
|
|
rows, err := r.db.QueryContext(ctx, query, bun.In(ids))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read genres: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
out := map[int64][]string{}
|
|
for rows.Next() {
|
|
var id int64
|
|
var genre string
|
|
if err := rows.Scan(&id, &genre); err != nil {
|
|
return nil, err
|
|
}
|
|
out[id] = append(out[id], genre)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// Genres returns every genre of the songs that can be played, with the songs and the albums that have it.
|
|
func (r *SubsonicRepo) Genres(ctx context.Context) ([]opensubsonic.Genre, error) {
|
|
vis, _ := visible("t", 0)
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT g.genre, COUNT(DISTINCT t.id), COUNT(DISTINCT t.album_id)
|
|
FROM track_genres g JOIN tracks t ON t.id = g.track_id
|
|
WHERE `+vis+` GROUP BY g.genre ORDER BY g.genre`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read genres: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
out := []opensubsonic.Genre{}
|
|
for rows.Next() {
|
|
var g opensubsonic.Genre
|
|
if err := rows.Scan(&g.Name, &g.Songs, &g.Albums); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, g)
|
|
}
|
|
return out, rows.Err()
|
|
}
|