Add initial code
This commit is contained in:
26
src/config.go
Normal file
26
src/config.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
Redis struct {
|
||||
URI string `yaml:"uri"`
|
||||
Database int `yaml:"database"`
|
||||
} `yaml:"redis"`
|
||||
}
|
||||
|
||||
func (c *Configuration) ReadFile(file string) error {
|
||||
data, err := ioutil.ReadFile(file)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(data, c)
|
||||
}
|
||||
56
src/main.go
Normal file
56
src/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/redis"
|
||||
"main/src/routes"
|
||||
"time"
|
||||
|
||||
"github.com/buaazp/fasthttprouter"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
config *Configuration = &Configuration{}
|
||||
r *redis.Redis = &redis.Redis{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
if err = config.ReadFile("config.yml"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
if err = r.Connect(config.Redis.URI, config.Redis.Database); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("Successfully connected to Redis (%s)\n", time.Since(start))
|
||||
|
||||
routes.InitRoutes(r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer r.Close()
|
||||
|
||||
router := fasthttprouter.New()
|
||||
|
||||
router.GET("/ping", routes.PingHandler)
|
||||
router.GET("/uuid/:user", routes.UUIDHandler)
|
||||
router.GET("/skin/:user", routes.SkinHandler)
|
||||
router.GET("/face/:user", routes.FaceHandler)
|
||||
router.GET("/head/:user", routes.HeadHandler)
|
||||
router.GET("/body/full/:user", routes.FullBodyHandler)
|
||||
router.GET("/body/front/:user", routes.FrontBodyHandler)
|
||||
router.GET("/body/back/:user", routes.BackBodyHandler)
|
||||
router.GET("/body/left/:user", routes.LeftBodyHandler)
|
||||
router.GET("/body/right/:user", routes.RightBodyHandler)
|
||||
|
||||
log.Printf("Listening on %s:%d\n", config.Host, config.Port)
|
||||
|
||||
log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", config.Host, config.Port), router.Handler))
|
||||
}
|
||||
103
src/redis/redis.go
Normal file
103
src/redis/redis.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type Redis struct {
|
||||
conn *redis.Client
|
||||
}
|
||||
|
||||
func (r *Redis) Connect(uri string, database int) error {
|
||||
c := redis.NewClient(&redis.Options{
|
||||
Addr: uri,
|
||||
DB: database,
|
||||
})
|
||||
|
||||
r.conn = c
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
return c.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func (r *Redis) GetString(key string) (string, bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
existsResult := r.conn.Exists(ctx, key)
|
||||
|
||||
if err := existsResult.Err(); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if existsResult.Val() == 0 {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
result := r.conn.Get(ctx, key)
|
||||
|
||||
return result.Val(), true, result.Err()
|
||||
}
|
||||
|
||||
func (r *Redis) GetBytes(key string) ([]byte, bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
existsResult := r.conn.Exists(ctx, key)
|
||||
|
||||
if err := existsResult.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if existsResult.Val() == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
result := r.conn.Get(ctx, key)
|
||||
|
||||
if err := result.Err(); err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
data, err := result.Bytes()
|
||||
|
||||
return data, true, err
|
||||
}
|
||||
|
||||
func (r *Redis) Exists(key string) (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
result := r.conn.Exists(ctx, key)
|
||||
|
||||
return result.Val() == 1, result.Err()
|
||||
}
|
||||
|
||||
func (r *Redis) Delete(key string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
return r.conn.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (r *Redis) Set(key string, value interface{}, ttl time.Duration) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
defer cancel()
|
||||
|
||||
return r.conn.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (r *Redis) Close() error {
|
||||
return r.conn.Close()
|
||||
}
|
||||
538
src/routes/body.go
Normal file
538
src/routes/body.go
Normal file
@@ -0,0 +1,538 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/util"
|
||||
"main/src/util/renders"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func FullBodyHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScaleFullBody), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:fullbody-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderBody(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
|
||||
func FrontBodyHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:frontbody-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderFrontBody(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
|
||||
func BackBodyHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:backbody-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderBackBody(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
|
||||
func LeftBodyHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:leftbody-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderLeftBody(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
|
||||
func RightBodyHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:rightbody-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderRightBody(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
118
src/routes/face.go
Normal file
118
src/routes/face.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/util"
|
||||
"main/src/util/renders"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func FaceHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:face-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderFace(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
118
src/routes/head.go
Normal file
118
src/routes/head.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/util"
|
||||
"main/src/util/renders"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func HeadHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
scale, err := ctx.QueryArgs().GetUint("scale")
|
||||
|
||||
if err != nil {
|
||||
scale = 4
|
||||
}
|
||||
|
||||
scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale))
|
||||
|
||||
overlay := true
|
||||
|
||||
if ctx.QueryArgs().Has("overlay") {
|
||||
overlay = ctx.QueryArgs().GetBool("overlay")
|
||||
}
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("result:head-%d-%t-%s", scale, overlay, uuid)
|
||||
|
||||
cache, ok, err := r.GetBytes(cacheKey)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if ok {
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "TRUE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(cache)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
render := renders.RenderHead(skin, renders.RenderOptions{
|
||||
Overlay: overlay,
|
||||
Slim: slim,
|
||||
Scale: scale,
|
||||
})
|
||||
|
||||
data, err := util.EncodePNG(render)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, time.Hour*24); err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.Response.Header.Set("X-Cache-Hit", "FALSE")
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
17
src/routes/init.go
Normal file
17
src/routes/init.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package routes
|
||||
|
||||
import "main/src/redis"
|
||||
|
||||
var (
|
||||
r *redis.Redis
|
||||
)
|
||||
|
||||
const (
|
||||
MaxScale float64 = 64.0
|
||||
MaxScaleFullBody float64 = 32.0
|
||||
MinScale float64 = 1.0
|
||||
)
|
||||
|
||||
func InitRoutes(red *redis.Redis) {
|
||||
r = red
|
||||
}
|
||||
9
src/routes/ping.go
Normal file
9
src/routes/ping.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func PingHandler(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetBodyString("Pong!")
|
||||
}
|
||||
60
src/routes/skin.go
Normal file
60
src/routes/skin.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func SkinHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
download := ctx.QueryArgs().GetBool("download")
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
skin, slim, err := util.GetPlayerSkin(r, uuid)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if skin == nil {
|
||||
skin = util.GetDefaultSkin(slim)
|
||||
}
|
||||
|
||||
data, err := util.EncodePNG(skin)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if download {
|
||||
ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
|
||||
}
|
||||
|
||||
ctx.SetContentType("image/png")
|
||||
ctx.SetBody(data)
|
||||
}
|
||||
26
src/routes/uuid.go
Normal file
26
src/routes/uuid.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"log"
|
||||
"main/src/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func UUIDHandler(ctx *fasthttp.RequestCtx) {
|
||||
user := ctx.UserValue("user").(string)
|
||||
|
||||
uuid, err := util.GetUUID(r, user)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||
ctx.SetBodyString(http.StatusText(http.StatusInternalServerError))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetBodyString(uuid)
|
||||
}
|
||||
BIN
src/util/alex.png
Normal file
BIN
src/util/alex.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
17
src/util/image.go
Normal file
17
src/util/image.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
func EncodePNG(img image.Image) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
if err := png.Encode(buf, img); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
5
src/util/init.go
Normal file
5
src/util/init.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package util
|
||||
|
||||
import "os"
|
||||
|
||||
var Debug = os.Getenv("DEBUG") == "true"
|
||||
59
src/util/renders/backbody.go
Normal file
59
src/util/renders/backbody.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package renders
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
func RenderBackBody(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
slimOffset := GetSlimOffset(opts.Slim)
|
||||
|
||||
var (
|
||||
backHead *image.NRGBA = RemoveTransparency(Extract(skin, 24, 8, 8, 8))
|
||||
backTorso *image.NRGBA = RemoveTransparency(Extract(skin, 32, 20, 8, 12))
|
||||
backLeftArm *image.NRGBA = nil
|
||||
backRightArm *image.NRGBA = RemoveTransparency(Extract(skin, 52-slimOffset, 20, 4-slimOffset, 12))
|
||||
backLeftLeg *image.NRGBA = nil
|
||||
backRightLeg *image.NRGBA = RemoveTransparency(Extract(skin, 12, 20, 4, 12))
|
||||
)
|
||||
|
||||
if IsOldSkin(skin) {
|
||||
backLeftArm = FlipHorizontal(backRightArm)
|
||||
backLeftLeg = FlipHorizontal(backRightLeg)
|
||||
} else {
|
||||
backLeftArm = RemoveTransparency(Extract(skin, 44-slimOffset, 52, 4-slimOffset, 12))
|
||||
backLeftLeg = RemoveTransparency(Extract(skin, 28, 52, 4, 12))
|
||||
|
||||
if opts.Overlay {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
backHead = Composite(backHead, Extract(overlaySkin, 56, 8, 8, 8), 0, 0)
|
||||
backTorso = Composite(backTorso, Extract(overlaySkin, 32, 36, 8, 12), 0, 0)
|
||||
backLeftArm = Composite(backLeftArm, Extract(overlaySkin, 60-slimOffset, 52, 4-slimOffset, 64), 0, 0)
|
||||
backRightArm = Composite(backRightArm, Extract(overlaySkin, 52-slimOffset, 36, 4-slimOffset, 48), 0, 0)
|
||||
backLeftLeg = Composite(backLeftLeg, Extract(overlaySkin, 12, 52, 8, 64), 0, 0)
|
||||
backRightLeg = Composite(backRightLeg, Extract(overlaySkin, 12, 36, 8, 48), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 16, 32))
|
||||
|
||||
// Face
|
||||
output = Composite(output, backHead, 4, 0)
|
||||
|
||||
// Torso
|
||||
output = Composite(output, backTorso, 4, 8)
|
||||
|
||||
// Left Arm
|
||||
output = Composite(output, backLeftArm, slimOffset, 8)
|
||||
|
||||
// Right Arm
|
||||
output = Composite(output, backRightArm, 12, 8)
|
||||
|
||||
// Left Leg
|
||||
output = Composite(output, backLeftLeg, 4, 20)
|
||||
|
||||
// Right Leg
|
||||
output = Composite(output, backRightLeg, 8, 20)
|
||||
|
||||
return Scale(output, opts.Scale)
|
||||
}
|
||||
90
src/util/renders/body.go
Normal file
90
src/util/renders/body.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package renders
|
||||
|
||||
import "image"
|
||||
|
||||
func RenderBody(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
scale := float64(opts.Scale)
|
||||
slimOffset := GetSlimOffset(opts.Slim)
|
||||
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 20*opts.Scale, 45*opts.Scale+int(scale*(1.0/16.0))))
|
||||
|
||||
var (
|
||||
frontHead *image.NRGBA = RemoveTransparency(Extract(skin, 8, 8, 8, 8))
|
||||
topHead *image.NRGBA = RemoveTransparency(Extract(skin, 8, 0, 8, 8))
|
||||
rightHead *image.NRGBA = RemoveTransparency(Extract(skin, 0, 8, 8, 8))
|
||||
frontTorso *image.NRGBA = RemoveTransparency(Extract(skin, 20, 20, 8, 12))
|
||||
frontLeftArm *image.NRGBA = nil
|
||||
topLeftArm *image.NRGBA = nil
|
||||
frontRightArm *image.NRGBA = RemoveTransparency(Extract(skin, 44, 20, 4-slimOffset, 12))
|
||||
topRightArm *image.NRGBA = RemoveTransparency(Extract(skin, 44, 16, 4-slimOffset, 4))
|
||||
rightRightArm *image.NRGBA = RemoveTransparency(Extract(skin, 40, 20, 4, 12))
|
||||
frontLeftLeg *image.NRGBA = nil
|
||||
frontRightLeg *image.NRGBA = RemoveTransparency(Extract(skin, 4, 20, 4, 12))
|
||||
rightRightLeg *image.NRGBA = RemoveTransparency(Extract(skin, 0, 20, 4, 12))
|
||||
)
|
||||
|
||||
if IsOldSkin(skin) {
|
||||
frontLeftArm = FlipHorizontal(frontRightArm)
|
||||
topLeftArm = FlipHorizontal(topRightArm)
|
||||
frontLeftLeg = FlipHorizontal(frontRightLeg)
|
||||
} else {
|
||||
frontLeftArm = RemoveTransparency(Extract(skin, 36, 52, 4-slimOffset, 12))
|
||||
topLeftArm = RemoveTransparency(Extract(skin, 36, 48, 4-slimOffset, 4))
|
||||
frontLeftLeg = RemoveTransparency(Extract(skin, 20, 52, 4, 12))
|
||||
|
||||
if opts.Overlay {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
frontHead = Composite(frontHead, Extract(overlaySkin, 40, 8, 8, 8), 0, 0)
|
||||
topHead = Composite(topHead, Extract(overlaySkin, 40, 0, 8, 8), 0, 0)
|
||||
rightHead = Composite(rightHead, Extract(overlaySkin, 32, 8, 8, 8), 0, 0)
|
||||
frontTorso = Composite(frontTorso, Extract(overlaySkin, 20, 36, 8, 12), 0, 0)
|
||||
frontLeftArm = Composite(frontLeftArm, Extract(overlaySkin, 52, 52, 4-slimOffset, 64), 0, 0)
|
||||
topLeftArm = Composite(topLeftArm, Extract(overlaySkin, 52, 48, 4-slimOffset, 4), 0, 0)
|
||||
frontRightArm = Composite(frontRightArm, Extract(overlaySkin, 44, 36, 4-slimOffset, 48), 0, 0)
|
||||
topRightArm = Composite(topRightArm, Extract(overlaySkin, 44, 48, 4-slimOffset, 4), 0, 0)
|
||||
rightRightArm = Composite(rightRightArm, Extract(overlaySkin, 40, 36, 4, 12), 0, 0)
|
||||
frontLeftLeg = Composite(frontLeftLeg, Extract(overlaySkin, 4, 52, 4, 12), 0, 0)
|
||||
frontRightLeg = Composite(frontRightLeg, Extract(overlaySkin, 4, 36, 4, 12), 0, 0)
|
||||
rightRightLeg = Composite(rightRightLeg, Extract(overlaySkin, 0, 36, 4, 12), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// Right Side of Right Leg
|
||||
output = CompositeTransform(output, Scale(rightRightLeg, opts.Scale), TransformRight, 4*scale, 23*scale)
|
||||
|
||||
// Front of Right Leg
|
||||
output = CompositeTransform(output, Scale(frontRightLeg, opts.Scale), TransformForward, 8*scale, 31*scale)
|
||||
|
||||
// Front of Left Leg
|
||||
output = CompositeTransform(output, Scale(frontLeftLeg, opts.Scale), TransformForward, 12*scale, 31*scale)
|
||||
|
||||
// Front of Torso
|
||||
output = CompositeTransform(output, Scale(frontTorso, opts.Scale), TransformForward, 8*scale, 19*scale)
|
||||
|
||||
// Front of Right Arm
|
||||
output = CompositeTransform(output, Scale(frontRightArm, opts.Scale), TransformForward, float64(4+slimOffset)*scale, 19*scale-1)
|
||||
|
||||
// Front of Left Arm
|
||||
output = CompositeTransform(output, Scale(frontLeftArm, opts.Scale), TransformForward, 16*scale, 21*scale-1)
|
||||
|
||||
// Top of Left Arm
|
||||
output = CompositeTransform(output, Scale(topLeftArm, opts.Scale), TransformUp, -5*scale, 17*scale)
|
||||
|
||||
// Right Side of Right Arm
|
||||
output = CompositeTransform(output, Scale(rightRightArm, opts.Scale), TransformRight, float64(slimOffset)*scale, float64(15-slimOffset)*scale)
|
||||
|
||||
// Top of Right Arm
|
||||
output = CompositeTransform(output, Scale(topRightArm, opts.Scale), TransformUp, float64(-15+slimOffset)*scale, 15*scale)
|
||||
|
||||
// Front of Head
|
||||
output = CompositeTransform(output, Scale(frontHead, opts.Scale), TransformForward, 10*scale, 13*scale-1)
|
||||
|
||||
// Top of Head
|
||||
output = CompositeTransform(output, Scale(topHead, opts.Scale), TransformUp, -3*scale, 5*scale)
|
||||
|
||||
// Right Side of Head
|
||||
output = CompositeTransform(output, Scale(rightHead, opts.Scale), TransformRight, 2*scale, 3*scale)
|
||||
|
||||
return output
|
||||
}
|
||||
13
src/util/renders/face.go
Normal file
13
src/util/renders/face.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package renders
|
||||
|
||||
import "image"
|
||||
|
||||
func RenderFace(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
output := RemoveTransparency(Extract(skin, 8, 8, 8, 8))
|
||||
|
||||
if opts.Overlay && !IsOldSkin(skin) {
|
||||
output = Composite(output, Extract(skin, 40, 8, 8, 8), 0, 0)
|
||||
}
|
||||
|
||||
return Scale(output, opts.Scale)
|
||||
}
|
||||
59
src/util/renders/frontbody.go
Normal file
59
src/util/renders/frontbody.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package renders
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
func RenderFrontBody(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
slimOffset := GetSlimOffset(opts.Slim)
|
||||
|
||||
var (
|
||||
frontHead *image.NRGBA = RemoveTransparency(Extract(skin, 8, 8, 8, 8))
|
||||
frontTorso *image.NRGBA = RemoveTransparency(Extract(skin, 20, 20, 8, 12))
|
||||
leftArm *image.NRGBA = nil
|
||||
rightArm *image.NRGBA = RemoveTransparency(Extract(skin, 44, 20, 4-slimOffset, 12))
|
||||
leftLeg *image.NRGBA = nil
|
||||
rightLeg *image.NRGBA = RemoveTransparency(Extract(skin, 4, 20, 4, 12))
|
||||
)
|
||||
|
||||
if IsOldSkin(skin) {
|
||||
leftArm = FlipHorizontal(rightArm)
|
||||
leftLeg = FlipHorizontal(rightLeg)
|
||||
} else {
|
||||
leftArm = RemoveTransparency(Extract(skin, 36, 52, 4-slimOffset, 12))
|
||||
leftLeg = RemoveTransparency(Extract(skin, 20, 52, 4, 12))
|
||||
|
||||
if opts.Overlay {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
frontHead = Composite(frontHead, Extract(overlaySkin, 40, 8, 8, 8), 0, 0)
|
||||
frontTorso = Composite(frontTorso, Extract(overlaySkin, 20, 36, 8, 12), 0, 0)
|
||||
leftArm = Composite(leftArm, Extract(overlaySkin, 52, 52, 4-slimOffset, 64), 0, 0)
|
||||
rightArm = Composite(rightArm, Extract(overlaySkin, 44, 36, 4-slimOffset, 48), 0, 0)
|
||||
leftLeg = Composite(leftLeg, Extract(overlaySkin, 4, 52, 4, 12), 0, 0)
|
||||
rightLeg = Composite(rightLeg, Extract(overlaySkin, 4, 36, 4, 12), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 16, 32))
|
||||
|
||||
// Face
|
||||
output = Composite(output, frontHead, 4, 0)
|
||||
|
||||
// Torso
|
||||
output = Composite(output, frontTorso, 4, 8)
|
||||
|
||||
// Left Arm
|
||||
output = Composite(output, leftArm, 12, 8)
|
||||
|
||||
// Right Arm
|
||||
output = Composite(output, rightArm, slimOffset, 8)
|
||||
|
||||
// Left Leg
|
||||
output = Composite(output, leftLeg, 8, 20)
|
||||
|
||||
// Right Leg
|
||||
output = Composite(output, rightLeg, 4, 20)
|
||||
|
||||
return Scale(output, opts.Scale)
|
||||
}
|
||||
33
src/util/renders/head.go
Normal file
33
src/util/renders/head.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package renders
|
||||
|
||||
import "image"
|
||||
|
||||
func RenderHead(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
scale := float64(opts.Scale)
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 16*opts.Scale, 19*opts.Scale-int(scale/2.0)-1))
|
||||
|
||||
var (
|
||||
frontHead *image.NRGBA = RemoveTransparency(Extract(skin, 8, 8, 8, 8))
|
||||
topHead *image.NRGBA = RemoveTransparency(Extract(skin, 8, 0, 8, 8))
|
||||
rightHead *image.NRGBA = RemoveTransparency(Extract(skin, 0, 8, 8, 8))
|
||||
)
|
||||
|
||||
if opts.Overlay && !IsOldSkin(skin) {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
frontHead = Composite(frontHead, Extract(overlaySkin, 40, 8, 8, 8), 0, 0)
|
||||
topHead = Composite(topHead, Extract(overlaySkin, 40, 0, 8, 8), 0, 0)
|
||||
rightHead = Composite(rightHead, Extract(overlaySkin, 32, 8, 8, 8), 0, 0)
|
||||
}
|
||||
|
||||
// Front Head
|
||||
output = CompositeTransform(output, Scale(frontHead, opts.Scale), TransformForward, 8*scale, 12*scale-1)
|
||||
|
||||
// Top Head
|
||||
output = CompositeTransform(output, Scale(topHead, opts.Scale), TransformUp, -4*scale, 4*scale)
|
||||
|
||||
// Right Head
|
||||
output = CompositeTransform(output, Scale(rightHead, opts.Scale), TransformRight, 0, 4*scale)
|
||||
|
||||
return output
|
||||
}
|
||||
44
src/util/renders/leftbody.go
Normal file
44
src/util/renders/leftbody.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package renders
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
func RenderLeftBody(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
slimOffset := GetSlimOffset(opts.Slim)
|
||||
|
||||
var (
|
||||
leftHead *image.NRGBA = RemoveTransparency(Extract(skin, 24, 8, 8, 8))
|
||||
leftLeftArm *image.NRGBA = nil
|
||||
leftLeftLeg *image.NRGBA = nil
|
||||
)
|
||||
|
||||
if IsOldSkin(skin) {
|
||||
leftLeftArm = FlipHorizontal(RemoveTransparency(Extract(skin, 40, 20, 4, 12)))
|
||||
leftLeftLeg = FlipHorizontal(RemoveTransparency(Extract(skin, 0, 20, 4, 12)))
|
||||
} else {
|
||||
leftLeftArm = RemoveTransparency(Extract(skin, 40-slimOffset, 52, 4, 12))
|
||||
leftLeftLeg = RemoveTransparency(Extract(skin, 24, 52, 4, 12))
|
||||
|
||||
if opts.Overlay {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
leftHead = Composite(leftHead, Extract(overlaySkin, 48, 8, 8, 8), 0, 0)
|
||||
leftLeftArm = Composite(leftLeftArm, Extract(overlaySkin, 56-slimOffset, 52, 4, 12), 0, 0)
|
||||
leftLeftLeg = Composite(leftLeftLeg, Extract(overlaySkin, 8, 52, 4, 12), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 8, 32))
|
||||
|
||||
// Left Head
|
||||
output = Composite(output, leftHead, 0, 0)
|
||||
|
||||
// Left Arm
|
||||
output = Composite(output, leftLeftArm, 2, 8)
|
||||
|
||||
// Left Leg
|
||||
output = Composite(output, leftLeftLeg, 2, 20)
|
||||
|
||||
return Scale(output, opts.Scale)
|
||||
}
|
||||
90
src/util/renders/matrix/transform.go
Normal file
90
src/util/renders/matrix/transform.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package matrix
|
||||
|
||||
import "math"
|
||||
|
||||
// Credit: https://github.com/fogleman/gg
|
||||
|
||||
type Matrix struct {
|
||||
XX, YX, XY, YY, X0, Y0 float64
|
||||
}
|
||||
|
||||
func Identity() Matrix {
|
||||
return Matrix{
|
||||
1, 0,
|
||||
0, 1,
|
||||
0, 0,
|
||||
}
|
||||
}
|
||||
|
||||
func Translate(x, y float64) Matrix {
|
||||
return Matrix{
|
||||
1, 0,
|
||||
0, 1,
|
||||
x, y,
|
||||
}
|
||||
}
|
||||
|
||||
func Scale(x, y float64) Matrix {
|
||||
return Matrix{
|
||||
x, 0,
|
||||
0, y,
|
||||
0, 0,
|
||||
}
|
||||
}
|
||||
|
||||
func Rotate(angle float64) Matrix {
|
||||
c := math.Cos(angle)
|
||||
s := math.Sin(angle)
|
||||
return Matrix{
|
||||
c, s,
|
||||
-s, c,
|
||||
0, 0,
|
||||
}
|
||||
}
|
||||
|
||||
func Shear(x, y float64) Matrix {
|
||||
return Matrix{
|
||||
1, y,
|
||||
x, 1,
|
||||
0, 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (a Matrix) Multiply(b Matrix) Matrix {
|
||||
return Matrix{
|
||||
a.XX*b.XX + a.YX*b.XY,
|
||||
a.XX*b.YX + a.YX*b.YY,
|
||||
a.XY*b.XX + a.YY*b.XY,
|
||||
a.XY*b.YX + a.YY*b.YY,
|
||||
a.X0*b.XX + a.Y0*b.XY + b.X0,
|
||||
a.X0*b.YX + a.Y0*b.YY + b.Y0,
|
||||
}
|
||||
}
|
||||
|
||||
func (a Matrix) TransformVector(x, y float64) (tx, ty float64) {
|
||||
tx = a.XX*x + a.XY*y
|
||||
ty = a.YX*x + a.YY*y
|
||||
return
|
||||
}
|
||||
|
||||
func (a Matrix) TransformPoint(x, y float64) (tx, ty float64) {
|
||||
tx = a.XX*x + a.XY*y + a.X0
|
||||
ty = a.YX*x + a.YY*y + a.Y0
|
||||
return
|
||||
}
|
||||
|
||||
func (a Matrix) Translate(x, y float64) Matrix {
|
||||
return Translate(x, y).Multiply(a)
|
||||
}
|
||||
|
||||
func (a Matrix) Scale(x, y float64) Matrix {
|
||||
return Scale(x, y).Multiply(a)
|
||||
}
|
||||
|
||||
func (a Matrix) Rotate(angle float64) Matrix {
|
||||
return Rotate(angle).Multiply(a)
|
||||
}
|
||||
|
||||
func (a Matrix) Shear(x, y float64) Matrix {
|
||||
return Shear(x, y).Multiply(a)
|
||||
}
|
||||
7
src/util/renders/options.go
Normal file
7
src/util/renders/options.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package renders
|
||||
|
||||
type RenderOptions struct {
|
||||
Scale int
|
||||
Overlay bool
|
||||
Slim bool
|
||||
}
|
||||
34
src/util/renders/rightbody.go
Normal file
34
src/util/renders/rightbody.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package renders
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
func RenderRightBody(skin *image.NRGBA, opts RenderOptions) *image.NRGBA {
|
||||
var (
|
||||
rightHead *image.NRGBA = RemoveTransparency(Extract(skin, 0, 8, 8, 8))
|
||||
rightRightArm *image.NRGBA = RemoveTransparency(Extract(skin, 40, 20, 4, 12))
|
||||
rightRightLeg *image.NRGBA = RemoveTransparency(Extract(skin, 0, 20, 4, 12))
|
||||
)
|
||||
|
||||
if opts.Overlay && !IsOldSkin(skin) {
|
||||
overlaySkin := FixTransparency(skin)
|
||||
|
||||
rightHead = Composite(rightHead, Extract(overlaySkin, 32, 8, 8, 8), 0, 0)
|
||||
rightRightArm = Composite(rightRightArm, Extract(overlaySkin, 40, 36, 4, 12), 0, 0)
|
||||
rightRightLeg = Composite(rightRightLeg, Extract(overlaySkin, 0, 36, 4, 12), 0, 0)
|
||||
}
|
||||
|
||||
output := image.NewNRGBA(image.Rect(0, 0, 8, 32))
|
||||
|
||||
// Right Head
|
||||
output = Composite(output, rightHead, 0, 0)
|
||||
|
||||
// Right Arm
|
||||
output = Composite(output, rightRightArm, 2, 8)
|
||||
|
||||
// Right Leg
|
||||
output = Composite(output, rightRightLeg, 2, 20)
|
||||
|
||||
return Scale(output, opts.Scale)
|
||||
}
|
||||
164
src/util/renders/util.go
Normal file
164
src/util/renders/util.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package renders
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
"main/src/util/renders/matrix"
|
||||
|
||||
drw "golang.org/x/image/draw"
|
||||
|
||||
"golang.org/x/image/math/f64"
|
||||
)
|
||||
|
||||
var (
|
||||
SkewA float64 = 26.0 / 45.0
|
||||
SkewB float64 = SkewA * 2.0
|
||||
TransformForward matrix.Matrix = matrix.Matrix{
|
||||
XX: 1, YX: -SkewA,
|
||||
XY: 0, YY: SkewB,
|
||||
X0: 0, Y0: SkewA,
|
||||
}
|
||||
TransformUp matrix.Matrix = matrix.Matrix{
|
||||
XX: 1, YX: -SkewA,
|
||||
XY: 1, YY: SkewA,
|
||||
X0: 0, Y0: 0,
|
||||
}
|
||||
TransformRight matrix.Matrix = matrix.Matrix{
|
||||
XX: 1, YX: SkewA,
|
||||
XY: 0, YY: SkewB,
|
||||
X0: 0, Y0: 0,
|
||||
}
|
||||
)
|
||||
|
||||
func Extract(img *image.NRGBA, x, y, width, height int) *image.NRGBA {
|
||||
output := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
|
||||
draw.Draw(output, output.Bounds(), img, image.Pt(x, y), draw.Src)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func Scale(img *image.NRGBA, scale int) *image.NRGBA {
|
||||
if scale == 1 {
|
||||
return img
|
||||
}
|
||||
|
||||
bounds := img.Bounds().Max
|
||||
output := image.NewNRGBA(image.Rect(0, 0, bounds.X*scale, bounds.Y*scale))
|
||||
|
||||
for x := 0; x < bounds.X; x++ {
|
||||
for y := 0; y < bounds.Y; y++ {
|
||||
color := img.At(x, y)
|
||||
|
||||
for sx := 0; sx < scale; sx++ {
|
||||
for sy := 0; sy < scale; sy++ {
|
||||
output.Set(x*scale+sx, y*scale+sy, color)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func RemoveTransparency(img *image.NRGBA) *image.NRGBA {
|
||||
output := image.NewNRGBA(img.Bounds())
|
||||
|
||||
for i, l := 0, len(img.Pix); i < l; i += 4 {
|
||||
output.Pix[i] = img.Pix[i]
|
||||
output.Pix[i+1] = img.Pix[i+1]
|
||||
output.Pix[i+2] = img.Pix[i+2]
|
||||
output.Pix[i+3] = 255
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func IsOldSkin(img *image.NRGBA) bool {
|
||||
return img.Bounds().Max.Y < 64
|
||||
}
|
||||
|
||||
func Composite(bottom, top *image.NRGBA, x, y int) *image.NRGBA {
|
||||
output := image.NewNRGBA(bottom.Bounds())
|
||||
|
||||
topBounds := top.Bounds().Max
|
||||
|
||||
draw.Draw(output, bottom.Bounds(), bottom, image.Pt(0, 0), draw.Src)
|
||||
draw.Draw(output, image.Rect(0, 0, topBounds.X+x, topBounds.Y+y), top, image.Pt(-x, -y), draw.Over)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func FlipHorizontal(img *image.NRGBA) *image.NRGBA {
|
||||
data := img.Pix
|
||||
bounds := img.Bounds()
|
||||
|
||||
output := image.NewNRGBA(bounds)
|
||||
|
||||
for x := 0; x < bounds.Max.X; x++ {
|
||||
for y := 0; y < bounds.Max.Y; y++ {
|
||||
fx := bounds.Max.X - x - 1
|
||||
fi := fx*4 + y*4*bounds.Max.X
|
||||
ix := x*4 + y*4*bounds.Max.X
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
output.Pix[ix+i] = data[fi+i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func FixTransparency(img *image.NRGBA) *image.NRGBA {
|
||||
a := img.Pix[0:4]
|
||||
|
||||
if a[3] == 0 {
|
||||
return img
|
||||
}
|
||||
|
||||
output := Clone(img)
|
||||
|
||||
for i, l := 0, len(output.Pix); i < l; i += 4 {
|
||||
if output.Pix[i+0] != a[0] || output.Pix[i+1] != a[1] || output.Pix[i+2] != a[2] || output.Pix[i+3] != a[3] {
|
||||
continue
|
||||
}
|
||||
|
||||
output.Pix[i+3] = 0
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func Clone(img *image.NRGBA) *image.NRGBA {
|
||||
bounds := img.Bounds()
|
||||
output := image.NewNRGBA(bounds)
|
||||
|
||||
draw.Draw(output, bounds, img, image.Pt(0, 0), draw.Src)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func GetSlimOffset(slim bool) int {
|
||||
if slim {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func CompositeTransform(bottom, top *image.NRGBA, mat matrix.Matrix, x, y float64) *image.NRGBA {
|
||||
output := image.NewNRGBA(bottom.Bounds())
|
||||
|
||||
draw.Draw(output, bottom.Bounds(), bottom, image.Pt(0, 0), draw.Src)
|
||||
|
||||
transformer := drw.NearestNeighbor
|
||||
|
||||
fx, fy := float64(x), float64(y)
|
||||
|
||||
m := mat.Translate(fx, fy)
|
||||
|
||||
transformer.Transform(output, f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0}, top, top.Bounds(), draw.Over, nil)
|
||||
|
||||
return output
|
||||
}
|
||||
47
src/util/skin.go
Normal file
47
src/util/skin.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"image"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed steve.png
|
||||
rawSteveData []byte
|
||||
|
||||
//go:embed alex.png
|
||||
rawAlexData []byte
|
||||
|
||||
steveSkin *image.NRGBA = image.NewNRGBA(image.Rect(0, 0, 64, 64))
|
||||
alexSkin *image.NRGBA = image.NewNRGBA(image.Rect(0, 0, 64, 64))
|
||||
)
|
||||
|
||||
func init() {
|
||||
rawSteveSkin, err := png.Decode(bytes.NewReader(rawSteveData))
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
draw.Draw(steveSkin, rawSteveSkin.Bounds(), rawSteveSkin, image.Pt(0, 0), draw.Src)
|
||||
|
||||
rawAlexSkin, err := png.Decode(bytes.NewReader(rawAlexData))
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
draw.Draw(alexSkin, rawAlexSkin.Bounds(), rawAlexSkin, image.Pt(0, 0), draw.Src)
|
||||
}
|
||||
|
||||
func GetDefaultSkin(slim bool) *image.NRGBA {
|
||||
if slim {
|
||||
return alexSkin
|
||||
}
|
||||
|
||||
return steveSkin
|
||||
}
|
||||
BIN
src/util/steve.png
Normal file
BIN
src/util/steve.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
50
src/util/uuid.go
Normal file
50
src/util/uuid.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/src/redis"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetUUID(r *redis.Redis, value string) (string, error) {
|
||||
value = strings.ToLower(strings.ReplaceAll(value, "-", ""))
|
||||
|
||||
if len(value) == 32 {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
cache, ok, err := r.GetString(fmt.Sprintf("uuid:%s", value))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ok {
|
||||
if Debug {
|
||||
log.Printf("[DEBUG] Retrieved UUID for player %s (%s) from cache\n", value, cache)
|
||||
}
|
||||
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
profile, err := GetProfileByUsername(value)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if profile == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if err = r.Set(fmt.Sprintf("uuid:%s", value), profile.ID, 0); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if Debug {
|
||||
log.Printf("[DEBUG] Fetched UUID for player %s (%s) from Mojang\n", profile.Name, profile.ID)
|
||||
}
|
||||
|
||||
return profile.ID, nil
|
||||
}
|
||||
243
src/util/yggdrasil.go
Normal file
243
src/util/yggdrasil.go
Normal file
@@ -0,0 +1,243 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"main/src/redis"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BasicProfile struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Properties []struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
Signature string `json:"signature"`
|
||||
} `yaml:"properties"`
|
||||
}
|
||||
|
||||
type Textures struct {
|
||||
Textures map[string]struct {
|
||||
URL string `json:"url"`
|
||||
Metadata struct {
|
||||
Model string `json:"model"`
|
||||
} `json:"metadata"`
|
||||
} `json:"textures"`
|
||||
}
|
||||
|
||||
func GetProfileByUsername(username string) (*BasicProfile, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("https://api.mojang.com/users/profiles/minecraft/%s", username))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 204 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
result := &BasicProfile{}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(body, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetPlayerProfile(uuid string) (*Profile, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("https://sessionserver.mojang.com/session/minecraft/profile/%s", uuid))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 404 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
result := &Profile{}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(body, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetPlayerSkin(r *redis.Redis, uuid string) (*image.NRGBA, bool, error) {
|
||||
if len(uuid) < 1 {
|
||||
return GetDefaultSkin(false), false, nil
|
||||
}
|
||||
|
||||
cache, ok, err := r.GetBytes(fmt.Sprintf("skin:%s", uuid))
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if ok {
|
||||
slim, err := r.Exists(fmt.Sprintf("slim:%s", uuid))
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
img, format, err := image.Decode(bytes.NewReader(cache))
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if Debug {
|
||||
log.Printf("[DEBUG] Retrieved skin for %s from cache\n", uuid)
|
||||
}
|
||||
|
||||
if format != "NRGBA" {
|
||||
newImage := image.NewNRGBA(img.Bounds())
|
||||
|
||||
draw.Draw(newImage, img.Bounds(), img, image.Pt(0, 0), draw.Src)
|
||||
|
||||
return newImage, slim, nil
|
||||
}
|
||||
|
||||
return img.(*image.NRGBA), slim, nil
|
||||
}
|
||||
|
||||
profile, err := GetPlayerProfile(uuid)
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if profile == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
var value string
|
||||
|
||||
for _, property := range profile.Properties {
|
||||
if property.Name != "textures" {
|
||||
continue
|
||||
}
|
||||
|
||||
value = property.Value
|
||||
}
|
||||
|
||||
if len(value) < 1 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
propertyValue, err := base64.StdEncoding.DecodeString(value)
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
textures := &Textures{}
|
||||
|
||||
if err = json.Unmarshal(propertyValue, textures); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
skinProperty, ok := textures.Textures["SKIN"]
|
||||
|
||||
if !ok {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
slim := skinProperty.Metadata.Model == "slim"
|
||||
|
||||
resp, err := http.Get(skinProperty.URL)
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 404 {
|
||||
return nil, slim, nil
|
||||
}
|
||||
|
||||
return nil, false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
img, format, err := image.Decode(bytes.NewReader(body))
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if slim {
|
||||
if err = r.Set(fmt.Sprintf("slim:%s", uuid), "true", time.Hour*24); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
} else {
|
||||
if err = r.Delete(fmt.Sprintf("slim:%s", uuid)); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
if err = r.Set(fmt.Sprintf("skin:%s", uuid), body, time.Hour*24); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if Debug {
|
||||
log.Printf("[DEBUG] Fetched skin for %s from Mojang\n", uuid)
|
||||
}
|
||||
|
||||
if format != "NRGBA" {
|
||||
newImage := image.NewNRGBA(img.Bounds())
|
||||
|
||||
draw.Draw(newImage, img.Bounds(), img, image.Pt(0, 0), draw.Src)
|
||||
|
||||
return newImage, slim, nil
|
||||
}
|
||||
|
||||
return img.(*image.NRGBA), slim, nil
|
||||
}
|
||||
Reference in New Issue
Block a user