Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package worker
- import (
- "errors"
- "io"
- "time"
- "lms-dws/internal/util/timeutil"
- "github.com/jlaffaye/ftp"
- )
- // IFTPConnector ftp connector interface
- type IFTPConnector interface {
- Quit() error
- Logout() error
- Login(username string, password string) error
- List(path string) ([]*ftp.Entry, error)
- Retr(filepath string) (io.Reader, error)
- }
- type ftpConnector struct {
- Conn *ftp.ServerConn
- }
- // DialTimeout return IFTPConnector
- func DialTimeout(addr string, timeout time.Duration) (IFTPConnector, error) {
- var (
- err error
- conn *ftp.ServerConn
- )
- // https://github.com/jlaffaye/ftp/issues/112
- // Tricky way force to exit incase connection hang
- // Consider switch to use https://github.com/goftp/server
- isTimeout := timeutil.TimeoutWithFunc(timeout, func() {
- conn, err = ftp.DialTimeout(addr, timeout)
- })
- if isTimeout {
- return nil, errors.New("Dial is timeout")
- }
- if err != nil {
- return nil, err
- }
- return &ftpConnector{
- Conn: conn,
- }, nil
- }
- func (f *ftpConnector) Quit() error {
- return f.Conn.Quit()
- }
- func (f *ftpConnector) Logout() error {
- return f.Conn.Logout()
- }
- func (f *ftpConnector) Login(username string, password string) error {
- return f.Conn.Login(username, password)
- }
- func (f *ftpConnector) List(path string) ([]*ftp.Entry, error) {
- return f.Conn.List(path)
- }
- func (f *ftpConnector) Retr(path string) (io.Reader, error) {
- return f.Conn.Retr(path)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement