chrly/http/textures.go

62 lines
1.5 KiB
Go
Raw Normal View History

package http
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
2019-04-27 01:46:15 +03:00
"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 01:46:15 +03:00
var textures *mojang.TexturesResponse
2019-04-28 00:43:22 +03:00
skin, skinErr := cfg.SkinsRepo.FindByUsername(username)
_, capeErr := cfg.CapesRepo.FindByUsername(username)
if (skinErr == nil && skin.SkinId != 0) || capeErr == nil {
textures = &mojang.TexturesResponse{}
2019-04-28 00:43:22 +03:00
if skinErr == nil && skin.SkinId != 0 {
skinTextures := &mojang.SkinTexturesResponse{
Url: skin.Url,
2019-04-27 01:46:15 +03:00
}
2019-04-28 00:43:22 +03:00
if skin.IsSlim {
skinTextures.Metadata = &mojang.SkinTexturesMetadata{
Model: "slim",
}
2019-04-27 01:46:15 +03:00
}
2019-04-28 00:43:22 +03:00
textures.Skin = skinTextures
}
if capeErr == nil {
2019-04-27 01:46:15 +03:00
textures.Cape = &mojang.CapeTexturesResponse{
2019-04-28 00:43:22 +03:00
Url: request.URL.Scheme + "://" + request.Host + "/cloaks/" + username,
2019-04-27 01:46:15 +03:00
}
}
2019-04-27 01:46:15 +03:00
} else {
mojangTextures := <-cfg.MojangTexturesQueue.GetTexturesForUsername(username)
if mojangTextures == nil {
response.WriteHeader(http.StatusNoContent)
return
}
2019-04-27 01:46:15 +03:00
texturesProp := mojangTextures.DecodeTextures()
if texturesProp == nil {
response.WriteHeader(http.StatusInternalServerError)
cfg.Logger.Error("Unable to find textures property")
return
}
2019-04-27 01:46:15 +03:00
textures = texturesProp.Textures
}
responseData, _ := json.Marshal(textures)
response.Header().Set("Content-Type", "application/json")
response.Write(responseData)
}