package mc import ( "encoding/base64" "encoding/json" "log" "os" "regexp" ) //DownloadSkin is exported function func (mc *Handler) DownloadSkin() { if _, err := os.Stat(mc.savePath + "/" + mc.UUID + ".png"); os.IsNotExist(err) { if IsValidUUID(mc.UUID) { resp := httpReq("https://sessionserver.mojang.com/session/minecraft/profile/" + mc.UUID) var profileArray profile json.Unmarshal(resp, &profileArray) if len(profileArray.Error) > 0 { log.Println("Error when reading profiles. Download aborted.") copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png") } else { if len(profileArray.Properties) > 0 { decoded, _ := base64.URLEncoding.DecodeString(profileArray.Properties[0].Value) var textureArray texture json.Unmarshal([]byte(decoded), &textureArray) downloadFile(mc.savePath+"/"+mc.UUID+".png", textureArray.Textures.SKIN.URL) } else { copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png") log.Println("Error when reading profiles. Download aborted.") } } } else { log.Println("Invaild UUID...") } } else { //log.Println("Skin exists, not downloading") } } //vaild UUID //https://stackoverflow.com/questions/25051675/how-to-validate-uuid-v4-in-go func IsValidUUID(uuid string) bool { 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}$") return r.MatchString(uuid) }