-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpclient.go
93 lines (87 loc) · 2.69 KB
/
ftpclient.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
// Package ftpclient implements some helper functions for an ftp client
package ftpclient
import (
"fmt"
"github.com/jlaffaye/ftp"
"github.com/m3tam3re/errors"
"io/ioutil"
"path/filepath"
"time"
)
const path errors.Path = "github.com/m3tam3re/ftpclient/ftpclient.go"
type FtpCon struct {
con *ftp.ServerConn
User string
Pass string
Host string
}
// GetFiletype downloads all files of the given ftype from the rfolder on the FTP Server
// to the local lfolder. if del is true files will be deleted after downloading
//
// Function params:
// lfolder = local folder for storing the files
// rfolder = remote folder to download the files from
// ftype = the file extension of the files that should be downloaded / "" for all files
// del = true for deleting files from the remote folder after download, false to keep them
func (fc *FtpCon) GetFiletype(lfolder string, rfolder string, ftype string, del bool) ([]string, error) {
var op errors.Op = "method: GetFiletype()"
var downloaded []string
if fc.con == nil {
err := fc.Connect()
if err != nil {
return nil, errors.E(errors.Internal, path, op, err)
}
}
defer fc.con.Logout()
if lfolder[len(lfolder)-1:] != "/" {
lfolder = lfolder + "/"
}
if rfolder[len(rfolder)-1:] != "/" {
rfolder = rfolder + "/"
}
if ftype != "" && ftype[0:1] != "." {
ftype = "." + ftype
}
err := fc.con.Login(fc.User, fc.Pass)
if err != nil {
return nil, errors.E(path, op, err, "could not login")
}
fl, err := fc.con.List(rfolder)
if err != nil {
return nil, errors.E(errors.Internal, path, op, err, "could not get file list")
}
for _, file := range fl {
if filepath.Ext(rfolder+file.Name) == ftype || ftype == "" {
f, err := fc.con.Retr(rfolder + file.Name)
if err != nil {
return nil, errors.E(errors.Internal, path, op, err, fmt.Sprintf("could not download %s", rfolder+file.Name))
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, errors.E(errors.Internal, path, op, err, fmt.Sprintf("could not download %s", rfolder+file.Name))
}
err = ioutil.WriteFile(lfolder+file.Name, buf, 0644)
if err != nil {
return nil, errors.E(errors.Internal, path, op, err, "could not write to local folder")
}
downloaded = append(downloaded, lfolder+file.Name)
f.Close()
if del {
err = fc.con.Delete(rfolder + file.Name)
if err != nil {
return nil, errors.E(errors.Internal, path, op, err, "could not delete file")
}
}
}
}
return downloaded, err
}
func (fc *FtpCon) Connect() error {
var op errors.Op = "method: Connect()"
c, err := ftp.Dial(fc.Host, ftp.DialWithTimeout(time.Second*10))
if err != nil {
return errors.E(errors.Internal, path, op, err, "connection error")
}
fc.con = c
return nil
}