From 7c811247ecf143e08c576986a884bedadc57dd66 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 05 六月 2020 18:29:41 +0800
Subject: [PATCH] add refresh token to resp

---
 service/SysService.go |   98 ++++++++++++++++++++++++++-----------------------
 1 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/service/SysService.go b/service/SysService.go
index 9fa1619..62c1d04 100644
--- a/service/SysService.go
+++ b/service/SysService.go
@@ -9,6 +9,7 @@
 	"mime/multipart"
 	"os"
 	"os/exec"
+	"path"
 	"strings"
 	"webserver/extend/config"
 	"webserver/extend/util"
@@ -76,7 +77,7 @@
 		dirFiles, _ := ioutil.ReadDir(fileTmpPath)
 		if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
 			//琛ㄧず鎵�鏈夊垎鍧楅兘涓婁紶浜嗭紝闇�瑕乵erge
-			if !sv.MergeChunks(fileTmpPath, mergedFilePath) {
+			if !MergeChunks(fileTmpPath, mergedFilePath) {
 				return false
 			}
 		}
@@ -84,7 +85,7 @@
 	return true
 }
 
-func (sv SysService) PatchUpload(arg *FileUploadVo) bool {
+func (sv SysService) PatchUpload(arg *FileUploadVo) (bool,bool) {
 	configPatchPath := ""
 	if config.Server.PatchPath != "" {
 		configPatchPath = config.Server.PatchPath
@@ -93,19 +94,17 @@
 	}
 	defer (*arg.File).Close()
 	if !util.CreateDirectory(configPatchPath) {
-		return false
+		return false, false
 	}
-	index := strings.LastIndex(arg.Header.Filename, ".")
-	if index < 0 {
-		return false
-	}
-	subfix := arg.Header.Filename[index:]
+
+	filenameWithSuffix := path.Base(arg.Header.Filename)
+	subfix := path.Ext(filenameWithSuffix)
 	MD5Str := arg.Identifier
 	logger.Debug("Identifier:",MD5Str)
 	fileTmpPath := configPatchPath + "/"+MD5Str
 	if !util.Exists(fileTmpPath) {
 		if !util.CreateDirectory(fileTmpPath) {
-			return false
+			return false, false
 		}
 	}
 	chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber)
@@ -114,13 +113,13 @@
 		rmErr := os.Remove(fileSavePath)
 		if rmErr != nil {
 			logger.Debug("rmErr:",rmErr)
-			return false
+			return false, false
 		}
 	}
 	file, e := os.Create(fileSavePath)
 	if e !=nil {
 		logger.Debug("os.Create err:",e,"fileSavePath:",fileSavePath)
-		return false
+		return false, false
 	}
 	defer file.Close()
 	writer := bufio.NewWriter(file)
@@ -128,12 +127,15 @@
 	n, err := (*arg.File).ReadAt(chunkData, 0)
 	if n ==0 || err !=nil {
 		logger.Debug("read chunkData err:",err,"n:",n)
-		return false
+		return false, false
 	}
 	nn, err2 := writer.Write(chunkData)
 	if nn ==0 || err2 !=nil {
 		logger.Debug("write chunkData err:",err2,"nn:",nn)
-		return false
+		return false, false
+	}
+	if err = writer.Flush(); err != nil {
+		logger.Debug("write flush err:",err)
 	}
 	isComplete := false
 	dirFiles, _ := ioutil.ReadDir(fileTmpPath)
@@ -141,13 +143,13 @@
 		isComplete = true
 	}
 	if isComplete {
-		if sv.MergeChunks(fileTmpPath,fileTmpPath + subfix) {
+		if MergeChunks(fileTmpPath,fileTmpPath + subfix) {
 			logger.Debug("merge all chunks success,identifier:",MD5Str,"fileName:",arg.FileName)
 		} else {
-			return false
+			return false, isComplete
 		}
 	}
-	return true
+	return true, isComplete
 }
 
 //upgrade
@@ -161,11 +163,10 @@
 	} else {
 		configPatchPath = "/opt/vasystem/patch"
 	}
-	index := strings.LastIndex(filename, ".")
-	if index < 0 {
-		return false,errors.New("闈炴硶鐨勫崌绾у帇缂╁寘鏂囦欢")
-	}
-	ext := filename[index:]
+
+	filenameWithSuffix := path.Base(filename)
+	ext := path.Ext(filenameWithSuffix)
+
 	zipFilePath := configPatchPath + "/"+identifier+ext
 	if util.Exists(zipFilePath) {
 		//鏍¢獙md5
@@ -174,15 +175,11 @@
 			return false,errors.New("鑾峰彇鍗囩骇鍘嬬缉鍖卪d5澶辫触")
 		}
 		if strMd5 == identifier {
-			if util.ZipCheck(zipFilePath) {
-				if !updatePatch(identifier, ext) {
-					return false,errors.New("鎵ц鍗囩骇杩囩▼寮傚父")
-				}
-				return true,nil
-			} else {
-				logger.Debug("not a valid zip file,path:",zipFilePath)
-				return false,errors.New("鍗囩骇绋嬪簭瑙e帇澶辫触锛岃纭畾涓婁紶鐨勮ˉ涓佹槸zip鏍煎紡")
+			if !updatePatch(identifier, ext) {
+				return false,errors.New("鎵ц鍗囩骇杩囩▼寮傚父,璇风‘瀹氫笂浼犵殑琛ヤ竵鏄痶ar.gz鏍煎紡")
 			}
+			return true,nil
+
 		} else {
 			logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
 			return false,errors.New("鏍¢獙鍗囩骇鏂囦欢澶辫触")
@@ -225,43 +222,52 @@
 		configPatchPath = "/opt/vasystem/patch"
 	}
 	//1.瑙e帇缂╂洿鏂板寘
-	unZipPath := configPatchPath+"/"+identifier+"_basic/"
-	if util.Exists(unZipPath) {
+	unPackPath := configPatchPath+"/"+identifier+"_basic/"
+	if util.Exists(unPackPath) {
 		//姝ょ増鏈凡缁忔洿鏂拌繃
-		rmErr := os.RemoveAll(unZipPath)
+		rmErr := os.RemoveAll(unPackPath)
 		if rmErr !=nil {
 			return false
 		}
 	}
-	if !util.CreateDirectory(unZipPath) {
+	if !util.CreateDirectory(unPackPath) {
 		return false
 	}
 
-	zipFilePath := configPatchPath+"/"+identifier+ext
-	err := util.UnZip(zipFilePath, unZipPath)
+	unPackFilePath := configPatchPath+"/"+identifier+ext
+	err := util.UnTarGz(unPackFilePath, unPackPath)
 	if err !=nil {
-		logger.Debug("UnZip err:",err,"zipFile:",zipFilePath)
+		logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
 		return false
 	}
+
+	//濡傛灉閫氱敤鑴氭湰鏈夋洿鏂帮紝鍒欐洿鏂伴�氱敤鑴氭湰
+	if util.Exists(unPackPath+"updatePatch.sh") {
+		cpStr := fmt.Sprintf("cp %s /opt/vasystem/bin",unPackPath+"updatePatch.sh")
+		b, err := ExecCmd(cpStr)
+		if err != nil {
+			logger.Debug("cp updatePatch.sh to bin err:",err,"result:",string(b))
+			return false
+		}
+	}
+
 	//鍒ゆ柇鏇存柊鍖呴噷鏄惁鏈夎ˉ涓佽剼鏈紝濡傛灉鏈夊垯鎵ц锛屽惁鍒欐墽琛寀pdatePatch.sh
-	updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unZipPath,zipFilePath,configPatchPath+"/"+identifier)
-	if util.Exists(unZipPath+"upgrade.sh") {
-		updateCmd = fmt.Sprintf("%supgrade.sh %s %s %s &",unZipPath,unZipPath,zipFilePath,configPatchPath+"/"+identifier)
-		logger.Debug("upgrade use ",unZipPath,"upgrade.sh")
+	updateCmd := fmt.Sprintf("./updatePatch.sh %s %s %s &",unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
+	if util.Exists(unPackPath+"upgrade.sh") {
+		updateCmd = fmt.Sprintf("%supgrade.sh %s %s %s &",unPackPath,unPackPath,unPackFilePath,configPatchPath+"/"+identifier)
 	}
 	//2.鏇存柊绯荤粺
-	var cmd *exec.Cmd
-	cmd = exec.Command("/bin/sh","-c", updateCmd)
-	if b, err := cmd.Output(); err != nil {
-		logger.Debug("updatePatch err:",err,"result:",string(b))
+	b,err := ExecCmd(updateCmd)
+	if err != nil {
+		logger.Debug("upgrade err:",err,"result:",string(b),"cmd:",updateCmd)
 		return false
 	} else {
-		logger.Debug("updatePatch result:",string(b),"cmd:",updateCmd)
+		logger.Debug("upgrade result:",string(b),"cmd:",updateCmd)
 	}
 	return true
 }
 
-func (sv SysService) MergeChunks(chunkPath string, storePath string) bool {
+func MergeChunks(chunkPath string, storePath string) bool {
 	var cmd *exec.Cmd
 	cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("./mergeAll.sh %s %s", chunkPath, storePath))
 	if b, err := cmd.Output(); err != nil {

--
Gitblit v1.8.0