Skip to content

Commit

Permalink
put helpers last
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Oct 1, 2023
1 parent e9accfb commit ae82153
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 234 deletions.
20 changes: 10 additions & 10 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import (
"github.com/stretchr/testify/require"
)

func randKey() string {
letters := []rune("abcdef0123456789")
b := make([]rune, 16)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
func TestMain(m *testing.M) {
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

func TestGetSetting(t *testing.T) {
Expand Down Expand Up @@ -46,7 +42,11 @@ func TestGetSetting(t *testing.T) {
require.Equal(t, value, actual)
}

func TestMain(m *testing.M) {
log.SetOutput(io.Discard)
os.Exit(m.Run())
func randKey() string {
letters := []rune("abcdef0123456789")
b := make([]rune, 16)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
51 changes: 24 additions & 27 deletions db/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//nolint:lll // struct tags get very long and can't be split
package db

// see this db fiddle to mess around with the schema
// https://www.db-fiddle.com/f/wJ7z8L7mu6ZKaYmWk1xr1p/5

import (
"fmt"
"path/filepath"
Expand All @@ -17,30 +14,6 @@ import (
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
)

func splitIDs(in, sep string) []specid.ID {
if in == "" {
return []specid.ID{}
}
parts := strings.Split(in, sep)
ret := make([]specid.ID, 0, len(parts))
for _, p := range parts {
id, _ := specid.New(p)
ret = append(ret, id)
}
return ret
}

func join[T fmt.Stringer](in []T, sep string) string {
if in == nil {
return ""
}
strs := make([]string, 0, len(in))
for _, id := range in {
strs = append(strs, id.String())
}
return strings.Join(strs, sep)
}

type Artist struct {
ID int `gorm:"primary_key"`
Name string `gorm:"not null; unique_index"`
Expand Down Expand Up @@ -461,3 +434,27 @@ func (p *ArtistInfo) SetSimilarArtists(items []string) { p.SimilarArtists = stri

func (p *ArtistInfo) GetTopTracks() []string { return strings.Split(p.TopTracks, ";") }
func (p *ArtistInfo) SetTopTracks(items []string) { p.TopTracks = strings.Join(items, ";") }

func splitIDs(in, sep string) []specid.ID {
if in == "" {
return []specid.ID{}
}
parts := strings.Split(in, sep)
ret := make([]specid.ID, 0, len(parts))
for _, p := range parts {
id, _ := specid.New(p)
ret = append(ret, id)
}
return ret
}

func join[T fmt.Stringer](in []T, sep string) string {
if in == nil {
return ""
}
strs := make([]string, 0, len(in))
for _, id := range in {
strs = append(strs, id.String())
}
return strings.Join(strs, sep)
}
44 changes: 22 additions & 22 deletions jukebox/jukebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ import (
"go.senan.xyz/gonic/jukebox"
)

func newJukebox(tb testing.TB) *jukebox.Jukebox {
tb.Helper()

sockPath := filepath.Join(tb.TempDir(), "mpv.sock")

j := jukebox.New()
err := j.Start(
sockPath,
[]string{jukebox.MPVArg("--ao", "null")},
)
if errors.Is(err, jukebox.ErrMPVTooOld) {
tb.Skip("old mpv found, skipping")
}
if err != nil {
tb.Fatalf("start jukebox: %v", err)
}
tb.Cleanup(func() {
j.Quit()
})
return j
}

func TestPlaySkipReset(t *testing.T) {
t.Skip("bit flakey currently")

Expand Down Expand Up @@ -187,6 +165,28 @@ func TestVolume(t *testing.T) {
require.Equal(t, 0.0, vol)
}

func newJukebox(tb testing.TB) *jukebox.Jukebox {
tb.Helper()

sockPath := filepath.Join(tb.TempDir(), "mpv.sock")

j := jukebox.New()
err := j.Start(
sockPath,
[]string{jukebox.MPVArg("--ao", "null")},
)
if errors.Is(err, jukebox.ErrMPVTooOld) {
tb.Skip("old mpv found, skipping")
}
if err != nil {
tb.Fatalf("start jukebox: %v", err)
}
tb.Cleanup(func() {
j.Quit()
})
return j
}

func testPath(path string) string {
cwd, _ := os.Getwd()
return filepath.Join(cwd, "testdata", path)
Expand Down
26 changes: 13 additions & 13 deletions mime/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ import (
"strings"
)

var supportedAudioTypes = map[string]string{
".mp3": "audio/mpeg",
".flac": "audio/x-flac",
".aac": "audio/x-aac",
".m4a": "audio/m4a",
".m4b": "audio/m4b",
".ogg": "audio/ogg",
".opus": "audio/ogg",
".wma": "audio/x-ms-wma",
".wav": "audio/x-wav",
".wv": "audio/x-wavpack",
}

//nolint:gochecknoinits
func init() {
for ext, mime := range supportedAudioTypes {
Expand All @@ -41,3 +28,16 @@ func TypeByAudioExtension(ext string) string {
}
return stdmime.TypeByExtension(ext)
}

var supportedAudioTypes = map[string]string{
".mp3": "audio/mpeg",
".flac": "audio/x-flac",
".aac": "audio/x-aac",
".m4a": "audio/m4a",
".m4b": "audio/m4b",
".ogg": "audio/ogg",
".opus": "audio/ogg",
".wma": "audio/x-ms-wma",
".wav": "audio/x-wav",
".wv": "audio/x-wavpack",
}
12 changes: 6 additions & 6 deletions server/ctrlsubsonic/ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (
"go.senan.xyz/gonic/transcode"
)

func TestMain(m *testing.M) {
gonic.Version = ""
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

var testCamelExpr = regexp.MustCompile("([a-z0-9])([A-Z])")

const (
Expand Down Expand Up @@ -156,9 +162,3 @@ func makec(tb testing.TB, roots []string, audio bool) *Controller {

return contr
}

func TestMain(m *testing.M) {
gonic.Version = ""
log.SetOutput(io.Discard)
os.Exit(m.Run())
}
132 changes: 66 additions & 66 deletions server/ctrlsubsonic/handlers_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,45 @@ import (
// b) return a non-nil spec.Response
// _but not both_

func streamGetTransodePreference(dbc *db.DB, userID int, client string) (*db.TranscodePreference, error) {
var pref db.TranscodePreference
err := dbc.
Where("user_id=?", userID).
Where("client COLLATE NOCASE IN (?)", []string{"*", client}).
Order("client DESC"). // ensure "*" is last if it's there
First(&pref).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
const (
coverDefaultSize = 600
coverCacheFormat = "png"
)

func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil {
return nil, fmt.Errorf("find transcode preference: %w", err)
return spec.NewError(10, "please provide an `id` parameter")
}
return &pref, nil
}
size := params.GetOrInt("size", coverDefaultSize)
cachePath := filepath.Join(
c.cacheCoverPath,
fmt.Sprintf("%s-%d.%s", id.String(), size, coverCacheFormat),
)
_, err = os.Stat(cachePath)
switch {
case os.IsNotExist(err):
reader, err := coverFor(c.dbc, c.artistInfoCache, id)
if err != nil {
return spec.NewError(10, "couldn't find cover `%s`: %v", id, err)
}
defer reader.Close()

func streamGetTranscodeMeta(dbc *db.DB, userID int, client string) spec.TranscodeMeta {
pref, _ := streamGetTransodePreference(dbc, userID, client)
if pref == nil {
return spec.TranscodeMeta{}
}
profile, ok := transcode.UserProfiles[pref.Profile]
if !ok {
return spec.TranscodeMeta{}
}
return spec.TranscodeMeta{
TranscodedContentType: profile.MIME(),
TranscodedSuffix: profile.Suffix(),
if err := coverScaleAndSave(reader, cachePath, size); err != nil {
log.Printf("error scaling cover: %v", err)
return nil
}
case err != nil:
log.Printf("error stating `%s`: %v", cachePath, err)
return nil
}
}

const (
coverDefaultSize = 600
coverCacheFormat = "png"
)
w.Header().Set("Cache-Control", "public, max-age=3600")
http.ServeFile(w, r, cachePath)

return nil
}

var (
errCoverNotFound = errors.New("could not find a cover with that id")
Expand Down Expand Up @@ -163,41 +166,6 @@ func coverScaleAndSave(reader io.Reader, cachePath string, size int) error {
return nil
}

func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
size := params.GetOrInt("size", coverDefaultSize)
cachePath := filepath.Join(
c.cacheCoverPath,
fmt.Sprintf("%s-%d.%s", id.String(), size, coverCacheFormat),
)
_, err = os.Stat(cachePath)
switch {
case os.IsNotExist(err):
reader, err := coverFor(c.dbc, c.artistInfoCache, id)
if err != nil {
return spec.NewError(10, "couldn't find cover `%s`: %v", id, err)
}
defer reader.Close()

if err := coverScaleAndSave(reader, cachePath, size); err != nil {
log.Printf("error scaling cover: %v", err)
return nil
}
case err != nil:
log.Printf("error stating `%s`: %v", cachePath, err)
return nil
}

w.Header().Set("Cache-Control", "public, max-age=3600")
http.ServeFile(w, r, cachePath)

return nil
}

func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
user := r.Context().Value(CtxUser).(*db.User)
Expand Down Expand Up @@ -268,3 +236,35 @@ func (c *Controller) ServeGetAvatar(w http.ResponseWriter, r *http.Request) *spe
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(reqUser.Avatar))
return nil
}

func streamGetTransodePreference(dbc *db.DB, userID int, client string) (*db.TranscodePreference, error) {
var pref db.TranscodePreference
err := dbc.
Where("user_id=?", userID).
Where("client COLLATE NOCASE IN (?)", []string{"*", client}).
Order("client DESC"). // ensure "*" is last if it's there
First(&pref).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("find transcode preference: %w", err)
}
return &pref, nil
}

func streamGetTranscodeMeta(dbc *db.DB, userID int, client string) spec.TranscodeMeta {
pref, _ := streamGetTransodePreference(dbc, userID, client)
if pref == nil {
return spec.TranscodeMeta{}
}
profile, ok := transcode.UserProfiles[pref.Profile]
if !ok {
return spec.TranscodeMeta{}
}
return spec.TranscodeMeta{
TranscodedContentType: profile.MIME(),
TranscodedSuffix: profile.Suffix(),
}
}
Loading

0 comments on commit ae82153

Please sign in to comment.