sunty
2020-08-20 9d88c7c467f8d93af4aab9ba0b6d6c01c2ffc546
extend/util/util.go
@@ -1,22 +1,27 @@
package util
import (
   "basic.com/pubsub/protomsg.git"
   "archive/zip"
   "basic.com/valib/logger.git"
   "bytes"
   "crypto/md5"
   "crypto/tls"
   "encoding/hex"
   "encoding/json"
   "errors"
   "github.com/gin-gonic/gin"
   "github.com/golang/glog"
   "github.com/pierrec/lz4"
   "gocv.io/x/gocv"
   "image"
   "io"
   "io/ioutil"
   "net"
   "os/exec"
   "net/http"
   "os"
   "path/filepath"
   "strconv"
   "time"
   "webserver/extend/code"
   "crypto/rand"
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/pierrec/lz4"
   "reflect"
   "strings"
)
@@ -36,7 +41,6 @@
// ResponseFormat 返回数据格式化
func ResponseFormat(c *gin.Context, respStatus *code.Code, data interface{}) {
   if respStatus == nil {
      glog.Error("response status param not found!")
      respStatus = code.RequestParamError
   }
   c.JSON(respStatus.Status, gin.H{
@@ -58,14 +62,14 @@
//struct转map
func Struct2Map(obj interface{}) map[string]interface{} {
   resultMap :=make(map[string]interface{},0)
   resultMap := make(map[string]interface{}, 0)
   bytesData, err := json.Marshal(obj)
   if err !=nil {
   if err != nil {
      return resultMap
   }
   json.Unmarshal(bytesData, &resultMap)
   return resultMap
   //
   //t := reflect.TypeOf(obj)
   //v := reflect.ValueOf(obj)
@@ -77,87 +81,17 @@
   //return data
}
// 按尺寸去切图
func SubImg(i protomsg.Image, x0, y0, x1, y1 int, ) []byte {
   img, _ := gocv.NewMatFromBytes(int(i.Height), int(i.Width), gocv.MatTypeCV8UC3, i.Data)
   rect := image.Rect(EnlargeSize(x0, y0, x1, y1, i))
   region := img.Region(rect)
   bytes, _ := gocv.IMEncode(".jpg", region)
   return bytes
}
// 长宽变为一比一,每边各扩百分之20
func EnlargeSize(x0, y0, x1, y1 int, i protomsg.Image) (x0_new, y0_new, x1_new, y1_new int) {
   // 先把长宽变为一比一
   chazhi := (y1 - y0) - (x1 - x0)
   x0 = x0 - chazhi/2
   if x0 < 0 {
      x0 = 0
   }
   x1 = x1 + chazhi/2
   if x1 > int(i.Width) {
      x1 = int(i.Width)
   }
   // 再把每边各扩大百分之20
   enlarge := float32(0.2)
   x0_new = int((1+enlarge)*float32(x0) - enlarge*float32(x1))
   if x0_new < 0 {
      x0_new = 0
   }
   x1_new = int((1+enlarge)*float32(x1) - enlarge*float32(x0))
   if x1_new > int(i.Width) {
      x1_new = int(i.Width)
   }
   y0_new = int((1+enlarge)*float32(y0) - enlarge*float32(y1))
   if y0_new < 0 {
      y0_new = 0
   }
   y1_new = int((1+enlarge)*float32(y1) - enlarge*float32(y0))
   if y1_new > int(i.Height) {
      y1_new = int(i.Height)
   }
   return
}
// 获取本机网卡IP
func GetLocalIP(networkName string) (ipv4 string,mask string, err error) {
   interfaces, err := net.Interfaces()
func ReadImgData(url string) ([]byte, error) {
   resp, err := http.Get(url)
   if err != nil {
      return "","", err
      return nil, err
   }
   for _, i := range interfaces {
      byName, err := net.InterfaceByName(i.Name)
      if err != nil {
         return "","", err
      }
      addresses, err := byName.Addrs()
      for _, v := range addresses {
         if ipnet, ok:=v.(*net.IPNet);ok && !ipnet.IP.IsLoopback(){
            if ipnet.IP.To4() !=nil{
               if byName.Name == networkName{
                  maskStr := ipnet.Mask.String()
                  mask64, _ := strconv.ParseUint(maskStr, 16, 32)
                  return ipnet.IP.String(),IpIntToString(int(mask64)),nil
               }
            }
         }
      }
   defer resp.Body.Close()
   pix, err := ioutil.ReadAll(resp.Body)
   if err != nil {
      return nil, err
   }
   return "","", errors.New("ipv4 not found")
}
func GetDefaultRoute(networkName string)(route string,err error){
   cmdStr := fmt.Sprintf("route | grep -P \"^default.*%s$\" | awk '{print $2}'",networkName)
   cmd := exec.Command("/bin/sh","-c",cmdStr)
   b, err := cmd.Output()
   if err!=nil{
      return "",err
   }
   return string(b),nil
   return pix, nil
}
func StringIpToInt(ipstring string) int {
@@ -173,7 +107,7 @@
   return ipInt
}
func IpIntToString(ipInt int) string{
func IpIntToString(ipInt int) string {
   ipSegs := make([]string, 4)
   var len int = len(ipSegs)
   buffer := bytes.NewBufferString("")
@@ -198,6 +132,14 @@
   }
   f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 32)
   return float32(f)
}
func ParseScore64(compareScore float64) float64 {
   if compareScore < 1 {
      compareScore = compareScore * 100
   }
   f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 64)
   return f
}
// UnCompress uncompress
@@ -227,3 +169,153 @@
   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
}
func FormatNum(oNum int,n int) string {
   m := 0
   for {
      oNum = oNum / 10
      m++
      if oNum == 0 {
         break
      }
   }
   fmtStr := "%0"+strconv.Itoa(m)+"d"
   return fmt.Sprintf(fmtStr, n)
}
func FileMd5(path string) (string,error){
   file, err := os.Open(path)
   if err !=nil {
      return "",err
   }
   defer file.Close()
   _md5 := md5.New()
   if _,err := io.Copy(_md5, file);err != nil {
      return "",err
   }
   return hex.EncodeToString(_md5.Sum(nil)),nil
}
func DownLoad(url string)([]byte,error){
   connectTimeout := 5 * time.Second
   readWriteTimeout := 10000 * time.Millisecond
   c := &http.Client{
      Transport: &http.Transport{
         TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
         Dial:            TimeoutDialer(connectTimeout, readWriteTimeout),
      },
   }
   req, err := http.NewRequest("GET",url,nil)
   if err != nil {
      return nil,err
   }
   resp,err := c.Do(req)
   body, err1 := ioutil.ReadAll(resp.Body)
   if err1 != nil {
      resp.Body.Close()
      return nil,err
   }else {
      logger.Debug("Download success")
   }
   resp.Body.Close()
   return body,nil
}
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
   return func(netw, addr string) (net.Conn, error) {
      conn, err := net.DialTimeout(netw, addr, cTimeout)
      if err != nil {
         return nil, err
      }
      conn.SetDeadline(time.Now().Add(rwTimeout))
      return conn, nil
   }
}