sunty
2019-11-19 d9e01c51a525adf4f2393a95f87604e3b6e79ad2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package service
 
import (
    "bytes"
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "os"
    "strings"
    "basic.com/valib/logger.git"
 
    "webserver/util"
)
// 从服务器上下载文件到临时目录,校验之后如果完整将其拷贝到目标目录下
func DownSo(url string)(bool,error) {
    resp, err := http.Get(url)
    if err != nil {
        logger.Error("获取文件失败")
        return false,err
    }
    // 从resp中读出zip文件解压缩,解出face.so,face.txt,然后把解压出的so用MD5编码出一个temp.txt文件,与解压出的so.txt文件比对,
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        logger.Error("读取resp.body失败")
        return false,err
    }
    CopyFile(body, "/opt/temp/temp.zip")
    util.DeCompress("/opt/temp/temp.zip", "/opt/temp")
    fileName := GetFileNameFromUrl(url,false)
    md5str,err1 := File2md5("/opt/temp/"+fileName+".so")
    if err1 != nil {
        logger.Error(err1)
        return false,err1
    }
    md5str_origin,err2 := ioutil.ReadFile("/opt/temp/"+fileName+".txt")
    if err2 != nil {
        logger.Error("读取解压后的md5文件失败")
        return false,err2
    }
    flag := CompareMd5([]byte(md5str),md5str_origin)
    if flag {
        logger.Info("两次MD5编码一致!")
    } else {
        logger.Debug("两次MD5编码不一致,请重新下载")
        return false,nil
    }
    // 从url中截取soName
    soName := GetFileNameFromUrl(url,true)
    f, err := os.Create("/opt/workspace/ruleprocess/algorithm/" + soName)
    if err != nil {
        logger.Error("在项目目录下创建so文件失败")
        return false,err
    }
    data,_ := ioutil.ReadFile("/opt/temp/"+soName)
    _,err4 := f.Write(data)
    if err4 != nil {
        logger.Error("复制文件出错")
        return false,err4
    }
    return true,nil
}
 
func CopyFile(byte []byte, dst string) (w int64, err error) {
    dstFile, err := os.Create(dst)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer dstFile.Close()
    return io.Copy(dstFile, bytes.NewReader(byte))
}
 
// 指定目录的文件生成相应的MD5码文件
func File2md5 (filename string) (string, error) {
    // 文件生成MD5加密加密文件
    file, err := os.Open(filename)
    if err != nil {
        fmt.Println("os Open error")
        return "", err
    }
    md5 := md5.New()
    _, err = io.Copy(md5, file)
    if err != nil {
        fmt.Println("io copy error")
        return "", err
    }
    md5Str := hex.EncodeToString(md5.Sum(nil))
    return md5Str,nil
}
// 从url中截取出文件名,参数是是否带后缀
func GetFileNameFromUrl(url string,withSuffix bool)string {
    fileName := strings.Split(url,"/")[len(strings.Split(url,"/"))-1]
    if withSuffix {
        return fileName
    } else {
        withoutSuffix := strings.Split(fileName,".")[0]
        return withoutSuffix
    }
}
 
// 比较两个MD5编码是否一致
func CompareMd5(value1 []byte,value2 []byte) bool{
    num := bytes.Compare(value1,value2)
    if num == 0 {
        return true
    } else {
        return false
    }
}