-
Notifications
You must be signed in to change notification settings - Fork 46
/
subsonic.go
414 lines (338 loc) · 10.5 KB
/
subsonic.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"encoding/xml"
"fmt"
"github.com/disintegration/imaging"
"github.com/julienschmidt/httprouter"
"net/http"
"strconv"
"time"
)
// Credit: https://github.com/mdlayher/wavepipe/blob/master/subsonic/subsonic.go
const (
// XMLName is the top-level name of a Subsonic XML response
SubsonicXMLName = "subsonic-response"
// XMLNS is the XML namespace of a Subsonic XML response
SubsonicXMLNS = "http://subsonic.org/restapi"
// Version is the emulated Subsonic API version
SubsonicVersion = "1.8.0"
)
type SubsonicResponse struct {
// Top-level container name
XMLName xml.Name `xml:"subsonic-response"`
// Attributes which are always present
XMLNS string `xml:"xmlns,attr"`
Status string `xml:"status,attr"`
Version string `xml:"version,attr"`
// Error, returned on failures
SubError *SubsonicError
// Nested data
// getAlbum.view
Album []SubsonicAlbum `xml:"album"`
// getAlbumList2.view
AlbumList2 *SubsonicAlbumList2
// getIndexes.view
Indexes *SubsonicIndexes
// getLicense.view
License *SubsonicLicense `xml:"license"`
// getMusicDirectory.view
MusicDirectory *SubsonicMusicDirectory
// getMusicFolders.view
MusicFolders *SubsonicMusicFolders
// getPlaylists.view
Playlists *SubsonicPlaylists
// getPlaylist.view
Playlist *SubsonicPlaylist
// getLyrics.view
Lyrics *SubsonicLyrics
// getRandomSongs.view
RandomSongs *SubsonicRandomSongs
// getStarred.view
Starred *SubsonicStarred `xml:"starred"`
}
// SubsonicError contains a Subsonic error, with status code and message
type SubsonicError struct {
XMLName xml.Name `xml:"error,omitempty"`
Code int `xml:"code,attr"`
Message string `xml:"message,attr"`
}
// SubsonicArtist represents an emulated Subsonic artist
type SubsonicArtist struct {
XMLName xml.Name `xml:"artist,omitempty"`
// Subsonic fields
Name string `xml:"name,attr"`
ID string `xml:"id,attr"`
}
// SubsonicAlbum represents an emulated Subsonic album
type SubsonicAlbum struct {
// Subsonic fields
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Artist string `xml:"artist,attr"`
ArtistID int `xml:"artistId,attr"`
CoverArt string `xml:"coverArt,attr"`
SongCount int `xml:"songCount,attr"`
Duration int `xml:"duration,attr"`
Created string `xml:"created,attr"`
// Nested data
// getAlbum.view
Songs []SubsonicSong `xml:"song"`
}
// SubsonicRandomSongs contains a random list of emulated Subsonic songs
type SubsonicRandomSongs struct {
// Container name
XMLName xml.Name `xml:"randomSongs,omitempty"`
// Songs
Songs []SubsonicSong `xml:"song"`
}
// SubsonicLyrics represents a Subsonic lyrics
type SubsonicLyrics struct {
// Container name
XMLName xml.Name `xml:"lyrics,omitempty"`
Artist string `xml:"artist,attr,omitempty"`
Title string `xml:"title,attr,omitempty"`
}
// SubsonicLicense represents a Subsonic license
type SubsonicLicense struct {
XMLName xml.Name `xml:"license,omitempty"`
Valid bool `xml:"valid,attr"`
Email string `xml:"email,attr"`
Key string `xml:"key,attr"`
Date string `xml:"date,attr"`
}
// SubsonicSong represents an emulated Subsonic song
type SubsonicSong struct {
ID int `xml:"id,attr"`
Parent int `xml:"parent,attr"`
Title string `xml:"title,attr"`
Album string `xml:"album,attr"`
Artist string `xml:"artist,attr"`
IsDir bool `xml:"isDir,attr"`
CoverArt string `xml:"coverArt,attr"`
Created string `xml:"created,attr"`
Duration int `xml:"duration,attr"`
BitRate int `xml:"bitRate,attr"`
Track int `xml:"track,attr"`
DiscNumber int `xml:"discNumber,attr"`
Year int `xml:"year,attr"`
Genre string `xml:"genre,attr"`
Size int64 `xml:"size,attr"`
Suffix string `xml:"suffix,attr"`
ContentType string `xml:"contentType,attr"`
IsVideo bool `xml:"isVideo,attr"`
Path string `xml:"path,attr"`
AlbumID int `xml:"albumId,attr"`
ArtistID int `xml:"artistId,attr"`
Type string `xml:"type,attr"`
}
// SubsonicAlbumList2 contains a list of emulated Subsonic albums, by tags
type SubsonicAlbumList2 struct {
// Container name
XMLName xml.Name `xml:"albumList2,omitempty"`
// Albums
Albums []SubsonicAlbum `xml:"album"`
}
// SubsonicMusicFolder represents an emulated Subsonic music folder
type SubsonicMusicFolder struct {
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
}
// SubsonicMusicDirectory contains a list of emulated Subsonic music folders
type SubsonicMusicDirectory struct {
// Container name
XMLName xml.Name `xml:"directory,omitempty"`
// Attributes
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Children []SubsonicChild `xml:"child"`
}
// SubsonicIndexes represents a Subsonic indexes container
type SubsonicIndexes struct {
XMLName xml.Name `xml:"indexes,omitempty"`
LastModified int64 `xml:"lastModified,attr"`
Indexes []SubsonicIndex `xml:"index"`
}
// SubsonicIndex represents an alphabetical Subsonic index
type SubsonicIndex struct {
XMLName xml.Name `xml:"index"`
Name string `xml:"name,attr"`
Artists []SubsonicArtist `xml:"artist"`
}
// SubsonicChild is any item displayed to Subsonic when browsing using getMusicDirectory
type SubsonicChild struct {
// Container name
XMLName xml.Name `xml:"child,omitempty"`
// Attributes
ID string `xml:"id,attr"`
Title string `xml:"title,attr"`
Album string `xml:"album,attr"`
Artist string `xml:"artist,attr"`
IsDir bool `xml:"isDir,attr"`
CoverArt int `xml:"coverArt,attr"`
Created string `xml:"created,attr"`
}
// SubsonicPlaylists represents the Subsonic playlists container
type SubsonicPlaylists struct {
XMLName xml.Name `xml:"playlists,omitempty"`
Playlists []SubsonicPlaylist `xml:"playlist"`
}
type SubsonicPlaylist struct {
XMLName xml.Name `xml:"playlist,omitempty"`
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Comment string `xml:"comment,attr"`
Owner string `xml:"owner,attr"`
Public bool `xml:"public,attr"`
SongCount int `xml:"songCount,attr"`
Duration int `xml:"duration,attr"`
CoverArt string `xml:"coverArt,attr"`
Created time.Time `xml:"created,attr"`
Entry []SubsonicPlaylistEntry `xml:"entry"`
}
type SubsonicPlaylistEntry struct {
XMLName xml.Name `xml:"entry,omitempty"`
ID string `xml:"id,attr"`
Parent string `xml:"parent,attr"`
Title string `xml:"title,attr"`
Album string `xml:"album,attr"`
Artist string `xml:"artist,attr"`
IsDir string `xml:"isDir,attr"`
Duration int `xml:"duration,attr"`
CoverArt string `xml:"coverArt,attr"`
Created time.Time `xml:"created,attr"`
IsVideo bool `xml:"isVideo,attr"`
Path string `xml:"path,attr"`
BitRate int `xml:"bitRate,attr"`
Suffix string `xml:"suffix,attr"`
ContentType string `xml:"contentType,attr"`
Type string `xml:"type,attr"`
}
// SubsonicMusicFolders contains a list of emulated Subsonic music folders
type SubsonicMusicFolders struct {
// Container name
XMLName xml.Name `xml:"musicFolders,omitempty"`
// Music folders
MusicFolders []SubsonicMusicFolder `xml:"musicFolder"`
}
// SubsonicStarred represents a Subsonic license
type SubsonicStarred struct {
XMLName xml.Name `xml:"starred,omitempty"`
}
func NewSubsonicResponse() *SubsonicResponse {
return &SubsonicResponse{
XMLNS: SubsonicXMLNS,
Version: SubsonicVersion,
Status: "ok",
}
}
func subsonicPing(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
XML(w, response)
}
func subsonicGetMusicFolders(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
response.MusicFolders = &SubsonicMusicFolders{
MusicFolders: []SubsonicMusicFolder{
{
ID: 1,
Name: "Music",
},
},
}
XML(w, response)
}
func subsonicGetIndexes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
indexes := &SubsonicIndexes{
LastModified: time.Now().Unix(),
}
response.Indexes = indexes
XML(w, response)
}
func subsonicGetPlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
lists, err := ListLists()
if err != nil {
Error(w, err)
return
}
var playlists []SubsonicPlaylist
for _, list := range lists {
playlists = append(playlists, SubsonicPlaylist{
ID: list.ID,
Name: list.Title,
Comment: list.Title,
Owner: "admin",
Public: false,
SongCount: len(list.Medias),
Duration: int(list.TotalLength()),
})
}
response.Playlists = &SubsonicPlaylists{Playlists: playlists}
XML(w, response)
}
func subsonicGetPlaylist(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
list, err := FindList(r.FormValue("id"))
if err != nil {
Error(w, err)
return
}
var entries []SubsonicPlaylistEntry
for _, media := range list.Medias {
entries = append(entries, SubsonicPlaylistEntry{
ID: media.ID,
Title: media.Title,
Duration: int(media.Length),
CoverArt: media.ID,
Path: fmt.Sprintf("/soundscape/stream/%s/%s.mp3", list.ID, media.ID),
ContentType: "audio/mp3",
Suffix: "mp3",
//Path: fmt.Sprintf("/soundscape/stream/%s/%s.m4a", list.ID, media.ID),
//ContentType: "audio/mp4",
//Suffix: "m4a",
Type: "music",
})
}
response.Playlist = &SubsonicPlaylist{
ID: list.ID,
Name: list.Title,
Comment: list.Title,
Owner: "admin",
Public: false,
SongCount: len(list.Medias),
Duration: int(list.TotalLength()),
CoverArt: list.ID,
Entry: entries,
}
XML(w, response)
}
func subsonicGetCoverArt(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
media, err := FindMedia(r.FormValue("id"))
if err != nil {
Error(w, err)
return
}
size, err := strconv.Atoi(r.FormValue("size"))
if err != nil {
size = 640
}
img, err := imaging.Open(media.ImageFile())
if err != nil {
Error(w, err)
return
}
img = imaging.Resize(img, size, 0, imaging.Lanczos)
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", 7*86400))
if err := imaging.Encode(w, img, imaging.JPEG); err != nil {
Error(w, err)
return
}
}
func subsonicGetLyrics(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
response := NewSubsonicResponse()
response.Lyrics = &SubsonicLyrics{}
XML(w, response)
}