forked from xvill/xutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.go
More file actions
85 lines (73 loc) · 1.6 KB
/
ftp.go
File metadata and controls
85 lines (73 loc) · 1.6 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package xutil
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/jlaffaye/ftp"
)
type Ftp struct {
Conn *ftp.ServerConn
}
func OpenFtp(host, user, pwd string, timeout time.Duration) (Ftp, error) {
conn, err := ftp.DialTimeout(host, timeout)
if err != nil {
return Ftp{Conn: conn}, err
}
err = conn.Login(user, pwd)
if err != nil {
return Ftp{Conn: conn}, err
}
return Ftp{Conn: conn}, nil
}
func (c Ftp) NameList(pattern string) (ftpfiles []string) {
files, err := c.Conn.NameList(filepath.Dir(pattern))
if err != nil {
fmt.Println(err)
return nil
}
for _, v := range files {
if ok, _ := filepath.Match(pattern, v); ok {
ftpfiles = append(ftpfiles, v)
}
}
return ftpfiles
}
func (c Ftp) FilesByPattern(pattern []string) (dat map[string][]byte, err error) {
files := make([]string, 0)
for _, par := range pattern {
files = append(files, c.NameList(par)...)
}
return c.Files(files)
}
func (c Ftp) Files(files []string) (dat map[string][]byte, err error) {
dat = make(map[string][]byte, 0)
for _, file := range files {
r, err := c.Conn.Retr(file)
if err != nil {
return dat, err
}
buf, err := ioutil.ReadAll(r)
if err != nil {
return dat, err
}
dat[file] = buf
r.Close()
}
return dat, nil
}
func (c Ftp) Logout() error {
return c.Conn.Logout()
}
func FileFTP(host, user, pwd string, pattern []string) (files map[string][]byte, err error) {
ftp, err := OpenFtp(host, user, pwd, 10*time.Second)
if err != nil {
return nil, err
}
files, err = ftp.FilesByPattern(pattern)
if err != nil {
return nil, err
}
ftp.Logout()
return files, nil
}