From 0bb9a4c37862e5098a596e121fb13fd12c2ca44f Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 29 五月 2020 14:14:15 +0800
Subject: [PATCH] install sdk tar.gz

---
 extend/util/zip.go            |   85 +++++++++++++-
 controllers/fileController.go |    3 
 controllers/syssetcont.go     |   26 ++++
 service/SdkInstall.go         |  229 ++++++++++++++++++++++++++++++++++++++
 router/router.go              |    2 
 5 files changed, 338 insertions(+), 7 deletions(-)

diff --git a/controllers/fileController.go b/controllers/fileController.go
index fb261b5..d6370fa 100644
--- a/controllers/fileController.go
+++ b/controllers/fileController.go
@@ -492,6 +492,9 @@
 					return
 				}
 				faceB = decodeF
+			} else {
+				util.ResponseFormat(c, code.ComError, "鏈潯浜鸿劯鎶撴媿鐗瑰緛涓虹┖锛岃妫�鏌�")
+				return
 			}
 		}
 
diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index cfcbe72..396b781 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -635,3 +635,29 @@
 		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/extend/util/zip.go b/extend/util/zip.go
index cb44ee3..f057359 100644
--- a/extend/util/zip.go
+++ b/extend/util/zip.go
@@ -5,10 +5,13 @@
 	"basic.com/valib/logger.git"
 	"compress/gzip"
 	"errors"
+	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"os"
 	"path"
+	"path/filepath"
 	"strings"
 )
 
@@ -26,7 +29,7 @@
 	logger.Debug("鏂囦欢鍚嶇О鏄�", FileName)
 	//鍒涘缓瀛樻斁鍘嬬缉鏂囦欢鐨勮矾寰�(濡傛灉鏂囦欢澶逛笉瀛樺湪)
 
-	if !dirExists(DesPathName) {
+	if !DirExists(DesPathName) {
 		os.MkdirAll(DesPathName, 0777)
 	}
 	logger.Debug("鍒涘缓鏂囦欢")
@@ -121,11 +124,11 @@
 	// 娓呯悊璺緞瀛楃涓�
 	src = path.Clean(src)
 	// 鍒ゆ柇瑕佹墦鍖呯殑鏂囦欢鎴栫洰褰曟槸鍚﹀瓨鍦�
-	if !exists(src) {
+	if !Exists(src) {
 		return errors.New("瑕佹墦鍖呯殑鏂囦欢鎴栫洰褰曚笉瀛樺湪锛�" + src)
 	}
 	// 鍒ゆ柇鐩爣鏂囦欢鏄惁瀛樺湪
-	if fileExists(dstTar) {
+	if FileExists(dstTar) {
 		if failIfExist { // 涓嶈鐩栧凡瀛樺湪鐨勬枃浠�
 			return errors.New("鐩爣鏂囦欢宸茬粡瀛樺湪锛�" + dstTar)
 		} else { // 瑕嗙洊宸插瓨鍦ㄧ殑鏂囦欢
@@ -296,19 +299,87 @@
 }
 
 // 鍒ゆ柇妗f鏄惁瀛樺湪
-func exists(name string) bool {
+func Exists(name string) bool {
 	_, err := os.Stat(name)
 	return err == nil || os.IsExist(err)
 }
 
 // 鍒ゆ柇鏂囦欢鏄惁瀛樺湪
-func fileExists(filename string) bool {
+func FileExists(filename string) bool {
 	fi, err := os.Stat(filename)
 	return (err == nil || os.IsExist(err)) && !fi.IsDir()
 }
 
 // 鍒ゆ柇鐩綍鏄惁瀛樺湪
-func dirExists(dirname string) bool {
+func DirExists(dirname string) bool {
 	fi, err := os.Stat(dirname)
 	return (err == nil || os.IsExist(err)) && fi.IsDir()
-}
\ No newline at end of file
+}
+
+
+//鏂囦欢澶瑰鍒�
+func CopyDir(src string, dest string) {
+	src_original := src
+	err := filepath.Walk(src, func(src string, f os.FileInfo, err error) error {
+		if f == nil {
+			return err
+		}
+		if f.IsDir() {
+			CopyDir(f.Name(), dest+"/"+f.Name())
+		} else {
+			fmt.Println(src)
+			fmt.Println(src_original)
+			fmt.Println(dest)
+
+			dest_new := strings.Replace(src, src_original, dest, -1)
+			fmt.Println(dest_new)
+			fmt.Println("CopyFile:" + src + " to " + dest_new)
+			CopyFile(src, dest_new)
+		}
+		return nil
+	})
+	if err != nil {
+		fmt.Printf("filepath.Walk() returned %v\n", err)
+	}
+}
+
+//澶ф枃浠跺鍒�
+func CopyFile(srcFileName string, dstFileName string) {
+	//鎵撳紑婧愭枃浠�
+	srcFile, err := os.Open(srcFileName)
+	if err != nil {
+		log.Fatalf("婧愭枃浠惰鍙栧け璐�,鍘熷洜鏄�:%v\n", err)
+	}
+	defer func() {
+		err = srcFile.Close()
+		if err != nil {
+			log.Fatalf("婧愭枃浠跺叧闂け璐�,鍘熷洜鏄�:%v\n", err)
+		}
+	}()
+
+	//鍒涘缓鐩爣鏂囦欢,绋嶅悗浼氬悜杩欎釜鐩爣鏂囦欢鍐欏叆鎷疯礉鍐呭
+	distFile, err := os.Create(dstFileName)
+	if err != nil {
+		log.Fatalf("鐩爣鏂囦欢鍒涘缓澶辫触,鍘熷洜鏄�:%v\n", err)
+	}
+	defer func() {
+		err = distFile.Close()
+		if err != nil {
+			log.Fatalf("鐩爣鏂囦欢鍏抽棴澶辫触,鍘熷洜鏄�:%v\n", err)
+		}
+	}()
+	//瀹氫箟鎸囧畾闀垮害鐨勫瓧鑺傚垏鐗�,姣忔鏈�澶氳鍙栨寚瀹氶暱搴�
+	var tmp = make([]byte, 1024*4)
+	//寰幆璇诲彇骞跺啓鍏�
+	for {
+		n, err := srcFile.Read(tmp)
+		n, _ = distFile.Write(tmp[:n])
+		if err != nil {
+			if err == io.EOF {//璇诲埌浜嗘枃浠舵湯灏�,骞朵笖鍐欏叆瀹屾瘯,浠诲姟瀹屾垚杩斿洖(鍏抽棴鏂囦欢鐨勬搷浣滅敱defer鏉ュ畬鎴�)
+				return
+			} else {
+				log.Fatalf("鎷疯礉杩囩▼涓彂鐢熼敊璇�,閿欒鍘熷洜涓�:%v\n", err)
+			}
+		}
+	}
+}
diff --git a/router/router.go b/router/router.go
index 9c87a5f..12d5af8 100644
--- a/router/router.go
+++ b/router/router.go
@@ -257,6 +257,8 @@
 		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")
diff --git a/service/SdkInstall.go b/service/SdkInstall.go
index 6d43c33..3ad4557 100644
--- a/service/SdkInstall.go
+++ b/service/SdkInstall.go
@@ -1 +1,230 @@
 package service
+
+import (
+	"basic.com/dbapi.git"
+	"basic.com/valib/logger.git"
+	"encoding/json"
+	"errors"
+	"io/ioutil"
+	"os"
+	"path"
+	"webserver/extend/config"
+	"webserver/extend/util"
+)
+
+//绠楁硶瀹夎鍖呭畨瑁�
+func (sv SysService) SdkInstall(identifier string,filename string) (bool,error) {
+	configPatchPath := ""
+	if config.Server.PatchPath != "" {
+		configPatchPath = config.Server.PatchPath
+	} else {
+		configPatchPath = "/opt/vasystem/patch"
+	}
+
+	filenameWithSuffix := path.Base(filename)
+	ext := path.Ext(filenameWithSuffix)
+
+	zipFilePath := configPatchPath + "/"+identifier+ext
+	if util.Exists(zipFilePath) {
+		strMd5, e := util.FileMd5(zipFilePath)
+		if e !=nil || strMd5 == "" {
+			return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触")
+		}
+		if strMd5 == identifier {
+			if !installSdk(identifier, ext) {
+				return false,errors.New("鎵ц瀹夎杩囩▼寮傚父,璇风‘瀹氫笂浼犵殑瀹夎鍖呮槸tar.gz鏍煎紡")
+			}
+			return true,nil
+
+		} else {
+			logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
+			return false,errors.New("鏍¢獙瀹夎鏂囦欢澶辫触")
+		}
+	} else {
+		return false,errors.New("瀹夎鍖呭凡涓㈠け锛岃閲嶆柊涓婁紶")
+	}
+}
+
+//瀹夎sdk
+func installSdk(identifier string, ext string) bool {
+	configPatchPath := ""
+	if config.Server.PatchPath != "" {
+		configPatchPath = config.Server.PatchPath
+	} else {
+		configPatchPath = "/opt/vasystem/patch"
+	}
+	//1.瑙e帇缂╂洿鏂板寘
+	unPackPath := configPatchPath+"/"+identifier+"_basic/"
+	if util.Exists(unPackPath) {
+		//姝ょ増鏈凡缁忔洿鏂拌繃
+		rmErr := os.RemoveAll(unPackPath)
+		if rmErr !=nil {
+			return false
+		}
+	}
+	if !util.CreateDirectory(unPackPath) {
+		return false
+	}
+
+	unPackFilePath := configPatchPath+"/"+identifier+ext
+	err := util.UnTarGz(unPackFilePath, unPackPath)
+	if err !=nil {
+		logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
+		return false
+	}
+	//瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁�
+	//1.瑙f瀽瀹夎璇存槑ins.inc
+	incPath := unPackPath+"ins.inc"
+	ret := false
+	if util.Exists(incPath) {
+		if incB, err := ioutil.ReadFile(incPath);err == nil {
+			var ins InsInc
+			if err = json.Unmarshal(incB, &ins); err == nil {
+				//澶勭悊鎺堟潈淇℃伅
+
+				//2.瑙f瀽sdk.def,灏唖o鍜寊conf澶嶅埗鍒�/opt/vasystem/libs/鏂囦欢澶逛笅
+				defPath := unPackPath + "sdk.def"
+				if util.Exists(defPath) {
+					if defB, err := ioutil.ReadFile(defPath); err == nil {
+						//3.灏嗙畻娉晄o銆佷緷璧栨枃浠躲�亃conf銆�
+						soM := make(map[string]SdkDef)
+						var skDefArr []SdkDef
+						if err = json.Unmarshal(defB, &skDefArr);err == nil {
+							var sdkApi dbapi.SdkApi
+							for _,skd := range skDefArr {
+								if _,ok := soM[skd.Def.SdkType];!ok {
+									soM[skd.Def.SdkType] = skd
+								}
+								srv := SdkRegisterVo{
+									Id: skd.Def.Id,
+									SdkType: skd.Def.SdkType,
+									SdkName: skd.Def.SdkName,
+									Icon: skd.Def.Icon,
+									Url: skd.Def.Url,
+								}
+								for _,ag := range skd.Args {
+									sra := SdkRegisterArgVo{
+										Scope: ag.Scope,
+									}
+									sra.SdkArg = SdkArg{
+										Alias: ag.Alias,
+										Name: ag.Name,
+										Type: ag.Type,
+										ArgType: ag.ArgType,
+										Must: ag.Must,
+										Unit: ag.Unit,
+										Range: ag.Range,
+										DefaultValue: ag.DefaultValue,
+										DefaultOperator: ag.DefaultOperator,
+										Sort: ag.Sort,
+									}
+									srv.Args = append(srv.Args,  sra)
+								}
+								paramBody := util.Struct2Map(srv)
+								sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
+							}
+
+							zconfPath := "/opt/vasystem/bin/zconf/"
+							libPath := "/opt/vasystem/libs/"
+							if !util.DirExists(zconfPath) {
+								os.MkdirAll(zconfPath, 0777)
+							}
+							if !util.DirExists(libPath) {
+								os.MkdirAll(libPath, 0777)
+							}
+							for sdkType,_ := range soM {
+								//澶嶅埗json鍚姩鏂囦欢
+								util.CopyFile(unPackPath+sdkType+"/"+sdkType+".json", zconfPath)
+								util.CopyDir(unPackPath+sdkType+"/"+sdkType, libPath)
+								if util.DirExists(unPackPath+sdkType+"models") {
+									util.CopyDir(unPackPath+sdkType+"models", "/opt/vasystem/bin/")
+								}
+							}
+							ret = true
+						}
+					}
+				}
+			}
+		}
+	}
+
+	return ret
+}
+
+
+type InsInc struct {
+	OrderId 			string 		`json:"orderId"`
+	ProductId 			string 		`json:"productId"`
+	ActivateCode 		string 		`json:"activateCode"`
+	MachineCode 		string 		`json:"machineCode"`
+	UserId 				string 		`json:"userId"`
+	SdkIds 				[]string 	`json:"sdkIds"`
+	ModuleIds 			[]string 	`json:"moduleIds"`
+	ChCount 			int 		`json:"chCount"`
+	AuthCount 			int 		`json:"authCount"`
+	ServeYear			int 		`json:"serveYear"`
+}
+
+//绠楁硶鍜屽弬鏁板畾涔�
+type SdkDef struct {
+	Def  Sdk 			`json:"def"`
+	Args []SdkArgEntity `json:"args"`
+}
+
+type Sdk struct {
+	Id 				string 		`gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"`
+	IpcId   		string 		`gorm:"column:ipc_id" json:"ipc_id"`
+	SdkType 		string 		`gorm:"column:sdk_type" json:"sdk_type"`
+	SdkName 		string 		`gorm:"column:sdk_name" json:"sdk_name" example:"浜鸿劯妫�娴�"`
+	Icon    		string 		`gorm:"column:icon" json:"icon,omitempty"`
+	Url     		string 		`gorm:"column:url" json:"url,omitempty" example:"http://ip:port/govideo/sdk/1"`
+	CreateTime 		string 		`gorm:"column:create_time" json:"create_time"`
+	CreateBy 		string 		`gorm:"column:create_by" json:"create_by"`
+	UpdateTime 		string 		`gorm:"column:update_time" json:"update_time"`
+	Enable 			bool 		`gorm:"column:enable;default:1" json:"enable"`
+	DelFlag 		int 		`gorm:"column:del_flag;default:0" json:"del_flag"`
+	Env 			string 		`gorm:"column:env" json:"env"` //杩愯鐜鍙婄粨鏋滆鏄庯紝json鏍煎紡锛屽寘鍚玸o_file_path,runtime,param,depends(cuda鐗堟湰锛宱pencv鐗堟湰锛宼ensorflow鐗堟湰绛�)
+}
+
+type SdkArgEntity struct {
+	Id 				string 		`gorm:"primary_key;column:id" json:"id"`
+	SdkId 			string 		`gorm:"column:sdk_id" json:"sdk_id"`
+	Scope 			string 		`gorm:"column:scope" json:"scope"`
+	SdkArg
+}
+
+type SdkArg struct {
+	Alias   		string 		`gorm:"column:alias" json:"alias"`   //鍙傛暟鐨勫埆鍚�
+	Name  			string 		`gorm:"column:name" json:"name"`  //鍙傛暟鍚嶇О
+	Type  			string 		`gorm:"column:type" json:"type"`  //鍙傛暟绫诲瀷(鏁存暟锛屽瓧绗︿覆鎴栨暟缁�)
+	ArgType 		string 		`gorm:"column:arg_type" json:"arg_type"`
+	Must  			bool   		`gorm:"column:must" json:"must"`  //鏄惁蹇呭~
+	Unit 			string 		`gorm:"column:unit" json:"unit"` //鍗曚綅
+	Range 			string 		`gorm:"column:range" json:"range"` //鍊肩殑鑼冨洿锛宔g锛�0,100琛ㄧず浠�0鍒�100
+	DefaultValue 	string 		`gorm:"column:default_value" json:"default_value"`
+	DefaultOperator string 		`gorm:"column:default_operator" json:"default_operator"`
+	Sort  			int    		`gorm:"column:sort;default:1" json:"sort"`
+}
+
+
+type SdkRegisterVo struct {
+	Id 		string `json:"id"`
+	SdkType string `json:"sdk_type"`//浜鸿劯妫�娴嬶細FaceDetect,浜鸿劯鎻愬彇锛欶aceExtract,浜鸿劯姣斿锛欶aceCompare,琛屼负锛歒olo
+	SdkName string `json:"sdk_name"`    //绠楁硶鍚嶇О
+	Args    []SdkRegisterArgVo `json:"args"` //绠楁硶鍙傛暟
+	Icon    string `json:"icon"`       //绠楁硶鍥炬爣
+	Url     string `json:"url"`                       //绠楁硶涓嬭浇鍦板潃
+}
+
+type SdkRegisterArgVo struct {
+	Scope string `json:"scope"`
+	SdkArg
+
+	Dics []SdkArgDic `json:"dics"` //濡傛灉姝ょ畻娉曞弬鏁版槸琚�夐」锛岄渶瑕佸皢鍙傛暟鏋氫妇鍊煎啓鍏ュ埌瀛楀吀琛ㄤ腑
+}
+
+type SdkArgDic struct {
+	Value string `json:"value"`
+	Name string `json:"name"`
+	Sort int `json:"sort"`
+}
\ No newline at end of file

--
Gitblit v1.8.0