2022-10-11 13:23:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-10-24 19:51:39 +00:00
|
|
|
"strings"
|
2022-10-11 13:23:10 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2022-10-24 19:51:39 +00:00
|
|
|
func isValidExt(ext string) bool {
|
|
|
|
valid := []string{"jpg", "jpeg", "png", "gif"}
|
|
|
|
for _, c := range valid {
|
|
|
|
if strings.ToLower(ext) == c {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:23:10 +00:00
|
|
|
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
2022-10-24 19:51:39 +00:00
|
|
|
v := mux.Vars(r)
|
|
|
|
f := v["filename"]
|
|
|
|
ext := v["ext"]
|
|
|
|
|
|
|
|
if !isValidExt(ext) {
|
|
|
|
write(w, http.StatusBadRequest, []byte("not an image :/"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-24 20:32:38 +00:00
|
|
|
// first segment of URL resize the image to reduce bandwith usage.
|
|
|
|
url := fmt.Sprintf("https://t2.genius.com/unsafe/300x300/https://images.genius.com/%s.%s", f, ext)
|
2022-10-11 13:23:10 +00:00
|
|
|
|
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
2022-10-11 13:25:07 +00:00
|
|
|
write(w, http.StatusInternalServerError, []byte("can't reach genius servers"))
|
2022-10-11 13:23:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
write(w, res.StatusCode, []byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-24 19:51:39 +00:00
|
|
|
w.Header().Add("Content-type", fmt.Sprintf("image/%s", ext))
|
2022-10-11 13:23:10 +00:00
|
|
|
io.Copy(w, res.Body)
|
|
|
|
}
|