chrly/lib/routes/Textures.go

63 lines
1.4 KiB
Go
Raw Normal View History

package routes
import (
"log"
"net/http"
"encoding/json"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/data"
"elyby/minecraft-skinsystem/lib/tools"
"elyby/minecraft-skinsystem/lib/services"
)
func Textures(w http.ResponseWriter, r *http.Request) {
2016-08-27 00:38:53 +05:30
services.Stats.Incr("textures-requests", 1)
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request textures for username " + username)
rec, err := data.FindSkinByUsername(username)
if (err != nil || rec.SkinId == 0) {
rec.Url = "http://skins.minecraft.net/MinecraftSkins/" + username + ".png"
rec.Hash = string(tools.BuildNonElyTexturesHash(username))
} else {
rec.Url = tools.BuildElyUrl(rec.Url)
}
textures := data.TexturesResponse{
Skin: &data.Skin{
Url: rec.Url,
Hash: rec.Hash,
},
}
if (rec.IsSlim) {
textures.Skin.Metadata = &data.SkinMetadata{
Model: "slim",
}
}
capeRec, err := data.FindCapeByUsername(username)
if (err == nil) {
capeUrl, err := services.Router.Get("cloaks").URL("username", username)
if (err != nil) {
log.Println(err.Error())
}
var scheme string = "http://";
if (r.TLS != nil) {
scheme = "https://"
}
textures.Cape = &data.Cape{
Url: scheme + r.Host + capeUrl.String(),
Hash: capeRec.CalculateHash(),
}
}
response,_ := json.Marshal(textures)
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}