Files
MusicServer/internal/db/repository/song.sql.go
T
Sansan 090e794460 Restructure backend into services and fix security, crash and sync issues
Security
- Serve character images through os.Root so names like ../ can't escape
  the characters folder
- Sync start and /sync/reset are POST only (legacy and /api/v1)
- /api/v1/token/cleanup requires a token, password compared in constant
  time, tokens no longer logged
- Echo's rate limiter replaces the custom one; TRUSTED_PROXIES enables
  X-Forwarded-For for reverse proxies

Crashes and races
- Sync state lives in SyncService with atomics and a mutex instead of
  unsynchronised globals (fixes concurrent map writes during full sync)
- Only one sync can run; /api/v1 sync and music routes are locked during
  sync like the legacy ones
- No log.Fatal outside startup: errors become 4xx/5xx responses
- Search uses an in-order character match instead of unescaped regexes
- Empty queue / no songs return 404 instead of panicking or looping

Behaviour
- Sync result is computed once and stays the same between polls
- Swagger and /openapi serve the embedded spec
- Shutdown stops HTTP before closing the database
- Expired tokens are deleted with a context that outlives the request
- Files are served with Range support and correct content types; audio
  has no write deadline

Performance
- Migration 000006 adds soundtrack.fingerprint (names, sizes, mtimes) so
  unchanged folders are skipped without reading any files
- Songs are reconciled per soundtrack with one query plus needed writes;
  renames keep play counts
- Moved MUSIC_PATH updates soundtrack and song paths
- .id cleanup only runs for changed soundtracks or a full sync
- Classic random uses one SQL query; Gitea release lookups are cached
  with a timeout

Structure
- internal/config reads all environment variables once
- sqlc emits a Querier interface; backend globals become services
  injected into handlers
- One route registration shared by legacy and /api/v1
- Removed dead code (dbHelper.go, unused middleware, duplicate Gitea
  functions, embedded Swagger UI copy) and lib/pq
- templ and swag pinned as go tools
2026-10-06 15:20:14 +02:00

386 lines
9.5 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: song.sql
package repository
import (
"context"
"github.com/google/uuid"
)
const addHashToSong = `-- name: AddHashToSong :exec
UPDATE song SET hash=$1 where soundtrack_id = $2 AND path = $3
`
type AddHashToSongParams struct {
Hash string `json:"hash"`
SoundtrackID int32 `json:"soundtrack_id"`
Path string `json:"path"`
}
func (q *Queries) AddHashToSong(ctx context.Context, arg AddHashToSongParams) error {
_, err := q.db.Exec(ctx, addHashToSong, arg.Hash, arg.SoundtrackID, arg.Path)
return err
}
const addSong = `-- name: AddSong :exec
INSERT INTO song(soundtrack_id, song_name, path, file_name, hash, uuid, soundtrack_uuid) VALUES ($1, $2, $3, $4, $5, $6, $7)
`
type AddSongParams struct {
SoundtrackID int32 `json:"soundtrack_id"`
SongName string `json:"song_name"`
Path string `json:"path"`
FileName *string `json:"file_name"`
Hash string `json:"hash"`
Uuid uuid.UUID `json:"uuid"`
SoundtrackUuid uuid.UUID `json:"soundtrack_uuid"`
}
func (q *Queries) AddSong(ctx context.Context, arg AddSongParams) error {
_, err := q.db.Exec(ctx, addSong,
arg.SoundtrackID,
arg.SongName,
arg.Path,
arg.FileName,
arg.Hash,
arg.Uuid,
arg.SoundtrackUuid,
)
return err
}
const addSongPlayed = `-- name: AddSongPlayed :exec
UPDATE song SET times_played = times_played + 1
WHERE soundtrack_id = $1 AND song_name = $2
`
type AddSongPlayedParams struct {
SoundtrackID int32 `json:"soundtrack_id"`
SongName string `json:"song_name"`
}
func (q *Queries) AddSongPlayed(ctx context.Context, arg AddSongPlayedParams) error {
_, err := q.db.Exec(ctx, addSongPlayed, arg.SoundtrackID, arg.SongName)
return err
}
const checkSong = `-- name: CheckSong :one
SELECT COUNT(*) FROM song WHERE soundtrack_id = $1 AND path = $2
`
type CheckSongParams struct {
SoundtrackID int32 `json:"soundtrack_id"`
Path string `json:"path"`
}
func (q *Queries) CheckSong(ctx context.Context, arg CheckSongParams) (int64, error) {
row := q.db.QueryRow(ctx, checkSong, arg.SoundtrackID, arg.Path)
var count int64
err := row.Scan(&count)
return count, err
}
const checkSongWithHash = `-- name: CheckSongWithHash :one
SELECT COUNT(*) FROM song WHERE hash = $1
`
func (q *Queries) CheckSongWithHash(ctx context.Context, hash string) (int64, error) {
row := q.db.QueryRow(ctx, checkSongWithHash, hash)
var count int64
err := row.Scan(&count)
return count, err
}
const clearSongs = `-- name: ClearSongs :exec
DELETE FROM song
`
func (q *Queries) ClearSongs(ctx context.Context) error {
_, err := q.db.Exec(ctx, clearSongs)
return err
}
const clearSongsBySoundtrackId = `-- name: ClearSongsBySoundtrackId :exec
DELETE FROM song WHERE soundtrack_id = $1
`
func (q *Queries) ClearSongsBySoundtrackId(ctx context.Context, soundtrackID int32) error {
_, err := q.db.Exec(ctx, clearSongsBySoundtrackId, soundtrackID)
return err
}
const fetchAllSongs = `-- name: FetchAllSongs :many
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, uuid, soundtrack_uuid FROM song
`
func (q *Queries) FetchAllSongs(ctx context.Context) ([]Song, error) {
rows, err := q.db.Query(ctx, fetchAllSongs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Song
for rows.Next() {
var i Song
if err := rows.Scan(
&i.SoundtrackID,
&i.SongName,
&i.Path,
&i.TimesPlayed,
&i.Hash,
&i.FileName,
&i.Uuid,
&i.SoundtrackUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const findSongsFromSoundtrack = `-- name: FindSongsFromSoundtrack :many
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, uuid, soundtrack_uuid
FROM song
WHERE soundtrack_id = $1
`
func (q *Queries) FindSongsFromSoundtrack(ctx context.Context, soundtrackID int32) ([]Song, error) {
rows, err := q.db.Query(ctx, findSongsFromSoundtrack, soundtrackID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Song
for rows.Next() {
var i Song
if err := rows.Scan(
&i.SoundtrackID,
&i.SongName,
&i.Path,
&i.TimesPlayed,
&i.Hash,
&i.FileName,
&i.Uuid,
&i.SoundtrackUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRandomActiveSong = `-- name: GetRandomActiveSong :one
SELECT song.soundtrack_id, song.song_name, song.path, song.times_played, song.hash, song.file_name, song.uuid, song.soundtrack_uuid, soundtrack.path AS soundtrack_path
FROM song
JOIN soundtrack ON soundtrack.id = song.soundtrack_id
WHERE soundtrack.deleted IS NULL
ORDER BY random()
LIMIT 1
`
type GetRandomActiveSongRow struct {
Song Song `json:"song"`
SoundtrackPath string `json:"soundtrack_path"`
}
func (q *Queries) GetRandomActiveSong(ctx context.Context) (GetRandomActiveSongRow, error) {
row := q.db.QueryRow(ctx, getRandomActiveSong)
var i GetRandomActiveSongRow
err := row.Scan(
&i.Song.SoundtrackID,
&i.Song.SongName,
&i.Song.Path,
&i.Song.TimesPlayed,
&i.Song.Hash,
&i.Song.FileName,
&i.Song.Uuid,
&i.Song.SoundtrackUuid,
&i.SoundtrackPath,
)
return i, err
}
const getSongByUUID = `-- name: GetSongByUUID :one
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, uuid, soundtrack_uuid FROM song WHERE uuid = $1
`
func (q *Queries) GetSongByUUID(ctx context.Context, argUuid uuid.UUID) (Song, error) {
row := q.db.QueryRow(ctx, getSongByUUID, argUuid)
var i Song
err := row.Scan(
&i.SoundtrackID,
&i.SongName,
&i.Path,
&i.TimesPlayed,
&i.Hash,
&i.FileName,
&i.Uuid,
&i.SoundtrackUuid,
)
return i, err
}
const getSongUUIDsBySoundtrackId = `-- name: GetSongUUIDsBySoundtrackId :many
SELECT uuid, soundtrack_uuid FROM song WHERE soundtrack_id = $1
`
type GetSongUUIDsBySoundtrackIdRow struct {
Uuid uuid.UUID `json:"uuid"`
SoundtrackUuid uuid.UUID `json:"soundtrack_uuid"`
}
func (q *Queries) GetSongUUIDsBySoundtrackId(ctx context.Context, soundtrackID int32) ([]GetSongUUIDsBySoundtrackIdRow, error) {
rows, err := q.db.Query(ctx, getSongUUIDsBySoundtrackId, soundtrackID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetSongUUIDsBySoundtrackIdRow
for rows.Next() {
var i GetSongUUIDsBySoundtrackIdRow
if err := rows.Scan(&i.Uuid, &i.SoundtrackUuid); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getSongWithHash = `-- name: GetSongWithHash :one
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, uuid, soundtrack_uuid FROM song WHERE hash = $1
`
func (q *Queries) GetSongWithHash(ctx context.Context, hash string) (Song, error) {
row := q.db.QueryRow(ctx, getSongWithHash, hash)
var i Song
err := row.Scan(
&i.SoundtrackID,
&i.SongName,
&i.Path,
&i.TimesPlayed,
&i.Hash,
&i.FileName,
&i.Uuid,
&i.SoundtrackUuid,
)
return i, err
}
const getSongsBySoundtrackUUID = `-- name: GetSongsBySoundtrackUUID :many
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, uuid, soundtrack_uuid
FROM song
WHERE soundtrack_uuid = $1
`
func (q *Queries) GetSongsBySoundtrackUUID(ctx context.Context, soundtrackUuid uuid.UUID) ([]Song, error) {
rows, err := q.db.Query(ctx, getSongsBySoundtrackUUID, soundtrackUuid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Song
for rows.Next() {
var i Song
if err := rows.Scan(
&i.SoundtrackID,
&i.SongName,
&i.Path,
&i.TimesPlayed,
&i.Hash,
&i.FileName,
&i.Uuid,
&i.SoundtrackUuid,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const removeBrokenSong = `-- name: RemoveBrokenSong :exec
DELETE FROM song WHERE soundtrack_id = $1 AND path = $2
`
type RemoveBrokenSongParams struct {
SoundtrackID int32 `json:"soundtrack_id"`
Path string `json:"path"`
}
func (q *Queries) RemoveBrokenSong(ctx context.Context, arg RemoveBrokenSongParams) error {
_, err := q.db.Exec(ctx, removeBrokenSong, arg.SoundtrackID, arg.Path)
return err
}
const removeBrokenSongs = `-- name: RemoveBrokenSongs :exec
DELETE FROM song WHERE uuid = ANY($1::uuid[])
`
func (q *Queries) RemoveBrokenSongs(ctx context.Context, uuids []uuid.UUID) error {
_, err := q.db.Exec(ctx, removeBrokenSongs, uuids)
return err
}
const updateSong = `-- name: UpdateSong :exec
UPDATE song SET song_name=$1, file_name=$2, path=$3 where hash=$4
`
type UpdateSongParams struct {
SongName string `json:"song_name"`
FileName *string `json:"file_name"`
Path string `json:"path"`
Hash string `json:"hash"`
}
func (q *Queries) UpdateSong(ctx context.Context, arg UpdateSongParams) error {
_, err := q.db.Exec(ctx, updateSong,
arg.SongName,
arg.FileName,
arg.Path,
arg.Hash,
)
return err
}
const updateSongByUUID = `-- name: UpdateSongByUUID :exec
UPDATE song SET song_name=$1, file_name=$2, path=$3, hash=$4
WHERE uuid=$5
`
type UpdateSongByUUIDParams struct {
SongName string `json:"song_name"`
FileName *string `json:"file_name"`
Path string `json:"path"`
Hash string `json:"hash"`
Uuid uuid.UUID `json:"uuid"`
}
func (q *Queries) UpdateSongByUUID(ctx context.Context, arg UpdateSongByUUIDParams) error {
_, err := q.db.Exec(ctx, updateSongByUUID,
arg.SongName,
arg.FileName,
arg.Path,
arg.Hash,
arg.Uuid,
)
return err
}