Загрузка плаща вынесена в отдельный метод, реализовано отображение ссылки на плащ в запросах на текстуры

This commit is contained in:
ErickSkrauch
2016-09-22 19:32:00 +03:00
parent a9acfb954f
commit 9467911025
11 changed files with 94 additions and 26 deletions

43
lib/data/CapeItem.go Normal file
View File

@@ -0,0 +1,43 @@
package data
import (
"io"
"os"
"fmt"
"strings"
"crypto/md5"
"encoding/hex"
"elyby/minecraft-skinsystem/lib/services"
)
type CapeItem struct {
File *os.File
}
func FindCapeByUsername(username string) (CapeItem, error) {
var record CapeItem
file, err := os.Open(services.RootFolder + "/data/capes/" + strings.ToLower(username) + ".png")
if (err != nil) {
return record, CapeNotFound{username}
}
record.File = file
return record, err
}
func (cape *CapeItem) CalculateHash() string {
hasher := md5.New()
io.Copy(hasher, cape.File)
return hex.EncodeToString(hasher.Sum(nil))
}
type CapeNotFound struct {
Who string
}
func (e CapeNotFound) Error() string {
return fmt.Sprintf("Cape file not found. Required username \"%v\"", e.Who)
}

View File

@@ -1,11 +0,0 @@
package data
import "fmt"
type DataNotFound struct {
Who string
}
func (e DataNotFound) Error() string {
return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who)
}

View File

@@ -2,6 +2,7 @@ package data
import (
"log"
"fmt"
"encoding/json"
"elyby/minecraft-skinsystem/lib/services"
@@ -59,11 +60,11 @@ func (s *SkinItem) Delete() {
pool.Cmd("EXEC")
}
func FindByUsername(username string) (SkinItem, error) {
func FindSkinByUsername(username string) (SkinItem, error) {
var record SkinItem;
response := services.RedisPool.Cmd("GET", tools.BuildKey(username));
if (response.IsType(redis.Nil)) {
return record, DataNotFound{username}
return record, SkinNotFound{username}
}
result, err := response.Str()
@@ -79,13 +80,21 @@ func FindByUsername(username string) (SkinItem, error) {
return record, err
}
func FindById(id int) (SkinItem, error) {
func FindSkinById(id int) (SkinItem, error) {
response := services.RedisPool.Cmd("HGET", accountIdToUsernameKey, id);
if (response.IsType(redis.Nil)) {
return SkinItem{}, DataNotFound{"unknown"}
return SkinItem{}, SkinNotFound{"unknown"}
}
username, _ := response.Str()
return FindByUsername(username)
return FindSkinByUsername(username)
}
type SkinNotFound struct {
Who string
}
func (e SkinNotFound) Error() string {
return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who)
}

View File

@@ -2,6 +2,7 @@ package data
type TexturesResponse struct {
Skin *Skin `json:"SKIN"`
Cape *Cape `json:"CAPE,omitempty"`
}
type Skin struct {
@@ -13,3 +14,8 @@ type Skin struct {
type SkinMetadata struct {
Model string `json:"model"`
}
type Cape struct {
Url string `json:"url"`
Hash string `json:"hash"`
}