Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.45 KB | None | 0 0
  1. package worker
  2.  
  3. import (
  4.     "errors"
  5.     "io"
  6.     "time"
  7.  
  8.     "lms-dws/internal/util/timeutil"
  9.  
  10.     "github.com/jlaffaye/ftp"
  11. )
  12.  
  13. // IFTPConnector ftp connector interface
  14. type IFTPConnector interface {
  15.     Quit() error
  16.     Logout() error
  17.     Login(username string, password string) error
  18.     List(path string) ([]*ftp.Entry, error)
  19.     Retr(filepath string) (io.Reader, error)
  20. }
  21.  
  22. type ftpConnector struct {
  23.     Conn *ftp.ServerConn
  24. }
  25.  
  26. // DialTimeout return IFTPConnector
  27. func DialTimeout(addr string, timeout time.Duration) (IFTPConnector, error) {
  28.     var (
  29.         err  error
  30.         conn *ftp.ServerConn
  31.     )
  32.  
  33.     // https://github.com/jlaffaye/ftp/issues/112
  34.     // Tricky way force to exit incase connection hang
  35.     // Consider switch to use https://github.com/goftp/server
  36.  
  37.     isTimeout := timeutil.TimeoutWithFunc(timeout, func() {
  38.         conn, err = ftp.DialTimeout(addr, timeout)
  39.     })
  40.  
  41.     if isTimeout {
  42.         return nil, errors.New("Dial is timeout")
  43.     }
  44.  
  45.     if err != nil {
  46.         return nil, err
  47.     }
  48.  
  49.     return &ftpConnector{
  50.         Conn: conn,
  51.     }, nil
  52. }
  53.  
  54. func (f *ftpConnector) Quit() error {
  55.     return f.Conn.Quit()
  56. }
  57.  
  58. func (f *ftpConnector) Logout() error {
  59.     return f.Conn.Logout()
  60. }
  61.  
  62. func (f *ftpConnector) Login(username string, password string) error {
  63.     return f.Conn.Login(username, password)
  64. }
  65.  
  66. func (f *ftpConnector) List(path string) ([]*ftp.Entry, error) {
  67.     return f.Conn.List(path)
  68. }
  69.  
  70. func (f *ftpConnector) Retr(path string) (io.Reader, error) {
  71.     return f.Conn.Retr(path)
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement