package controller
|
|
import (
|
"basic.com/valib/logger.git"
|
"fmt"
|
"github.com/gin-gonic/gin"
|
"io/ioutil"
|
"net/http"
|
"os"
|
"path/filepath"
|
"vamicro/update-service/models"
|
"vamicro/update-service/upgrade"
|
"vamicro/update-service/utils"
|
)
|
|
func UploadFiles(c *gin.Context) {
|
file, err := c.FormFile("upload")
|
if nil != err {
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": err.Error(),
|
"data": &struct {
|
}{},
|
})
|
logger.Error("UploadFiles upload field is not filled with a file, err:", err.Error())
|
return
|
}
|
|
if err = c.SaveUploadedFile(file, file.Filename); err != nil {
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": err.Error(),
|
"data": &struct {
|
}{},
|
})
|
logger.Error("UploadFiles save file failed, err:", err.Error())
|
return
|
}
|
|
c.JSON(http.StatusOK, gin.H {
|
"code": http.StatusOK,
|
"msg": "ok",
|
"data": &struct {
|
}{},
|
})
|
}
|
|
func Download(c *gin.Context) {
|
path := c.Query("filename")
|
if len(path) == 0 || !utils.PathExists(path) {
|
msg := fmt.Sprintf("%s not exists", path)
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": msg,
|
"data": &struct {
|
}{},
|
})
|
logger.Error("UploadApps upload field is not filled with a file, err:", msg)
|
return
|
}
|
|
filename := filepath.Base(path)
|
c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
c.Writer.Header().Add("Content-Type", "application/octet-stream")
|
|
c.File(path)
|
}
|
|
//根据platform获取zip包的地址,以及所有二进制文件的属性(md5,版本号,commit sha1,buildTime)
|
func GetAppVersion(c *gin.Context) {
|
arch := c.Query("arch")
|
if !models.IsValidPlatform(arch) {
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": "platform is unspecified or invalid",
|
"data": &struct {
|
}{},
|
})
|
return
|
}
|
|
programs, err := models.FindAllPrograms(arch)
|
if err != nil {
|
c.JSON(http.StatusOK, gin.H {
|
"code": http.StatusInternalServerError,
|
"msg": err.Error(),
|
"data": &struct {
|
}{},
|
})
|
}
|
|
var dist models.DistFile
|
files, err := dist.FindAll(arch)
|
if err != nil {
|
c.JSON(http.StatusOK, gin.H {
|
"code": http.StatusInternalServerError,
|
"msg": err.Error(),
|
"data": &struct {
|
}{},
|
})
|
}
|
if len(files) < 1 {
|
c.JSON(http.StatusOK, gin.H {
|
"code": http.StatusInternalServerError,
|
"msg": "no version found",
|
"data": &struct {
|
}{},
|
})
|
}
|
|
c.JSON(http.StatusOK, gin.H {
|
"code": http.StatusOK,
|
"msg": "ok",
|
"data": map[string]interface{}{
|
"archive":files[0].Path,
|
"programs":programs,
|
"version":files[0].Version,
|
"intro":files[0].Intro,
|
},
|
})
|
}
|
|
//上传执行make命令生成的zip程序包(包含所有的二进制可执行文件)
|
func UploadApps(c *gin.Context) {
|
arch := c.Query("arch")
|
version := c.Query("version")
|
inrto := c.Query("intro")
|
if !models.IsValidPlatform(arch) {
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": "platform is unspecified or invalid",
|
"data": []*models.Program{},
|
})
|
return
|
}
|
|
file, err := c.FormFile("archive")
|
if nil != err {
|
c.JSON(http.StatusBadRequest, gin.H {
|
"code": http.StatusBadRequest,
|
"msg": err.Error(),
|
"data": []*models.Program{},
|
})
|
logger.Error("UploadApps upload field is not filled with a file, err:", err.Error())
|
return
|
}
|
|
tmpFile, err := ioutil.TempFile("", "dist-*.zip")
|
if nil != err {
|
c.JSON(http.StatusInternalServerError, gin.H {
|
"code": http.StatusInternalServerError,
|
"msg": err.Error(),
|
"data": []*models.Program{},
|
})
|
logger.Error("UploadApps create temp file failed, err:", err.Error())
|
return
|
}
|
defer func() {
|
tmpFile.Close()
|
os.Remove(tmpFile.Name())
|
}()
|
|
if err = c.SaveUploadedFile(file, tmpFile.Name()); err != nil {
|
c.JSON(http.StatusInternalServerError, gin.H {
|
"code": http.StatusInternalServerError,
|
"msg": err.Error(),
|
"data": []*models.Program{},
|
})
|
logger.Error("UploadApps save file failed, err:", err.Error())
|
return
|
}
|
|
logger.Info("UploadApps saved to temp file ok:", tmpFile.Name())
|
|
ch := make(chan *upgrade.UpgradeReply)
|
defer close(ch)
|
msg := &upgrade.UpgradePara{
|
Platform: arch,
|
ArchiveFile: tmpFile,
|
ReplyCh: ch,
|
Version: version,
|
Intro: inrto,
|
}
|
upgrade.PUshMsg(msg)
|
reply := <- ch
|
|
logger.Info("UploadApps get response from routing:", tmpFile.Name())
|
|
c.JSON(reply.Status, reply.Body)
|
}
|