/
ctrl.go
128 lines (114 loc) · 2.99 KB
/
ctrl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Package ctrlsubsonic provides HTTP handlers for subsonic api
package ctrlsubsonic
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"go.senan.xyz/gonic/server/ctrlbase"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/jukebox"
"go.senan.xyz/gonic/server/podcasts"
"go.senan.xyz/gonic/server/scrobble"
)
type CtxKey int
const (
CtxUser CtxKey = iota
CtxSession
CtxParams
)
type Controller struct {
*ctrlbase.Controller
CachePath string
CoverCachePath string
PodcastsPath string
MusicPaths []string
Jukebox *jukebox.Jukebox
Scrobblers []scrobble.Scrobbler
Podcasts *podcasts.Podcasts
}
type metaResponse struct {
XMLName xml.Name `xml:"subsonic-response" json:"-"`
*spec.Response `json:"subsonic-response"`
}
type errWriter struct {
w io.Writer
err error
}
func (ew *errWriter) write(buf []byte) {
if ew.err != nil {
return
}
_, ew.err = ew.w.Write(buf)
}
func writeResp(w http.ResponseWriter, r *http.Request, resp *spec.Response) error {
if resp == nil {
return nil
}
if resp.Error != nil {
log.Printf("subsonic error code %d: %s", resp.Error.Code, resp.Error.Message)
}
res := metaResponse{Response: resp}
params := r.Context().Value(CtxParams).(params.Params)
ew := &errWriter{w: w}
switch v, _ := params.Get("f"); v {
case "json":
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(res)
if err != nil {
return fmt.Errorf("marshal to json: %w", err)
}
ew.write(data)
case "jsonp":
w.Header().Set("Content-Type", "application/javascript")
data, err := json.Marshal(res)
if err != nil {
return fmt.Errorf("marshal to jsonp: %w", err)
}
// TODO: error if no callback provided instead of using a default
pCall := params.GetOr("callback", "cb")
ew.write([]byte(pCall))
ew.write([]byte("("))
ew.write(data)
ew.write([]byte(");"))
default:
w.Header().Set("Content-Type", "application/xml")
data, err := xml.MarshalIndent(res, "", " ")
if err != nil {
return fmt.Errorf("marshal to xml: %w", err)
}
ew.write(data)
}
return ew.err
}
type (
handlerSubsonic func(r *http.Request) *spec.Response
handlerSubsonicRaw func(w http.ResponseWriter, r *http.Request) *spec.Response
)
func (c *Controller) H(h handlerSubsonic) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := writeResp(w, r, h(r)); err != nil {
log.Printf("error writing subsonic response: %v\n", err)
}
})
}
func (c *Controller) HR(h handlerSubsonicRaw) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := writeResp(w, r, h(w, r)); err != nil {
log.Printf("error writing raw subsonic response: %v\n", err)
}
})
}
func (c *Controller) getMusicFolder(p params.Params) string {
idx, err := p.GetInt("musicFolderId")
if err != nil {
return ""
}
if idx < 0 || idx > len(c.MusicPaths) {
return ""
}
return c.MusicPaths[idx]
}