sunty
2020-08-20 9303b69ea569bcb5e581147543a3fd58e90d0d25
controllers/sdk.go
@@ -1,9 +1,15 @@
package controllers
import (
   "basic.com/dbapi.git"
   "net/http"
   "strconv"
   "webserver/extend/code"
   "webserver/extend/util"
   "webserver/middlewares/auth"
   "webserver/service"
   "basic.com/dbapi.git"
   "basic.com/valib/logger.git"
   "github.com/gin-gonic/gin"
)
@@ -11,31 +17,35 @@
}
type SdkVo struct {
   Id       string `json:"id"`
   SdkType string `json:"sdk_type"`//人脸检测:FaceDetect,人脸提取:FaceExtract,人脸比对:FaceCompare,行为:Yolo
   SdkName string `json:"sdk_name"`    //算法名称
   Args    []SdkArgVo `json:"args"` //算法参数
   Icon    string `json:"icon"`       //算法图标
   Url     string `json:"url"`                       //算法下载地址
   CreateTime string `json:"create_time"`
   CreateBy string `json:"create_by"`
   UpdateTime string `json:"update_time"`
   Enable bool `json:"enable"`//是否启用
   DelFlag bool `json:"del_flag"`//逻辑删除
   Id         string     `json:"id"`
   SdkType    string     `json:"sdk_type"` //人脸检测:FaceDetect,人脸提取:FaceExtract,人脸比对:FaceCompare,行为:Yolo
   SdkName    string     `json:"sdk_name"` //算法名称
   Args       []SdkArgVo `json:"args"`     //算法参数
   Icon       string     `json:"icon"`     //算法图标
   Url        string     `json:"url"`      //算法下载地址
   CreateTime string     `json:"create_time"`
   CreateBy   string     `json:"create_by"`
   UpdateTime string     `json:"update_time"`
   Enable     bool       `json:"enable"`   //是否启用
   DelFlag    bool       `json:"del_flag"` //逻辑删除
}
//算法参数定义
type SdkArgVo struct {
   Alias   string `json:"alias"`   //参数的别名
   Name  string `json:"name"`  //参数名称
   Type  string `json:"type"`  //参数类型(整数,字符串或数组)
   Must  bool   `json:"must"`  //是否必填
   Range string `json:"range"` //值的范围,eg:0,100表示从0到100
   Sort  int    `json:"sort"`  //参数顺序
   Alias        string `json:"alias"` //参数的别名
   Name         string `json:"name"`  //参数名称
   Type         string `json:"type"`  //参数类型(整数,字符串或数组)
   Must         bool   `json:"must"`  //是否必填
   Unit         string `json:"unit"`
   Range        string `json:"range"` //值的范围,eg:0,100表示从0到100
   DefaultValue string `json:"default_value"`
   Sort         int    `json:"sort"` //参数顺序
}
// @Security ApiKeyAuth
// @Summary 算法保存
// @Description 算法保存
// @Accept json
// @Produce json
// @Tags sdk
// @Param reqMap body controllers.SdkVo true "人脸检测"
@@ -46,19 +56,20 @@
   var sdk SdkVo
   var api dbapi.SdkApi
   if err := c.BindJSON(&sdk);err !=nil {
      util.ResponseFormat(c,code.RequestParamError,"参数错误")
   if err := c.BindJSON(&sdk); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数错误")
      return
   }
   paramBody := util.Struct2Map(sdk)
   flag, data := api.Save(paramBody)
   if flag{
   flag, data := api.Register(paramBody)
   if flag {
      util.ResponseFormat(c, code.Success, data)
   } else {
      util.ResponseFormat(c, code.ComError, data)
   }
}
// @Security ApiKeyAuth
// @Summary 查找所有算法
// @Description 查找所有算法
// @Produce json
@@ -68,13 +79,15 @@
// @Failure 500 {string} json "{"code":500, msg:"请求失败",data:"[]",success:false}"
// @Router /data/api-v/sdk/findAllSdk [GET]
func (sc SdkController) FindAllSdk(c *gin.Context) {
   var api dbapi.SdkApi
   sdkName := c.Query("sdkName")
   sdks := api.FindAll(sdkName)
   logger.Debug("FindAllSdk sdkName:", sdkName)
   sdks := service.GetSdkList("") //本机已安装的算法
   util.ResponseFormat(c, code.Success, sdks)
}
// @Security ApiKeyAuth
// @Summary 根据id获取算法信息
// @Description 根据id获取算法信息
// @Produce json
@@ -86,17 +99,17 @@
func (sc SdkController) GetById(c *gin.Context) {
   var api dbapi.SdkApi
   sdkId := c.Query("id")
   if sdkId == ""{
      util.ResponseFormat(c,code.RequestParamError,"sdkId不能为空")
   if sdkId == "" {
      util.ResponseFormat(c, code.RequestParamError, "sdkId不能为空")
      return
   }
   flag, sdk := api.GetById(sdkId)
   if flag {
      util.ResponseFormat(c,code.Success,sdk)
      util.ResponseFormat(c, code.Success, sdk)
   } else {
      util.ResponseFormat(c,code.ComError,sdk)
      util.ResponseFormat(c, code.ComError, sdk)
   }
}
@@ -104,19 +117,20 @@
func (sc SdkController) GetSdkArgs(c *gin.Context) {
   sdkId := c.Query("sdkId")
   scope := c.Query("scope")
   if sdkId== "" || scope == ""{
      util.ResponseFormat(c,code.RequestParamError,"参数有误")
   if sdkId == "" || scope == "" {
      util.ResponseFormat(c, code.RequestParamError, "参数有误")
      return
   }
   var api dbapi.SdkApi
   b,d := api.GetSdkArgs(sdkId, scope)
   if b{
      util.ResponseFormat(c,code.Success,d)
   b, d := api.GetSdkArgs(sdkId, scope)
   if b {
      util.ResponseFormat(c, code.Success, d)
   } else {
      util.ResponseFormat(c,code.ComError,"查询失败")
      util.ResponseFormat(c, code.ComError, "查询失败")
   }
}
// @Security ApiKeyAuth
// @Summary 根据taskId获取算法信息
// @Description 根据taskId获取算法信息
// @Produce json
@@ -129,13 +143,156 @@
   var api dbapi.SdkApi
   taskId := c.Query("taskId")
   if taskId == "" {
      util.ResponseFormat(c,code.ComError,"任务id不能为空")
      util.ResponseFormat(c, code.ComError, "任务id不能为空")
      return
   }
   flag,sdks := api.FindByTaskId(taskId)
   flag, sdks := api.FindByTaskId(taskId)
   if flag {
      util.ResponseFormat(c,code.Success,sdks)
      util.ResponseFormat(c, code.Success, sdks)
   } else {
      util.ResponseFormat(c,code.ComError,sdks)
      util.ResponseFormat(c, code.ComError, sdks)
   }
}
}
// @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, "")
   }
}
// @Security ApiKeyAuth
// @Summary 从商城下载或升级算法
// @Description 从商城下载或升级算法
// @Produce json
// @Tags sysset
// @Param path query string true "算法id"
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /data/api-v/sdk/sdkDownload [get]
func (sc SdkController) SdkDownLoad(c *gin.Context) {
   sdkId, exist := c.GetQuery("path")
   if !exist {
      util.ResponseFormat(c, code.RequestParamError, "下载的算法参数有误")
      return
   }
   if b,err := service.DownloadOrUpgrade(sdkId);b {
      util.ResponseFormat(c, code.Success, "算法安装成功")
   } else {
      util.ResponseFormat(c, &code.Code{http.StatusBadRequest, false, err.Error()}, err.Error())
   }
}