Skip to content

Commit

Permalink
website: actually add the api file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Apr 3, 2024
1 parent c35b9f9 commit 54a839b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions website/api/v1/song.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v1

import (
"net/http"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/util"
"github.com/rs/zerolog/hlog"
)

func (a *API) GetSong(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()

tid, err := radio.ParseTrackID(query.Get("id"))
if err != nil {
hlog.FromRequest(r).Error().Err(err)
http.Error(w, "invalid or missing id", http.StatusBadRequest)
return
}

key := query.Get("key")

song, err := a.storage.Track(r.Context()).Get(tid)
if err != nil {
hlog.FromRequest(r).Error().Err(err)
http.Error(w, "invalid id", http.StatusNotFound)
return
}

if !a.songSecret.Equal(key, song.Hash[:]) {
hlog.FromRequest(r).Error().Msg("wrong key for song download")
http.Error(w, "invalid key", http.StatusUnauthorized)
return
}

path := util.AbsolutePath(a.Config.Conf().MusicPath, song.FilePath)
http.ServeFile(w, r, path)
}

0 comments on commit 54a839b

Please sign in to comment.