From 63873fc1fea408c201cef5cfedb3a9bdaeab5b4d Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期一, 16 十二月 2019 17:05:21 +0800
Subject: [PATCH] add patchUpdate
---
controllers/syssetcont.go | 114 ++++++++++++++
config/test.yaml | 1
config/dev.yaml | 13 +
updatePatch.sh | 27 +++
extend/util/util.go | 89 +++++++++++
service/SysService.go | 201 +++++++++++++++++++++++++
config/pro.yaml | 13 +
extend/config/config.go | 1
router/router.go | 3
9 files changed, 462 insertions(+), 0 deletions(-)
diff --git a/config/dev.yaml b/config/dev.yaml
index a4e7f65..f2a8878 100644
--- a/config/dev.yaml
+++ b/config/dev.yaml
@@ -19,6 +19,19 @@
channelCount: 0
#纭洏涓暟
diskCount: 2
+ #Exec root command
+ sudoPassword: 123
+ sysThresholds:
+ - value: 60
+ color: '#13ce66'
+ - value: 80
+ color: '#FF9C4A'
+ - value: 95
+ color: '#f53d3d'
+ - value: 100
+ color: '#5d0000'
+ ptzSpeed: 50
+ patchPath: /opt/vasystem/patch
database:
driver: sqlite
name: sqlite3
diff --git a/config/pro.yaml b/config/pro.yaml
index f3882e1..e71329e 100644
--- a/config/pro.yaml
+++ b/config/pro.yaml
@@ -19,6 +19,19 @@
channelCount: 0
#纭洏涓暟
diskCount: 2
+ #Exec root command
+ sudoPassword: 123
+ sysThresholds:
+ - value: 60
+ color: '#13ce66'
+ - value: 80
+ color: '#FF9C4A'
+ - value: 95
+ color: '#f53d3d'
+ - value: 100
+ color: '#5d0000'
+ ptzSpeed: 50
+ patchPath: /opt/vasystem/patch
database:
driver: sqlite
name: sqlite3
diff --git a/config/test.yaml b/config/test.yaml
index 655231c..90ffccc 100644
--- a/config/test.yaml
+++ b/config/test.yaml
@@ -35,6 +35,7 @@
value: 100
color: '#5d0000'
ptzSpeed: 50
+ patchPath: /opt/vasystem/patch
database:
driver: sqlite
name: sqlite3
diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go
index 9275310..56199d5 100644
--- a/controllers/syssetcont.go
+++ b/controllers/syssetcont.go
@@ -1,11 +1,14 @@
package controllers
import (
+ "net/http"
"regexp"
+ "strconv"
"webserver/extend/code"
"webserver/extend/config"
"webserver/extend/sys"
"webserver/extend/util"
+ "webserver/middlewares/auth"
"basic.com/dbapi.git"
"github.com/gin-gonic/gin"
@@ -429,3 +432,114 @@
util.ResponseFormat(c, code.Success, "閰嶇疆鎴愬姛")
}
+
+// @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 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/sysset/patchUpdate [get]
+func (sset SysSetController) PatchUpdateCheck(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/sysset/patchUpdate [post]
+func (sset SysSetController) PatchUpdate(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 := sv.PatchUpload(&arg); b {
+ util.ResponseFormat(c, code.Success, "")
+ } else {
+ util.ResponseFormat(c, code.ComError, "")
+ }
+}
\ No newline at end of file
diff --git a/extend/config/config.go b/extend/config/config.go
index 32e4f13..41cb8bb 100644
--- a/extend/config/config.go
+++ b/extend/config/config.go
@@ -29,6 +29,7 @@
SudoPassword string `mapstructure: "sudoPassword"` //绯荤粺瀵嗙爜
SysThresholds []threshold `mapstructure: "sysThresholds"`
PTZSpeed int `mapstructure: "ptzSpeed"` // 浜戝彴绉诲姩閫熷害
+ PatchPath string `mapstructure:"patchPath"`//绯荤粺鏇存柊鍖呰矾寰�
}
var Server = &server{}
diff --git a/extend/util/util.go b/extend/util/util.go
index d5180d0..845ff42 100644
--- a/extend/util/util.go
+++ b/extend/util/util.go
@@ -1,12 +1,17 @@
package util
import (
+ "archive/zip"
"bytes"
"encoding/json"
"image"
+ "io"
"io/ioutil"
"net/http"
+ "os"
+ "path/filepath"
"strconv"
+ "time"
"webserver/extend/code"
"basic.com/pubsub/protomsg.git"
@@ -208,3 +213,87 @@
out = out[:n] // compressed data
return out, nil
}
+
+// 鍒ゆ柇鎵�缁欒矾寰勬枃浠�/鏂囦欢澶规槸鍚﹀瓨鍦�
+func Exists(path string) bool {
+ if path == "" {
+ return false
+ }
+ _, err := os.Stat(path) //os.Stat鑾峰彇鏂囦欢淇℃伅
+ if err != nil {
+ if os.IsExist(err) {
+ return true
+ }
+ return false
+ }
+ return true
+}
+func CreateDirectory(path string) bool {
+ if path == "" {
+ return false
+ }
+ if Exists(path) {
+ return true
+ }
+ err := os.MkdirAll(path,os.ModePerm)
+ if err !=nil {
+ return false
+ }
+ return true
+}
+
+//鏃ユ湡杞瓧绗︿覆
+func FormatDate(date time.Time, layout string) string {
+ layout = strings.Replace(layout, "yyyy", "2006", 1)
+ layout = strings.Replace(layout, "yy", "06", 1)
+ layout = strings.Replace(layout, "MM", "01", 1)
+ layout = strings.Replace(layout, "dd", "02", 1)
+ layout = strings.Replace(layout, "HH", "15", 1)
+ layout = strings.Replace(layout, "mm", "04", 1)
+ layout = strings.Replace(layout, "ss", "05", 1)
+ layout = strings.Replace(layout, "SSS", "000", -1)
+ return date.Format(layout)
+}
+func ZipCheck(zipPath string) bool {
+ f, err := os.Open(zipPath)
+ if err != nil {
+ return false
+ }
+ defer f.Close()
+ buf := make([]byte, 4)
+ if n, err := f.Read(buf); err != nil || n < 4 {
+ return false
+ }
+ return bytes.Equal(buf, []byte("PK\x03\x04"))
+}
+func UnZip(archive, target string) error {
+ reader, err := zip.OpenReader(archive)
+ if err != nil {
+ return err
+ }
+ if err := os.MkdirAll(target, 0755); err != nil {
+ return err
+ }
+ for _, file := range reader.File {
+ path := filepath.Join(target, file.Name)
+ if file.FileInfo().IsDir() {
+ os.MkdirAll(path, file.Mode())
+ continue
+ }
+ fileReader, err := file.Open()
+ if err != nil {
+ return err
+ }
+ defer fileReader.Close()
+ targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
+ if err != nil {
+ return err
+ }
+ defer targetFile.Close()
+ if _, err := io.Copy(targetFile, fileReader); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
\ No newline at end of file
diff --git a/router/router.go b/router/router.go
index bb66024..0d0d123 100644
--- a/router/router.go
+++ b/router/router.go
@@ -230,6 +230,9 @@
vsset.GET("/reboot", ssController.RebootOS)
vsset.GET("/rebootTask", ssController.GetRebootTask)
vsset.POST("/rebootTask", ssController.SetRebootTask)
+
+ vsset.GET("/patchUpdate", ssController.PatchUpdateCheck)
+ vsset.POST("/patchUpdate", ssController.PatchUpdate)
}
//绠楁硶搴撴搷浣�
diff --git a/service/SysService.go b/service/SysService.go
new file mode 100644
index 0000000..aff3d33
--- /dev/null
+++ b/service/SysService.go
@@ -0,0 +1,201 @@
+package service
+import (
+ "basic.com/valib/logger.git"
+ "bufio"
+ "fmt"
+ "io/ioutil"
+ "mime/multipart"
+ "os"
+ "os/exec"
+ "strconv"
+ "strings"
+ "webserver/extend/config"
+ "webserver/extend/util"
+)
+
+type SysService struct {
+
+}
+
+type FileChunkCheckVo struct {
+ UserId string
+ ChunkNumber int //褰撳墠鍒嗙墖涓嬫爣,浠�1寮�濮�
+ ChunkSize int //姣忎竴鍧楃殑澶у皬
+ CurrentChunkSize int //褰撳墠鍒嗗潡鐨勫ぇ灏�
+ FileName string //鏂囦欢鍚嶇О
+ Identifier string //鏁翠釜鏂囦欢鍞竴鏍囪瘑,md5
+ RelativePath string //鏂囦欢瀹㈡埛绔矾寰�
+ TotalSize int64 //鏂囦欢鎬诲ぇ灏�
+ TotalChunks int //鎬诲垎鐗囨暟閲�
+}
+
+type FileUploadVo struct {
+ Id string
+ UserId string
+ ChunkNumber int //褰撳墠鍒嗙墖涓嬫爣,浠�1寮�濮�
+ ChunkSize int //姣忎竴鍧楃殑澶у皬
+ CurrentChunkSize int //褰撳墠鍒嗗潡鐨勫ぇ灏�
+ FileName string //鏂囦欢鍚嶇О
+ Identifier string //鏁翠釜鏂囦欢鍞竴鏍囪瘑,md5
+ RelativePath string //鏂囦欢瀹㈡埛绔矾寰�
+ TotalSize int64 //鏂囦欢鎬诲ぇ灏�
+ TotalChunks int //鎬诲垎鐗囨暟閲�
+ File *multipart.File //褰撳墠鍒嗙墖鐨勬枃浠跺唴瀹�
+ Header *multipart.FileHeader
+}
+
+func (sv SysService) CheckUpdateFile(arg *FileChunkCheckVo) bool {
+ configPatchPath := ""
+ if config.Server.PatchPath != "" {
+ configPatchPath = config.Server.PatchPath
+ } else {
+ configPatchPath = "/opt/vasystem/patch"
+ }
+ fileTmpPath := configPatchPath + "/"+arg.Identifier
+ if !util.Exists(fileTmpPath) {
+ return false
+ }
+ //鍒ゆ柇鍚堟垚鐨勬枃浠舵槸鍚﹀瓨鍦�
+ index := strings.LastIndex(arg.FileName, ".")
+ subfix := ""
+ if index >-1 {//鏈夊悗缂�
+ subfix = arg.FileName[index:]
+ }
+ mergedFilePath := fileTmpPath+subfix
+ if util.Exists(mergedFilePath) {
+ return true
+ }
+ //鍒ゆ柇鍒嗗潡鏂囦欢鏄惁瀛樺湪
+ chunkFilePath := fileTmpPath+"/"+arg.Identifier+"_"+strconv.Itoa(arg.ChunkNumber)
+ if !util.Exists(chunkFilePath) {
+ return false
+ }
+ if arg.ChunkNumber == arg.TotalChunks {
+ dirFiles, _ := ioutil.ReadDir(fileTmpPath)
+ if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
+ //琛ㄧず鎵�鏈夊垎鍧楅兘涓婁紶浜嗭紝闇�瑕乵erge
+ if sv.MergeChunks(fileTmpPath, mergedFilePath) {
+ if util.ZipCheck(mergedFilePath) {
+ if !updatePatch(arg.Identifier, subfix) {
+ return false
+ }
+ }
+ }
+ }
+ }
+ return true
+}
+
+func (sv SysService) PatchUpload(arg *FileUploadVo) bool {
+ configPatchPath := ""
+ if config.Server.PatchPath != "" {
+ configPatchPath = config.Server.PatchPath
+ } else {
+ configPatchPath = "/opt/vasystem/patch"
+ }
+ defer (*arg.File).Close()
+ if !util.CreateDirectory(configPatchPath) {
+ return false
+ }
+ index := strings.LastIndex(arg.Header.Filename, ".")
+ if index < 0 {
+ return false
+ }
+ subfix := arg.Header.Filename[index:]
+ MD5Str := arg.Identifier
+ logger.Debug("Identifier:",MD5Str)
+ fileTmpPath := configPatchPath + "/"+MD5Str
+ if !util.Exists(fileTmpPath) {
+ if !util.CreateDirectory(fileTmpPath) {
+ return false
+ }
+ }
+ fileSavePath := fileTmpPath+"/"+MD5Str+"_"+strconv.Itoa(arg.ChunkNumber)
+ if util.Exists(fileSavePath) {
+ rmErr := os.Remove(fileSavePath)
+ if rmErr != nil {
+ logger.Debug("rmErr:",rmErr)
+ return false
+ }
+ }
+ file, e := os.Create(fileSavePath)
+ if e !=nil {
+ logger.Debug("os.Create err:",e,"fileSavePath:",fileSavePath)
+ return false
+ }
+ defer file.Close()
+ writer := bufio.NewWriter(file)
+ chunkData := make([]byte, arg.Header.Size)
+ n, err := (*arg.File).ReadAt(chunkData, 0)
+ if n ==0 || err !=nil {
+ logger.Debug("read chunkData err:",err,"n:",n)
+ return false
+ }
+ nn, err2 := writer.Write(chunkData)
+ if nn ==0 || err2 !=nil {
+ logger.Debug("write chunkData err:",err2,"nn:",nn)
+ return false
+ }
+ isComplete := false
+ dirFiles, _ := ioutil.ReadDir(fileTmpPath)
+ if dirFiles != nil && len(dirFiles) == arg.TotalChunks {
+ isComplete = true
+ }
+ if isComplete {
+ if sv.MergeChunks(fileTmpPath,fileTmpPath + subfix) {
+ logger.Debug("merge all chunks success,identifier:",MD5Str,"fileName:",arg.FileName)
+ if util.ZipCheck(fileTmpPath + subfix) {
+ if !updatePatch(arg.Identifier, subfix) {
+ return false
+ }
+ }
+ }
+ }
+ return true
+}
+
+//鏇存柊绯荤粺绋嬪簭
+func updatePatch(identifier string, ext string) bool {
+ configPatchPath := ""
+ if config.Server.PatchPath != "" {
+ configPatchPath = config.Server.PatchPath
+ } else {
+ configPatchPath = "/opt/vasystem/patch"
+ }
+ //1.瑙e帇缂╂洿鏂板寘
+ unZipPath := configPatchPath+"/"+identifier+"_basic/"
+ if util.Exists(unZipPath) {
+ //姝ょ増鏈凡缁忔洿鏂拌繃
+ return true
+ } else {
+ if !util.CreateDirectory(unZipPath) {
+ return false
+ }
+ }
+ err := util.UnZip(configPatchPath+"/"+identifier+ext, unZipPath)
+ if err !=nil {
+ logger.Debug("UnZip err:",err,"zipFile:",configPatchPath+"/"+identifier+ext)
+ return false
+ }
+ //2.鏇存柊绯荤粺
+ var cmd *exec.Cmd
+ cmd = exec.Command("/bin/sh","-c",fmt.Sprintf("./updatePatch.sh %s",unZipPath))
+ logger.Debug("called sh updatePatch.sh ", unZipPath)
+ if b, err := cmd.Output(); err != nil {
+ logger.Debug("updatePatch err:",err,"result:",string(b))
+ return false
+ }
+ return true
+}
+
+func (sv SysService) MergeChunks(chunkPath string, storePath string) bool {
+ var cmd *exec.Cmd
+ cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("./mergeAll.sh %s %s", chunkPath, storePath))
+ logger.Debug("called sh mergeAll.sh ", chunkPath, storePath)
+ if b, err := cmd.Output(); err != nil {
+ logger.Debug("mergeChunks err:", err, "result:", string(b))
+ return false
+ } else {
+ return true
+ }
+}
\ No newline at end of file
diff --git a/updatePatch.sh b/updatePatch.sh
new file mode 100644
index 0000000..491934a
--- /dev/null
+++ b/updatePatch.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+cd /opt/vasystem/
+#stop
+./kill.sh
+sleep 3
+patchPath=$1
+files=$(ls $patchPath)
+echo "patchPath: " $patchPath
+if [ ! -f $patchPath ];then
+ echo "$patchPath not exist"
+else
+ for item in $files
+ do
+ if [ -f $item ];then
+ cp $patchPath/$item /opt/vasystem/bin
+ else
+ echo "directory name is ${item}"
+ if [[ "$item"=="dist" ]];then
+ echo "update web dist"
+ cp -r $patchPath/$item /opt/vasystem/web/dist
+ fi
+ fi
+ done
+fi
+./daemon.sh -shm
+sleep 3
+echo "patch update success"
\ No newline at end of file
--
Gitblit v1.8.0