qixiaoning
2025-08-22 0f97177f258c67397b206b70e5aea2b24a4868c1
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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)
}