-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathfile.go
139 lines (133 loc) · 3.46 KB
/
file.go
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
package controller
import (
"fmt"
"io"
"net/http"
"strings"
"time"
"webssh/core"
"github.com/gin-gonic/gin"
)
// File 结构体
type File struct {
Name string
Size string
ModifyTime string
// 0: 文件 1: 文件夹
FType uint8
}
// UploadFile 上传文件
func UploadFile(c *gin.Context) *ResponseBody {
var (
sshClient core.SSHClient
err error
)
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
path := strings.TrimSpace(c.DefaultPostForm("path", "/root"))
sshInfo := c.PostForm("sshInfo")
if sshClient, err = core.DecodedMsgToSSHClient(sshInfo); err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
if err := sshClient.CreateSftp(); err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
file, header, err := c.Request.FormFile("file")
if err != nil {
responseBody.Msg = err.Error()
return &responseBody
}
defer file.Close()
filename := header.Filename
if path[len(path)-1:] == "/" {
path = path + filename
} else {
path = path + "/" + filename
}
err = sshClient.Upload(file, path)
if err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
}
return &responseBody
}
// DownloadFile 下载文件
func DownloadFile(c *gin.Context) *ResponseBody {
var (
sshClient core.SSHClient
err error
)
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
path := strings.TrimSpace(c.DefaultQuery("path", "/root"))
sshInfo := c.DefaultQuery("sshInfo", "")
if sshClient, err = core.DecodedMsgToSSHClient(sshInfo); err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
if err := sshClient.CreateSftp(); err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
if sftpFile, err := sshClient.Download(path); err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
} else {
defer sftpFile.Close()
c.Writer.WriteHeader(http.StatusOK)
fileMeta := strings.Split(path, "/")
c.Header("Content-Disposition", "attachment; filename="+fileMeta[len(fileMeta)-1])
_, _ = io.Copy(c.Writer, sftpFile)
}
return &responseBody
}
// FileList 获取文件列表
func FileList(c *gin.Context) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
path := c.DefaultQuery("path", "/root")
sshInfo := c.DefaultQuery("sshInfo", "")
sshClient, err := core.DecodedMsgToSSHClient(sshInfo)
if err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
result, err := sshClient.ExecRemoteCommand(fmt.Sprintf("ls -lh %s|grep -v ^l|awk 'NR>1'|sort", path))
if err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
if strings.Contains(result, "ls:") {
fmt.Println(result)
responseBody.Msg = result
return &responseBody
}
lineList := strings.Split(result, "\n")
var fileList []*File
for _, line := range lineList {
if line == "" {
continue
}
//多个空格合并为一个
line = strings.Join(strings.Fields(line), " ")
metaList := strings.Split(line, " ")
file := File{Name: metaList[len(metaList)-1], FType: 0, Size: metaList[4], ModifyTime: strings.Join(metaList[5:len(metaList)-1], " ")}
if strings.Contains(metaList[0], "d") {
file.FType = 1
}
fileList = append(fileList, &file)
}
responseBody.Data = map[string]interface{}{
"path": path,
"list": fileList,
}
return &responseBody
}