-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftp.go
44 lines (40 loc) · 959 Bytes
/
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
package request
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"github.com/interline-io/transitland-lib/tl"
"github.com/jlaffaye/ftp"
)
type Ftp struct{}
func (Ftp) Download(ctx context.Context, ustr string, secret tl.Secret, auth tl.FeedAuthorization) (io.ReadCloser, int, error) {
// Download FTP
u, err := url.Parse(ustr)
if err != nil {
return nil, 0, errors.New("could not parse url")
}
p := u.Port()
if p == "" {
p = "21"
}
c, err := ftp.Dial(fmt.Sprintf("%s:%s", u.Hostname(), p), ftp.DialWithContext(ctx))
if err != nil {
return nil, 0, errors.New("could not connect to server")
}
if auth.Type != "basic_auth" {
secret.Username = "anonymous"
secret.Password = "anonymous"
}
err = c.Login(secret.Username, secret.Password)
if err != nil {
return nil, 0, errors.New("could not connect to server")
}
r, err := c.Retr(u.Path)
if err != nil {
// return error directly
return nil, 0, err
}
return r, 0, nil
}