diff --git a/api.go b/api.go index b8e93c9..a367b60 100644 --- a/api.go +++ b/api.go @@ -36,7 +36,7 @@ func (a *API) Wait() { a.wg.Wait() } -func getQueueEntryFromBody(body []byte) *QueueTrackEntry { +func (a *API) getQueueEntryFromBody(body []byte) *QueueTrackEntry { entry := &QueueTrackEntry{} err := json.Unmarshal(body, &entry.original) if err != nil { @@ -50,7 +50,12 @@ func getQueueEntryFromBody(body []byte) *QueueTrackEntry { var val interface{} var strVal string var ok bool - if val, ok = entry.original["path"]; ok { + + if val, ok = entry.original["hash"]; ok && a.config.Queue.SongFetchUrl != "" { + if strVal, ok = val.(string); ok { + entry.Path = a.config.Queue.SongFetchUrl + strVal + } + } else if val, ok = entry.original["path"]; ok { if strVal, ok = val.(string); ok { entry.Path = strVal } @@ -83,7 +88,7 @@ func (a *API) getRandomTrack() *QueueTrackEntry { return nil } - return getQueueEntryFromBody(body) + return a.getQueueEntryFromBody(body) } func (a *API) setNowRandom(nr *QueueTrackEntry) { a.nr = nr @@ -210,7 +215,7 @@ func (a *API) listen() { if request.Method == "POST" { result := queueResultResponse{} if body, err := ioutil.ReadAll(request.Body); err == nil { - if e := getQueueEntryFromBody(body); e != nil { + if e := a.getQueueEntryFromBody(body); e != nil { if err = a.queue.AddTrack(e, false); err == nil { result.Success = true result.QueueId = e.QueueIdentifier @@ -240,7 +245,7 @@ func (a *API) listen() { if request.Method == "POST" { result := queueResultResponse{} if body, err := ioutil.ReadAll(request.Body); err == nil { - if e := getQueueEntryFromBody(body); e != nil { + if e := a.getQueueEntryFromBody(body); e != nil { if err = a.queue.AddTrack(e, true); err == nil { result.Success = true result.QueueId = e.QueueIdentifier diff --git a/config.go b/config.go index bb0ea80..4010205 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ type Config struct { } `toml:"api"` Queue struct { RandomSongApi string `toml:"random_song_api"` + SongFetchUrl string `toml:"song_fetch_url"` NowPlaying string `toml:"np"` NowRandom string `toml:"nr"` FallbackPath string `toml:"fallback"` diff --git a/example_config.toml b/example_config.toml index 1db5386..fb4862e 100644 --- a/example_config.toml +++ b/example_config.toml @@ -21,7 +21,11 @@ host="127.0.0.1" # It can also be an http(s) URL, that supports Range requests and returns proper Content-Length. # Additionally, the "title", "artist" and "art" properties can be included to be used as metadata. # If "title", "artist" are not specified, file tags may be used. +# +# If the key "hash" exists and song_fetch_url is set, +# hash will be appended and downloaded from that location. random_song_api="http://localhost:8012/api/random" +song_fetch_url="" # # An HTTP POST is issued to this URL when MeteorLight starts playing a track. The body # will be identical to the JSON blob in the queue. diff --git a/http.go b/http.go index 5a7b86a..0b55b6f 100644 --- a/http.go +++ b/http.go @@ -158,7 +158,9 @@ func (r *RangeReadSeekCloser) Seek(offset int64, whence int) (int64, error) { } if r.i < r.ib || r.i >= r.ib+int64(len(r.buf)) { - r.body.Close() + if r.body != nil { + r.body.Close() + } r.body = nil }