fix proxy to handle other image extensions instead of only jpeg
This commit is contained in:
parent
8849d67cf1
commit
409f49a4c5
2
main.go
2
main.go
|
@ -24,7 +24,7 @@ func main() {
|
||||||
r.Use(securityHeaders)
|
r.Use(securityHeaders)
|
||||||
|
|
||||||
r.HandleFunc("/{id}-lyrics", lyricsHandler)
|
r.HandleFunc("/{id}-lyrics", lyricsHandler)
|
||||||
r.HandleFunc("/images/{filename}.jpg", proxyHandler)
|
r.HandleFunc("/images/{filename}.{ext}", proxyHandler)
|
||||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
|
|
26
proxy.go
26
proxy.go
|
@ -4,13 +4,33 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func isValidExt(ext string) bool {
|
||||||
|
valid := []string{"jpg", "jpeg", "png", "gif"}
|
||||||
|
for _, c := range valid {
|
||||||
|
if strings.ToLower(ext) == c {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
f := mux.Vars(r)["filename"]
|
v := mux.Vars(r)
|
||||||
url := fmt.Sprintf("https://images.genius.com/%s.jpg", f)
|
f := v["filename"]
|
||||||
|
ext := v["ext"]
|
||||||
|
|
||||||
|
if !isValidExt(ext) {
|
||||||
|
write(w, http.StatusBadRequest, []byte("not an image :/"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("https://images.genius.com/%s.%s", f, ext)
|
||||||
|
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -23,6 +43,6 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Add("Content-type", "image/jpeg")
|
w.Header().Add("Content-type", fmt.Sprintf("image/%s", ext))
|
||||||
io.Copy(w, res.Body)
|
io.Copy(w, res.Body)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user