2017-08-20 03:52:42 +05:30
|
|
|
package http
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func (cfg *Config) Textures(response http.ResponseWriter, request *http.Request) {
|
|
|
|
cfg.Logger.IncCounter("textures.request", 1)
|
|
|
|
username := parseUsername(mux.Vars(request)["username"])
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
var textures *mojang.TexturesResponse
|
2019-04-28 03:13:22 +05:30
|
|
|
skin, skinErr := cfg.SkinsRepo.FindByUsername(username)
|
|
|
|
_, capeErr := cfg.CapesRepo.FindByUsername(username)
|
|
|
|
if (skinErr == nil && skin.SkinId != 0) || capeErr == nil {
|
|
|
|
textures = &mojang.TexturesResponse{}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
if skinErr == nil && skin.SkinId != 0 {
|
|
|
|
skinTextures := &mojang.SkinTexturesResponse{
|
|
|
|
Url: skin.Url,
|
2019-04-27 04:16:15 +05:30
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
if skin.IsSlim {
|
|
|
|
skinTextures.Metadata = &mojang.SkinTexturesMetadata{
|
|
|
|
Model: "slim",
|
|
|
|
}
|
2019-04-27 04:16:15 +05:30
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
textures.Skin = skinTextures
|
|
|
|
}
|
|
|
|
|
|
|
|
if capeErr == nil {
|
2019-04-27 04:16:15 +05:30
|
|
|
textures.Cape = &mojang.CapeTexturesResponse{
|
2019-04-28 03:13:22 +05:30
|
|
|
Url: request.URL.Scheme + "://" + request.Host + "/cloaks/" + username,
|
2019-04-27 04:16:15 +05:30
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
2019-04-27 04:16:15 +05:30
|
|
|
} else {
|
|
|
|
mojangTextures := <-cfg.MojangTexturesQueue.GetTexturesForUsername(username)
|
|
|
|
if mojangTextures == nil {
|
|
|
|
response.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
texturesProp := mojangTextures.DecodeTextures()
|
|
|
|
if texturesProp == nil {
|
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
|
|
|
cfg.Logger.Error("Unable to find textures property")
|
|
|
|
return
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
2019-04-27 04:16:15 +05:30
|
|
|
|
|
|
|
textures = texturesProp.Textures
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
responseData, _ := json.Marshal(textures)
|
2017-06-30 21:10:25 +05:30
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
response.Write(responseData)
|
|
|
|
}
|