Procházet zdrojové kódy

minor update to prevent nil ptr

AY před 4 roky
rodič
revize
192e73c7a0
6 změnil soubory, kde provedl 38 přidání a 5 odebrání
  1. binární
      __debug_bin
  2. binární
      default_skin.png
  3. 27 0
      mod/mc/common.go
  4. 9 5
      mod/mc/skin.go
  5. 2 0
      mod/mc/struct.go
  6. binární
      server/skin/1234.png

binární
__debug_bin


binární
default_skin.png


+ 27 - 0
mod/mc/common.go

@@ -1,6 +1,7 @@
 package mc
 
 import (
+	"fmt"
 	"io"
 	"io/ioutil"
 	"net/http"
@@ -42,3 +43,29 @@ func downloadFile(filepath string, url string) error {
 	_, err = io.Copy(out, resp.Body)
 	return err
 }
+
+//https://opensource.com/article/18/6/copying-files-go
+func copy(src, dst string) (int64, error) {
+	sourceFileStat, err := os.Stat(src)
+	if err != nil {
+		return 0, err
+	}
+
+	if !sourceFileStat.Mode().IsRegular() {
+		return 0, fmt.Errorf("%s is not a regular file", src)
+	}
+
+	source, err := os.Open(src)
+	if err != nil {
+		return 0, err
+	}
+	defer source.Close()
+
+	destination, err := os.Create(dst)
+	if err != nil {
+		return 0, err
+	}
+	defer destination.Close()
+	nBytes, err := io.Copy(destination, source)
+	return nBytes, err
+}

+ 9 - 5
mod/mc/skin.go

@@ -13,11 +13,15 @@ func (mc *Handler) DownloadSkin() {
 		resp := httpReq("https://sessionserver.mojang.com/session/minecraft/profile/" + mc.UUID)
 		var profileArray profile
 		json.Unmarshal(resp, &profileArray)
-
-		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)
+		if len(profileArray.Error) > 0 {
+			log.Println("Error when reading profiles. Download aborted.")
+			copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
+		} else {
+			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 {
 		log.Println("Skin exists, not downloading")
 	}

+ 2 - 0
mod/mc/struct.go

@@ -7,6 +7,8 @@ type profile struct {
 		Name  string `json:"name"`
 		Value string `json:"value"`
 	} `json:"properties"`
+	Error string `json:"error"`
+	Path  string `json:"path"`
 }
 
 type texture struct {

binární
server/skin/1234.png