-
Notifications
You must be signed in to change notification settings - Fork 8
/
ftp.go
335 lines (306 loc) · 8.18 KB
/
ftp.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package xutil
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
ftp4go "github.com/shenshouer/ftp4go"
)
type XFtp struct {
Addr string
User string
Pwd string
PASV string
FilePattern []string
LocalFilePrefix string
Conn *ftp4go.FTP
}
func (c *XFtp) Connect() (err error) {
addra := strings.Split(c.Addr, ":")
host := addra[0]
port, _ := strconv.Atoi(addra[1])
c.Conn = ftp4go.NewFTP(0) // 1 for debugging
_, err = c.Conn.Connect(host, port, "")
if err != nil {
return err
}
_, err = c.Conn.Login(c.User, c.Pwd, "")
if err != nil {
return err
}
if c.PASV == "PORT" {
c.Conn.SetPassive(false)
}
return nil
}
func (c XFtp) MKdir(path string) {
xdir, xfile := filepath.Split(path)
fname := filepath.Join(xdir, xfile)
xdirFiles, _ := c.Conn.Nlst(xdir)
for _, v := range xdirFiles {
if v == fname {
return
}
}
_, err := c.Conn.Mkd(path)
if err != nil {
c.MKdir(xdir)
c.MKdir(fname)
}
return
}
func (c XFtp) Size(path string) int64 {
size, err := c.Conn.Size(path)
if err != nil {
return 0
}
return int64(size)
}
func (c XFtp) NameList() (ftpfiles []string) {
return c.FileList("NLST")
}
func (c XFtp) InfoList() (ftpfiles []string) {
return c.FileList("LIST")
}
func (c XFtp) FileList(CMD string) (ftpfiles []string) {
for _, fpattern := range c.FilePattern {
nowfiles := []string{}
if strings.Contains(filepath.Dir(fpattern), "*") {
fdirs := []string{}
fmaps := make(map[string][]string, 0)
fpaths := strings.Split(fpattern, "/")
for i, fpath := range fpaths {
if strings.Contains(fpath, "*") {
fp := strings.Join(fpaths[0:i+1], "/")
fmaps[fp] = []string{}
fdirs = append(fdirs, fp)
}
}
if len(fdirs) == 0 {
continue
}
files, err := c.Conn.Nlst(fdirs[0])
if err != nil {
continue
}
fmaps[fdirs[0]] = files
for i, nowpath := range fdirs[1 : len(fdirs)-1] {
lastpath := fdirs[i]
xfdir := strings.ReplaceAll(nowpath, lastpath, "")
for _, fpath := range fmaps[lastpath] {
xfiles, _ := c.Conn.Nlst(filepath.Join(fpath, xfdir))
fmaps[nowpath] = append(fmaps[nowpath], xfiles...)
}
}
lastpath := fdirs[len(fdirs)-1]
lastPathDir := filepath.Dir(lastpath)
lastPathBase := filepath.Base(lastpath)
for _, fpath := range fmaps[lastPathDir] {
nowfiles = append(nowfiles, filepath.Join(fpath, lastPathBase))
}
} else {
nowfiles = []string{fpattern}
}
if CMD == "NLST" {
for _, v := range nowfiles {
xfiles, _ := c.Conn.Nlst(v)
xdir := filepath.Dir(v)
for i := range xfiles {
xfiles[i] = filepath.Join(xdir, filepath.Base(xfiles[i]))
}
ftpfiles = append(ftpfiles, xfiles...)
}
} else if CMD == "LIST" {
for _, v := range nowfiles {
xdir := filepath.Dir(v)
xfiles, _ := c.Conn.Dir(v)
for _, e := range xfiles {
ls := ParsrLS(e)
ftpfiles = append(ftpfiles, xdir+"/"+strings.Join(ls, ","))
}
}
}
}
return ftpfiles
}
func (c XFtp) DownloadFiles(files []string) (dat map[string]string, err error) {
dat = make(map[string]string, 0)
if len(files) == 0 {
return
}
if c.LocalFilePrefix != "" {
err = IsDirsExist([]string{c.LocalFilePrefix}, false)
if err == nil {
c.LocalFilePrefix = filepath.Dir(c.LocalFilePrefix+string(filepath.Separator)) + string(filepath.Separator)
} else {
return dat, err
}
}
fmt.Println("DownloadFiles begin")
for _, file := range files {
if c.LocalFilePrefix == "" {
c.LocalFilePrefix = time.Now().Format("20060102150405") + "_"
}
localpath := c.LocalFilePrefix + filepath.Base(file)
fmt.Println("DownloadFile " + file + " to " + localpath)
blockSize := 819200
f, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
err = c.Conn.GetBytes(ftp4go.RETR_FTP_CMD, f, blockSize, file)
// err = c.Conn.DownloadFile(file, localpath, false)
if err != nil {
return dat, err
}
f.Close()
dat[file] = localpath
}
fmt.Println("DownloadFiles end")
return dat, nil
}
func (c XFtp) DownloadFilesMap(files map[string]string) (dat map[string]string, err error) {
dat = make(map[string]string, 0)
if len(files) == 0 {
return
}
fmt.Println("DownloadFiles begin")
for ftpfile, localfile := range files {
fmt.Println("DownloadFile " + ftpfile + " to " + localfile)
blockSize := 819200
f, err := os.OpenFile(localfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
err = c.Conn.GetBytes(ftp4go.RETR_FTP_CMD, f, blockSize, ftpfile)
// err = c.Conn.DownloadFile(file, localpath, false)
if err != nil {
return dat, err
}
f.Close()
dat[ftpfile] = localfile
}
fmt.Println("DownloadFiles end")
return dat, nil
}
func (c XFtp) Logout() error {
_, err := c.Conn.Quit()
return err
}
func (c *XFtp) ConnectAndDownload() (files map[string]string, err error) {
err = c.Connect()
if err != nil {
return nil, err
}
defer c.Logout()
files, err = c.DownloadFiles(c.NameList())
if err != nil {
return nil, err
}
return files, nil
}
func (c *XFtp) UploadFiles(files map[string]string, useLineMode bool) (retInfo map[string]error) {
retInfo = make(map[string]error, 0)
for localname, remotename := range files {
retInfo[localname] = c.Conn.UploadFile(remotename, localname, useLineMode, nil)
}
return
}
//GetFTPFiles 获取 FTP/SFTP 匹配的文件
func GetFTPFiles(ftptype, addr, user, pwd, pasv, localfileprefix string, pattern []string, expectfiles []string) (files map[string]string, err error) {
ftpfiles := make([]string, 0)
var xftp XFtp
var xsftp XSFtp
files = make(map[string]string, 0)
switch ftptype {
case "FTP":
xftp = XFtp{Addr: addr,
User: user,
Pwd: pwd,
PASV: pasv,
FilePattern: pattern,
LocalFilePrefix: localfileprefix}
err = xftp.Connect()
if err != nil {
log.Println(err)
return
}
defer xftp.Logout()
ftpfiles = xftp.NameList()
case "SFTP":
xsftp = XSFtp{Addr: addr,
User: user,
Pwd: pwd,
FilePattern: pattern,
LocalFilePrefix: localfileprefix}
err = xsftp.Connect()
if err != nil {
log.Println(err)
return
}
defer xsftp.Logout()
ftpfiles = xsftp.NameList()
}
// ------------------------------------------------------------------------------
for i := range ftpfiles {
ftpfiles[i] = fmt.Sprintf("[%s]%s", addr, ftpfiles[i])
}
getftpfiles := StringsMinus(ftpfiles, expectfiles) // 要下载的文件 = FTP文件名 - 已入库的文件
if len(getftpfiles) == 0 { //没有可下载的文件
return
}
getftpfiles = StringsUniq(getftpfiles)
for i := range getftpfiles {
getftpfiles[i] = strings.TrimPrefix(getftpfiles[i], "["+addr+"]")
}
// ------------------------------------------------------------------------------
xfiles := make(map[string]string, 0)
switch ftptype {
case "FTP":
xfiles, err = xftp.DownloadFiles(getftpfiles)
case "SFTP":
xfiles, err = xsftp.DownloadFiles(getftpfiles)
}
for ftpfile, localfile := range xfiles {
files[fmt.Sprintf("[%s]%s", addr, ftpfile)] = localfile
}
return
}
func ParsrLS(s string) (fileInfo []string) {
//"drwxrwxr-x 5 577 554 4096 May 10 2019 pm",
//"-rwxrwxrwx 1 501 510 5102081 Oct 09 17:23 pmchk.out",
// "06-29-22 01:31PM 1383076 NK_Kpi4G_Plmn_202206291315.zip" --windowsFTP
var fileName, fileType, fileSize, fileTime string
arr := strings.Fields(s)
if len(arr) == 9 {
fileName, fileSize = arr[8], arr[4]
fileTime = strings.Join(arr[5:8], " ")
if strings.Contains(arr[7], ":") {
t, err := time.Parse("Jan 02 15:04", fileTime)
if err == nil {
fileTime = t.AddDate(time.Now().Year(), 0, 0).Format("2006-01-02 15:04")
}
} else {
t, err := time.Parse("Jan 02 2006", fileTime)
if err == nil {
fileTime = t.Format("2006-01-02 15:04")
}
}
switch arr[0][0] {
case '-':
fileType = "file"
case 'd':
fileType = "folder"
case 'l':
fileType = "link"
default:
fileType = ""
}
} else if len(arr) == 4 {
t, err := time.Parse("01-02-06 15:04PM", strings.Join(arr[0:2], " "))
if err == nil {
fileTime = t.Format("2006-01-02 15:04")
}
fileType, fileName, fileSize = "file", arr[3], arr[2]
} else {
return
}
return []string{fileName, fileType, fileSize, fileTime}
}