chrly/http/textures.go

65 lines
1.5 KiB
Go
Raw Normal View History

package http
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
2019-04-27 04:16:15 +05:30
"github.com/elyby/chrly/api/mojang"
)
func (cfg *Config) Textures(response http.ResponseWriter, request *http.Request) {
cfg.Logger.IncCounter("textures.request", 1)
username := parseUsername(mux.Vars(request)["username"])
2019-04-27 04:16:15 +05:30
var textures *mojang.TexturesResponse
skin, err := cfg.SkinsRepo.FindByUsername(username)
2019-04-27 04:16:15 +05:30
if err == nil && skin.SkinId != 0 {
textures = &mojang.TexturesResponse{
Skin: &mojang.SkinTexturesResponse{
Url: skin.Url,
},
}
2019-04-27 04:16:15 +05:30
if skin.IsSlim {
textures.Skin.Metadata = &mojang.SkinTexturesMetadata{
Model: "slim",
}
}
2019-04-27 04:16:15 +05:30
_, err = cfg.CapesRepo.FindByUsername(username)
if err == nil {
var scheme = "http://"
if request.TLS != nil {
scheme = "https://"
}
2019-04-27 04:16:15 +05:30
textures.Cape = &mojang.CapeTexturesResponse{
Url: scheme + request.Host + "/cloaks/" + username,
}
}
2019-04-27 04:16:15 +05:30
} else {
mojangTextures := <-cfg.MojangTexturesQueue.GetTexturesForUsername(username)
if mojangTextures == nil {
// TODO: test compatibility with exists authlibs
response.WriteHeader(http.StatusNoContent)
return
}
2019-04-27 04:16:15 +05:30
texturesProp := mojangTextures.DecodeTextures()
if texturesProp == nil {
// TODO: test compatibility with exists authlibs
response.WriteHeader(http.StatusInternalServerError)
cfg.Logger.Error("Unable to find textures property")
return
}
2019-04-27 04:16:15 +05:30
textures = texturesProp.Textures
}
responseData, _ := json.Marshal(textures)
response.Header().Set("Content-Type", "application/json")
response.Write(responseData)
}