2017-08-20 03:52:42 +05:30
|
|
|
package http
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func (cfg *Config) Cape(response http.ResponseWriter, request *http.Request) {
|
2017-06-30 21:10:25 +05:30
|
|
|
if mux.Vars(request)["converted"] == "" {
|
2017-08-20 03:52:42 +05:30
|
|
|
cfg.Logger.IncCounter("capes.request", 1)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
username := parseUsername(mux.Vars(request)["username"])
|
|
|
|
rec, err := cfg.CapesRepo.FindByUsername(username)
|
2019-04-27 04:16:15 +05:30
|
|
|
if err == nil {
|
|
|
|
request.Header.Set("Content-Type", "image/png")
|
2019-04-28 23:00:55 +05:30
|
|
|
_, _ = io.Copy(response, rec.File)
|
2017-08-20 03:52:42 +05:30
|
|
|
return
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
mojangTextures := <-cfg.MojangTexturesQueue.GetTexturesForUsername(username)
|
|
|
|
if mojangTextures == nil {
|
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
texturesProp := mojangTextures.DecodeTextures()
|
|
|
|
cape := texturesProp.Textures.Cape
|
|
|
|
if cape == nil {
|
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(response, request, cape.Url, 301)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func (cfg *Config) CapeGET(response http.ResponseWriter, request *http.Request) {
|
|
|
|
cfg.Logger.IncCounter("capes.get_request", 1)
|
2017-06-30 21:10:25 +05:30
|
|
|
username := request.URL.Query().Get("name")
|
|
|
|
if username == "" {
|
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.Vars(request)["username"] = username
|
|
|
|
mux.Vars(request)["converted"] = "1"
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
cfg.Cape(response, request)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|