From 0d858e8610a79b15cb337daf134c4a00962dd477 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期四, 04 六月 2020 16:44:29 +0800 Subject: [PATCH] add sdk upload --- controllers/syssetcont.go | 28 ------ service/SdkInstall.go | 36 ++++++-- service/SysService.go | 24 +++--- controllers/sdk.go | 123 ++++++++++++++++++++++++++++++ router/router.go | 4 5 files changed, 163 insertions(+), 52 deletions(-) diff --git a/controllers/sdk.go b/controllers/sdk.go index 847ec2b..ae10470 100644 --- a/controllers/sdk.go +++ b/controllers/sdk.go @@ -1,9 +1,12 @@ package controllers import ( + "net/http" + "strconv" "webserver/extend/code" "webserver/extend/config" "webserver/extend/util" + "webserver/middlewares/auth" "webserver/service" "basic.com/dbapi.git" @@ -150,6 +153,126 @@ } } +// @Summary 绠楁硶瀹夎鍖呬笂浼�(鍒嗗潡妫�鏌�) +// @Description 绠楁硶瀹夎鍖呬笂浼�(鍒嗗潡妫�鏌�) +// @Accept multipart/form-data +// @Produce json +// @Tags sdk +// @Param chunkNumber formData int true "褰撳墠鏄鍑犱釜鍒嗗潡" +// @Param chunkSize formData int true "姣忎竴鍧楃殑澶у皬" +// @Param currentChunkSize formData int true "褰撳墠鍧楃殑澶у皬" +// @Param identifier formData string true "鏁翠釜鏂囦欢鐨勫敮涓�鏍囪瘑锛岀洰鍓嶆槸md5" +// @Param filename formData string true "鏂囦欢鍚嶇О" +// @Param relativePath formData string true "鏂囦欢鍦ㄥ鎴风鐢佃剳鐨勮矾寰�" +// @Param totalChunks formData int true "鏂囦欢鎬诲潡鏁�" +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", success:false}" +// @Router /data/api-v/sdk/upload [get] +func (sc SdkController) Upload(c *gin.Context) { + authDriver := auth.GenerateAuthDriver() + userM := (*authDriver).User(c) + if userM ==nil { + util.ResponseFormat(c,code.TokenNotFound,"鐧诲綍杩囨湡锛岃鐧诲綍") + return + } + chunkNumber, e1 := strconv.Atoi(c.Request.FormValue("chunkNumber")) + chunkSize, e2 := strconv.Atoi(c.Request.FormValue("chunkSize")) + currentChunkSize, e3 := strconv.Atoi(c.Request.FormValue("currentChunkSize")) + identifier := c.Request.FormValue("identifier") + filename := c.Request.FormValue("filename") + relativePath := c.Request.FormValue("relativePath") + totalChunks, e5 := strconv.Atoi(c.Request.FormValue("totalChunks")) + if identifier == "" || e1 != nil || e2 != nil || e3 != nil || e5 !=nil { + util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎") + return + } + var arg = service.FileChunkCheckVo { + UserId: userM["id"].(string), + FileName: filename, + Identifier: identifier, + RelativePath: relativePath, + TotalChunks: totalChunks, + ChunkNumber: chunkNumber, + ChunkSize: chunkSize, + CurrentChunkSize:currentChunkSize, + } + var sv service.SysService + if b := sv.CheckUpdateFile(&arg);b { + c.String(http.StatusOK,"found") + } else { + c.String(http.StatusNoContent,"") + } +} + +// @Security ApiKeyAuth +// @Summary 绠楁硶瀹夎鍖呬笂浼� +// @Description 绠楁硶瀹夎鍖呬笂浼� +// @Accept multipart/form-data +// @Produce json +// @Tags sysset +// @Param chunkNumber formData int true "褰撳墠鏄鍑犱釜鍒嗗潡" +// @Param chunkSize formData int true "姣忎竴鍧楃殑澶у皬" +// @Param currentChunkSize formData int true "褰撳墠鍧楃殑澶у皬" +// @Param totalSize formData string true "鏂囦欢鎬诲ぇ灏�" +// @Param identifier formData string true "鏁翠釜鏂囦欢鐨勫敮涓�鏍囪瘑锛岀洰鍓嶆槸md5" +// @Param filename formData string true "鏂囦欢鍚嶇О" +// @Param relativePath formData string true "鏂囦欢鍦ㄥ鎴风鐢佃剳鐨勮矾寰�" +// @Param totalChunks formData int true "鏂囦欢鎬诲潡鏁�" +// @Param file formData file true "鏂囦欢鍒嗙墖鍐呭" +// @Success 200 {string} json "{"code":200, msg:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", success:false}" +// @Router /data/api-v/sdk/upload [post] +func (sc SdkController) UploadPack(c *gin.Context) { + authDriver := auth.GenerateAuthDriver() + userM := (*authDriver).User(c) + if userM == nil { + util.ResponseFormat(c, code.TokenNotFound, "鐧诲綍杩囨湡锛岃鐧诲綍") + return + } + chunkNumber, e1 := strconv.Atoi(c.Request.FormValue("chunkNumber")) + chunkSize, e2 := strconv.Atoi(c.Request.FormValue("chunkSize")) + currentChunkSize, e3 := strconv.Atoi(c.Request.FormValue("currentChunkSize")) + totalSize, e4 := strconv.ParseInt(c.Request.FormValue("totalSize"), 10, 64) + identifier := c.Request.FormValue("identifier") + filename := c.Request.FormValue("filename") + relativePath := c.Request.FormValue("relativePath") + totalChunks, e5 := strconv.Atoi(c.Request.FormValue("totalChunks")) + file, header, e6 := c.Request.FormFile("file") + if identifier == "" || e1 != nil || e2 != nil || e3 != nil || e4 != nil || e5 != nil || e6 != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + var arg = service.FileUploadVo{ + UserId: userM["id"].(string), + FileName: filename, + Identifier: identifier, + RelativePath: relativePath, + TotalSize: totalSize, + TotalChunks: totalChunks, + ChunkNumber: chunkNumber, + ChunkSize: chunkSize, + CurrentChunkSize: currentChunkSize, + File: &file, + Header: header, + } + var sv service.SysService + if b,isComplete := sv.PatchUpload(&arg); b { + if isComplete { //涓婁紶瀹屾瘯闇�瑕佽嚜鍔ㄨЕ鍙戝畨瑁� + var sv service.SdkInstallService + if b,err := sv.SdkInstall(identifier, filename);b { + util.ResponseFormat(c,&code.Code{http.StatusOK, true, "绠楁硶瀹夎鎴愬姛"},"绠楁硶瀹夎鎴愬姛") + return + } else { + util.ResponseFormat(c,&code.Code{http.StatusBadRequest, false, "绠楁硶瀹夎澶辫触"},err.Error()) + return + } + } + util.ResponseFormat(c, code.Success, "") + } else { + util.ResponseFormat(c, code.ComError, "") + } +} + func (sc SdkController) SdkDownLoad(c *gin.Context) { var soApi dbapi.SdkApi var sdkConfig map[string]interface{} diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go index 396b781..ee7e0d6 100644 --- a/controllers/syssetcont.go +++ b/controllers/syssetcont.go @@ -602,7 +602,7 @@ Header: header, } var sv service.SysService - if b := sv.PatchUpload(&arg); b { + if b,_ := sv.PatchUpload(&arg); b { util.ResponseFormat(c, code.Success, "") } else { util.ResponseFormat(c, code.ComError, "") @@ -631,32 +631,6 @@ var sv service.SysService if b,err := sv.Upgrade(identifier, filename);b { util.ResponseFormat(c,code.UpgradeSuccess,"鍗囩骇鎴愬姛") - } else { - util.ResponseFormat(c,code.UpgradeFail,err.Error()) - } -} - -// @Security ApiKeyAuth -// @Summary 绠楁硶瀹夎鍖呭紑濮嬪畨瑁� -// @Description 绠楁硶瀹夎鍖呭紑濮嬪畨瑁� -// @Accept multipart/form-data -// @Produce json -// @Tags sysset -// @Param identifier formData string true "鏁翠釜鏂囦欢鐨勫敮涓�鏍囪瘑锛岀洰鍓嶆槸md5" -// @Param filename formData string true "鏂囦欢鍚嶇О" -// @Success 200 {string} json "{"code":200, msg:"", success:true}" -// @Failure 500 {string} json "{"code":500, msg:"", success:false}" -// @Router /data/api-v/sysset/sdkInstall [post] -func (sset SysSetController) SdkInstall(c *gin.Context) { - identifier := c.Request.FormValue("identifier") - filename := c.Request.FormValue("filename") - if identifier == "" || filename == "" { - util.ResponseFormat(c,code.RequestParamError,"") - return - } - var sv service.SysService - if b,err := sv.SdkInstall(identifier, filename);b { - util.ResponseFormat(c,code.UpgradeSuccess,"绠楁硶瀹夎鎴愬姛") } else { util.ResponseFormat(c,code.UpgradeFail,err.Error()) } diff --git a/router/router.go b/router/router.go index 12d5af8..d45ed44 100644 --- a/router/router.go +++ b/router/router.go @@ -257,8 +257,6 @@ vsset.GET("/patchUpdate", ssController.PatchUpdateCheck) vsset.POST("/patchUpdate", ssController.PatchUpdate) vsset.POST("/upgrade", ssController.Upgrade) - - vsset.POST("/sdkInstall", ssController.SdkInstall) } ic := r.Group(urlPrefix + "/info") @@ -274,6 +272,8 @@ sdkApi.GET("/findByTaskId", sdkController.FindByTaskId) sdkApi.GET("/getById", sdkController.GetById) sdkApi.GET("/sdkDownload", sdkController.SdkDownLoad) + sdkApi.GET("/upload", sdkController.Upload) + sdkApi.POST("/upload", sdkController.UploadPack) } //绠楁硶鍙傛暟 diff --git a/service/SdkInstall.go b/service/SdkInstall.go index c0d784b..b930aab 100644 --- a/service/SdkInstall.go +++ b/service/SdkInstall.go @@ -2,7 +2,6 @@ import ( "basic.com/dbapi.git" - //"basic.com/dbapi.git" "basic.com/valib/logger.git" "encoding/json" "errors" @@ -14,8 +13,12 @@ "webserver/extend/util" ) +type SdkInstallService struct { + +} + //绠楁硶瀹夎鍖呭畨瑁� -func (sv SysService) SdkInstall(identifier string,filename string) (bool,error) { +func (sv SdkInstallService) SdkInstall(identifier string,filename string) (bool,error) { configPatchPath := "" if config.Server.PatchPath != "" { configPatchPath = config.Server.PatchPath @@ -30,6 +33,7 @@ if util.Exists(zipFilePath) { strMd5, e := util.FileMd5(zipFilePath) if e !=nil || strMd5 == "" { + go os.Remove(zipFilePath) return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触") } if strMd5 == identifier { @@ -39,6 +43,7 @@ return true,nil } else { + go os.Remove(zipFilePath) logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal") return false,errors.New("鏍¢獙瀹夎鏂囦欢澶辫触") } @@ -56,27 +61,36 @@ configPatchPath = "/opt/vasystem/patch" } //1.瑙e帇缂╂洿鏂板寘 - unPackPath := configPatchPath+"/"+identifier+"_basic/" - if util.Exists(unPackPath) { + unPackTargetPath := configPatchPath+"/"+identifier+"_basic/" + unPackFilePath := configPatchPath+"/"+identifier+ext + defer func() { + if util.Exists(unPackTargetPath) { + os.RemoveAll(unPackTargetPath) + } + if util.Exists(unPackFilePath) { + os.RemoveAll(unPackFilePath) + } + }() + + if util.Exists(unPackTargetPath) { //姝ょ増鏈凡缁忔洿鏂拌繃 - rmErr := os.RemoveAll(unPackPath) + rmErr := os.RemoveAll(unPackTargetPath) if rmErr !=nil { return false,rmErr } } - if !util.CreateDirectory(unPackPath) { + if !util.CreateDirectory(unPackTargetPath) { return false, errors.New("鍒涘缓鍘嬬缉鏂囦欢澶瑰け璐�") } - unPackFilePath := configPatchPath+"/"+identifier+ext - logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackPath) - _,err := util.UnTarGzByCmd(unPackFilePath, unPackPath) + logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackTargetPath) + _,err := util.UnTarGzByCmd(unPackFilePath, unPackTargetPath) if err !=nil { logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath) return false, err } targetFileName := "" - err = filepath.Walk(unPackPath, func(path string, f os.FileInfo, err error) error { + err = filepath.Walk(unPackTargetPath, func(path string, f os.FileInfo, err error) error { if f == nil { return err } @@ -88,7 +102,7 @@ if err != nil { return false, err } - unPackPath = unPackPath + targetFileName +"/" + unPackPath := unPackTargetPath + targetFileName +"/" //瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁� //1.瑙f瀽瀹夎璇存槑ins.inc incPath := unPackPath+"ins.inc" diff --git a/service/SysService.go b/service/SysService.go index 1a1f14c..62c1d04 100644 --- a/service/SysService.go +++ b/service/SysService.go @@ -77,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 } } @@ -85,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 @@ -94,7 +94,7 @@ } defer (*arg.File).Close() if !util.CreateDirectory(configPatchPath) { - return false + return false, false } filenameWithSuffix := path.Base(arg.Header.Filename) @@ -104,7 +104,7 @@ fileTmpPath := configPatchPath + "/"+MD5Str if !util.Exists(fileTmpPath) { if !util.CreateDirectory(fileTmpPath) { - return false + return false, false } } chunkAlignNum := util.FormatNum(arg.TotalChunks, arg.ChunkNumber) @@ -113,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) @@ -127,12 +127,12 @@ 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) @@ -143,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 @@ -267,7 +267,7 @@ 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