goPaxan/ftpActions.go

58 lines
892 B
Go
Raw Normal View History

2020-07-18 06:55:51 +00:00
package paxan
import (
"io"
"os"
2020-07-18 07:09:55 +00:00
"path/filepath"
2020-07-18 06:55:51 +00:00
"time"
"github.com/jlaffaye/ftp"
)
func DownloadFTP(user string, password string, path string) error {
c, err := ftp.Dial("paxan-shop.de:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
return err
}
err = c.Login(user, password)
if err != nil {
return err
}
defer c.Quit()
//c.ChangeDir("desiredDir")
2020-07-18 07:25:00 +00:00
files, err := c.List("/")
2020-07-18 06:55:51 +00:00
if err != nil {
return err
}
for _, file := range files {
2020-07-26 18:23:34 +00:00
if file.Type != ftp.EntryTypeFile {
continue
}
2020-07-18 06:55:51 +00:00
res, err := c.Retr(file.Name)
if err != nil {
return err
}
2020-07-18 07:09:55 +00:00
outFile, err := os.Create(filepath.Join(path, file.Name))
2020-07-18 06:55:51 +00:00
if err != nil {
return err
}
defer outFile.Close()
_, err = io.Copy(outFile, res)
if err != nil {
return err
}
res.Close()
2020-07-26 18:26:35 +00:00
err = os.Chtimes(filepath.Join(path, file.Name), file.Time, file.Time)
2020-07-18 06:55:51 +00:00
}
return nil
}