skin.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package mc
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "log"
  6. "os"
  7. "regexp"
  8. )
  9. //DownloadSkin is exported function
  10. func (mc *Handler) DownloadSkin() {
  11. if _, err := os.Stat(mc.savePath + "/" + mc.UUID + ".png"); os.IsNotExist(err) {
  12. if IsValidUUID(mc.UUID) {
  13. resp := httpReq("https://sessionserver.mojang.com/session/minecraft/profile/" + mc.UUID)
  14. var profileArray profile
  15. json.Unmarshal(resp, &profileArray)
  16. if len(profileArray.Error) > 0 {
  17. log.Println("Error when reading profiles. Download aborted.")
  18. copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
  19. } else {
  20. if len(profileArray.Properties) > 0 {
  21. decoded, _ := base64.URLEncoding.DecodeString(profileArray.Properties[0].Value)
  22. var textureArray texture
  23. json.Unmarshal([]byte(decoded), &textureArray)
  24. downloadFile(mc.savePath+"/"+mc.UUID+".png", textureArray.Textures.SKIN.URL)
  25. } else {
  26. copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
  27. log.Println("Error when reading profiles. Download aborted.")
  28. }
  29. }
  30. } else {
  31. log.Println("Invaild UUID...")
  32. }
  33. } else {
  34. //log.Println("Skin exists, not downloading")
  35. }
  36. }
  37. //vaild UUID
  38. //https://stackoverflow.com/questions/25051675/how-to-validate-uuid-v4-in-go
  39. func IsValidUUID(uuid string) bool {
  40. r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  41. return r.MatchString(uuid)
  42. }