add proxy to images
This commit is contained in:
parent
f5288a0e1d
commit
ce95d5de3e
|
@ -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
|
||||
|
|
1
main.go
1
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{
|
||||
|
|
28
proxy.go
Normal file
28
proxy.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user