-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsftp.go
135 lines (111 loc) · 3.51 KB
/
sftp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"io"
"net"
"os"
"regexp"
"strings"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// SFTP client
type SFTP struct {
sftpclient *sftp.Client
filenameToDownload []string
lastFileModTime map[string]time.Time
lastFileUpload map[string]string
}
// NewSFTP initiates SFTP client
func NewSFTP(host, port, username, password string) Interface {
sshconfig := &ssh.ClientConfig{
User: username,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
hostAddr := fmt.Sprintf("%s:%s", host, port)
sshclient, errSSHClient := ssh.Dial("tcp", hostAddr, sshconfig)
if errSSHClient != nil {
panic(errSSHClient)
}
sftpclient, errSftpClient := sftp.NewClient(sshclient)
if errSftpClient != nil {
panic(errSftpClient)
}
return &SFTP{
sftpclient: sftpclient,
lastFileModTime: LastFileModTime,
lastFileUpload: LastFileUpload,
}
}
// ReaddirSourceFolder is used to read files in a dir
func (s *SFTP) ReaddirSourceFolder(crondata Cron) error {
fileToDownload := make([]string, 0)
if crondata.Task.FilePrefix != "" {
sourceFiles, errSourceFiles := s.sftpclient.ReadDir(crondata.Task.SourceFolder)
if errSourceFiles != nil {
Logf("Failed to list directory dir=%s error=%s\n", crondata.Task.SourceFolder, errSourceFiles.Error())
}
for _, item := range sourceFiles {
isMatch, _ := regexp.MatchString(crondata.Task.FilePrefix, item.Name())
isYearMatch := item.ModTime().Year() == time.Now().Year()
isMonthMatch := item.ModTime().Month() == time.Now().Month()
isDayMatch := item.ModTime().Day() == time.Now().Day()
prefixCodes := strings.Split(item.Name(), crondata.Task.FilePrefixDelimiter)
prefixCode := prefixCodes[crondata.Task.FilePrefixIndex]
isFileLatestUpdate := item.ModTime().After(s.lastFileModTime[prefixCode])
isPrevFileDifferent := s.lastFileUpload[prefixCode] != item.Name()
if !isPrevFileDifferent {
continue
}
if isMatch && isYearMatch && isMonthMatch && isDayMatch && isFileLatestUpdate && isPrevFileDifferent {
s.lastFileModTime[prefixCode] = item.ModTime()
s.lastFileUpload[prefixCode] = item.Name()
fileToDownload = append(fileToDownload, item.Name())
}
}
} else {
fileToDownload = append(fileToDownload, crondata.Task.File)
}
s.SetFilenameToDownload(fileToDownload)
return nil
}
// SetFilenameToDownload is used to set a filename to download as temp file
func (s *SFTP) SetFilenameToDownload(filename []string) {
s.filenameToDownload = filename
}
// GetFilenameToDownload is used to get a filename to download as temp file
func (s *SFTP) GetFilenameToDownload() []string {
return s.filenameToDownload
}
// DownloadTempFile will download the file
func (s *SFTP) DownloadTempFile(filepath string) error {
Logf("Downloading file=%s ...\n", filepath)
sourceFile, errSourceFile := s.sftpclient.Open(filepath)
if errSourceFile != nil {
return errSourceFile
}
defer sourceFile.Close()
tempfile := fmt.Sprintf("./%s", s.GetFilenameToDownload())
destinationFile, errCreateDestFile := os.Create(tempfile)
if errCreateDestFile != nil {
return errCreateDestFile
}
defer destinationFile.Close()
_, errCopySourceToDest := io.Copy(destinationFile, sourceFile)
if errCopySourceToDest != nil {
return errCopySourceToDest
}
destinationFile.Sync()
Log("File has been downloaded succesfully ...")
return nil
}
// Close is used to close a connection
func (s *SFTP) Close() {
s.sftpclient.Close()
}