diff --git a/lyrics.go b/lyrics.go index d366d19..9ad7447 100644 --- a/lyrics.go +++ b/lyrics.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/http" + "net/url" "path" "strings" "text/template" @@ -33,7 +34,9 @@ func (s *song) parseMetadata(doc *goquery.Document) { title := doc.Find("h1[class*='Title']").First().Text() image, exists := doc.Find("meta[property='og:image']").Attr("content") if exists { - s.Image = image + if u, err := url.Parse(image); err == nil { + s.Image = fmt.Sprintf("/images%s", u.Path) + } } s.Title = title diff --git a/main.go b/main.go index 35f8155..6ea3392 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ func main() { r.Use(securityHeaders) r.HandleFunc("/{id}-lyrics", lyricsHandler) + r.HandleFunc("/images/{filename}.jpg", proxyHandler) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) server := &http.Server{ diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..28c0c23 --- /dev/null +++ b/proxy.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "io" + "net/http" + + "github.com/gorilla/mux" +) + +func proxyHandler(w http.ResponseWriter, r *http.Request) { + f := mux.Vars(r)["filename"] + url := fmt.Sprintf("https://images.genius.com/%s.jpg", f) + + res, err := http.Get(url) + if err != nil { + write(w, http.StatusInternalServerError, []byte("can't reach genius genius servers")) + return + } + + if res.StatusCode != http.StatusOK { + write(w, res.StatusCode, []byte{}) + return + } + + w.Header().Add("Content-type", "image/jpeg") + io.Copy(w, res.Body) +}