-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileWatcher.go
More file actions
285 lines (245 loc) · 7.76 KB
/
fileWatcher.go
File metadata and controls
285 lines (245 loc) · 7.76 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/gen2brain/beeep"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"gopkg.in/ini.v1"
)
type Config struct {
FolderToWatch string
SftpServer string
SftpUser string
SftpPassword string
PrivateKeyPath string
WatchExtensions []string
destionationFolder string
processedFolder string
}
func main() {
// Read private key file
// Create a new SSH signer
// Create SSH client config
// Connect to the SFTP server
// Create SFTP client
// Process existing files in the folder
// Create a new file watcher
// Start watching the specified folder without subfolders
config, sftpClient, sshClient, watcher, shouldReturn := initialize()
if shouldReturn {
return
}
defer watcher.Close()
defer sftpClient.Close()
defer sshClient.Close()
// Process file events
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create == fsnotify.Create {
if hasExtension(event.Name, config.WatchExtensions) {
// A new file was created
fmt.Println("New file detected:", event.Name)
// Open the file
file, err := os.Open(event.Name)
if err != nil {
fmt.Println("Failed to open file:", err)
continue
}
defer file.Close()
err = copyFileToSftp(file, sftpClient, config.destionationFolder)
if err != nil {
fmt.Println("Error copying file to SFTP server:", err)
continue
}
// Check if the "processed" folder exists
if _, err := os.Stat(config.processedFolder); os.IsNotExist(err) {
// Create the "processed" folder
err := os.Mkdir(config.processedFolder, 0755)
if err != nil {
fmt.Println("Failed to create 'processed' folder:", err)
continue
}
}
processedFilePath := filepath.Join(config.processedFolder, filepath.Base(event.Name))
err = moveFileToProcessed(event.Name, file, processedFilePath)
if err != nil {
fmt.Println("Error moving file to 'processed' folder:", err)
continue
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
beeep.Alert("Error", "File watcher error: "+err.Error(), "error")
}
}
}
func initialize() (*Config, *sftp.Client, *ssh.Client, *fsnotify.Watcher, bool) {
workDir, err := os.Getwd()
if err != nil {
beeep.Alert("Error", "Failed to get working directory: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
config, err := loadConfig(filepath.Join(workDir, "config.ini"))
if err != nil {
beeep.Alert("Error", "Failed to load configuration: "+err.Error(), "error")
}
var auth []ssh.AuthMethod
var user string
if config.PrivateKeyPath != "" {
privateKey, err := os.ReadFile(config.PrivateKeyPath)
if err != nil {
beeep.Alert("Error", "Failed to read private key: "+config.PrivateKeyPath+" - "+err.Error(), "error")
return nil, nil, nil, nil, true
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
beeep.Alert("Error", "Failed to parse private key: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
auth = []ssh.AuthMethod{
ssh.PublicKeys(signer),
}
user = config.SftpUser
} else {
auth = []ssh.AuthMethod{
ssh.Password(config.SftpPassword),
}
user = config.SftpUser
}
sshConfig := &ssh.ClientConfig{
User: user,
Auth: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
sshClient, err := ssh.Dial("tcp", config.SftpServer+":22", sshConfig)
if err != nil {
beeep.Alert("Error", "Failed to connect to SFTP server: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
beeep.Alert("Error", "Failed to create SFTP client: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
err = processExistingFiles(config.FolderToWatch, sftpClient, *config)
if err != nil {
beeep.Alert("Error", "Failed to process existing files: "+err.Error(), "error")
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
beeep.Alert("Error", "Failed to create file watcher: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
err = watcher.Add(config.FolderToWatch)
if err != nil {
beeep.Alert("Error", "Failed to watch folder: "+err.Error(), "error")
return nil, nil, nil, nil, true
}
fmt.Println("Watching " + config.FolderToWatch + " folder for new files...")
return config, sftpClient, sshClient, watcher, false
}
func processExistingFiles(folderToWatch string, sftpClient *sftp.Client, config Config) error {
// Process existing files in the folder
files, err := os.ReadDir(folderToWatch)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
for _, fileInfo := range files {
if !fileInfo.IsDir() && hasExtension(fileInfo.Name(), config.WatchExtensions) {
// Open the file
file, err := os.Open(filepath.Join(folderToWatch, fileInfo.Name()))
if err != nil {
beeep.Alert("Error", fmt.Sprintf("Failed to open file: %s", err.Error()), "error")
continue
}
defer file.Close()
err = copyFileToSftp(file, sftpClient, config.destionationFolder)
if err != nil {
log.Println("Error copying file to SFTP server:", err)
}
err = moveFileToProcessed(filepath.Join(folderToWatch, fileInfo.Name()), file, filepath.Join(config.processedFolder, fileInfo.Name()))
if err != nil {
log.Println("Error moving file to 'processed' folder:", err)
}
}
}
return nil
}
func copyFileToSftp(file *os.File, sftpClient *sftp.Client, destFolder string) error {
fmt.Println("creating remote file: " + destFolder + filepath.Base(file.Name()))
// Create remote file
remoteFile, err := sftpClient.Create(destFolder + filepath.Base(file.Name()))
if err != nil {
fmt.Println("Failed to create remote file:", err)
return err
}
// Copy the contents of the local file to the remote file
_, err = io.Copy(remoteFile, file)
if err != nil {
fmt.Println("Failed to upload file to SFTP server:", err)
return err
}
fmt.Println("File uploaded successfully")
return nil
}
func moveFileToProcessed(srcFilePath string, file *os.File, processedPath string) error {
// Create the destination file
dstFile, err := os.Create(processedPath)
if err != nil {
fmt.Println("Failed to create destination file:", err)
return err
}
defer dstFile.Close()
// Copy the contents of the source file to the destination file
_, err = io.Copy(dstFile, file)
if err != nil {
fmt.Println("Failed to copy file to 'processed' folder:", err)
return err
}
fmt.Println("File copied to 'processed' folder successfully")
err = os.Remove(srcFilePath) // delete sourceFile
if err != nil {
fmt.Println("Failed to delete source file:", err)
return err
}
return nil
}
func loadConfig(filename string) (*Config, error) {
cfg, err := ini.Load(filename)
if err != nil {
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
config := &Config{}
// Read values from the ini file
config.FolderToWatch = cfg.Section("paths").Key("FolderToWatch").String()
config.SftpServer = cfg.Section("server").Key("SftpServer").String()
config.SftpUser = cfg.Section("server").Key("SftpUser").String()
config.SftpPassword = cfg.Section("server").Key("SftpPassword").String()
config.PrivateKeyPath = cfg.Section("paths").Key("PrivateKeyPath").String()
config.destionationFolder = cfg.Section("server").Key("DestinationFolder").String()
config.processedFolder = filepath.Join(config.FolderToWatch, "processed")
// Read list of file extensions to watch
config.WatchExtensions = cfg.Section("general").Key("WatchFileExtension").Strings(",")
return config, nil
}
func hasExtension(filename string, extensions []string) bool {
ext := filepath.Ext(filename)
for _, e := range extensions {
if e == ext {
return true
}
}
return false
}