This repository was archived by the owner on Mar 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.go
More file actions
69 lines (65 loc) · 1.48 KB
/
system.go
File metadata and controls
69 lines (65 loc) · 1.48 KB
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
package ginx
import (
"os"
"os/exec"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/virzz/ginx/code"
"github.com/virzz/ginx/rsp"
"github.com/virzz/vlog"
)
func handleSystemUpgrade(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.AbortWithError(400, err)
return
}
exePath, err := os.Executable()
if err != nil {
c.AbortWithError(500, err)
return
}
newFile := exePath + "_upgrade"
err = c.SaveUploadedFile(file, newFile)
if err != nil {
c.AbortWithError(500, err)
return
}
err = os.Chmod(newFile, 0o755)
if err != nil {
c.AbortWithError(500, err)
return
}
err = os.Rename(newFile, exePath)
if err != nil {
c.AbortWithError(500, err)
return
}
buf, err := exec.Command(exePath, "restart").CombinedOutput()
if err != nil {
c.AbortWithError(500, err)
return
}
c.String(200, string(buf))
}
func handleSystemUpload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
vlog.Error("Failed to get file", "err", err.Error())
c.AbortWithStatusJSON(400, rsp.E(code.ParamInvalid, err))
return
}
exePath, err := os.Executable()
if err != nil {
c.AbortWithError(500, err)
return
}
dstPath := filepath.Join(filepath.Dir(exePath), "_"+filepath.Base(file.Filename))
err = c.SaveUploadedFile(file, dstPath)
if err != nil {
vlog.Error("Failed to save file", "file", file.Filename, "path", dstPath, "err", err.Error())
c.AbortWithStatusJSON(400, rsp.E(code.UnknownErr, err))
return
}
c.JSON(200, rsp.S(dstPath))
}