-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfile_sftp_handler.go
134 lines (100 loc) · 2.19 KB
/
file_sftp_handler.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
package main
import (
"time"
"log"
"sync"
"github.com/pkg/sftp"
)
var (
wg sync.WaitGroup
err error
sftpClient *sftp.Client
)
func fileSftpHandler() {
// Connect
sftpClient, err = connect(config.SshUserName, config.SshPassword, config.SshHost, config.SshPort)
if err != nil {
log.Fatal("SSH connect error: ", err)
}
defer sftpClient.Close()
// Check if the remote dir exist
if _, err := sftpClient.Stat(remoteDir); err != nil {
panic("Remote dir dose not exist: " + remoteDir)
}
wg.Add(5)
// Create Event handler
go func() {
for {
select {
case <-time.After(fileHandleTimeOut):
//timeout for 3 seconds
case createFileName := <-fileCreateEvent:
log.Print("createFile:" + createFileName)
if isDir(createFileName) == 1 {
uploadDirectory(createFileName, remoteDir)
} else {
uploadFile(createFileName, remoteDir)
}
}
}
defer wg.Done()
}()
// Write event handler
go func() {
for {
select {
case <-time.After(fileHandleTimeOut):
//timeout for 3 seconds
case writeFileName := <-fileWriteEvent:
log.Print("write file: " + writeFileName)
isDir := isDir(writeFileName)
if isDir == 1 {
uploadDirectory(writeFileName, remoteDir)
} else if isDir == 0 {
uploadFile(writeFileName, remoteDir)
}
}
}
defer wg.Done()
}()
// Remove event handler
go func() {
for {
select {
case <-time.After(fileHandleTimeOut):
//timeout for 3 seconds
case removeFileName := <-fileRemoveEvent:
log.Print("remove file: " + removeFileName)
remove(removeFileName, remoteDir)
}
}
defer wg.Done()
}()
// Rename event handler
go func() {
for {
select {
case <-time.After(fileHandleTimeOut):
//timeout for 3 seconds
case renameFileName := <-fileRenameEvent:
log.Print("rename file: " + renameFileName)
// Rename same as remove
remove(renameFileName, remoteDir)
}
}
defer wg.Done()
}()
// Chmod event handler
go func() {
for {
select {
case <-time.After(fileHandleTimeOut):
//timeout for 3 seconds
case chmodFileName := <-fileChmodEvent:
log.Print("chmod file" + chmodFileName)
}
}
defer wg.Done()
}()
wg.Wait()
}