-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (164 loc) · 3.6 KB
/
main.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
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
package main
import (
"bytes"
"fmt"
"ftploader/app"
"github.com/gorilla/mux"
"github.com/secsy/goftp"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
)
var logpath string = ""
func main() {
port := "3000"
if len(os.Args) == 2 {
port = os.Args[1]
}
r := mux.NewRouter()
r.HandleFunc("/download", download).Methods("GET")
fmt.Println("Listening...")
err := http.ListenAndServe(":"+port, r)
if err != nil {
fmt.Println(err.Error())
}
}
func toLog(msg string) {
config := app.NewConfig()
if logpath == "" && config.FTP.LogPath != "" {
logpath = config.FTP.LogPath
}
if logpath != "" {
f, err := os.OpenFile(logpath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
defer f.Close()
logger := log.New(f, "ftploader", log.LstdFlags)
logger.Println(msg)
} else {
log.Println(msg)
}
}
func download(w http.ResponseWriter, r *http.Request) {
if pth, ok := r.URL.Query()["path"]; ok && len(pth) > 0 {
if lg, ok := r.URL.Query()["log"]; ok && len(lg) > 0 {
logpath = lg[0]
}
data, err := ioutil.ReadFile(pth[0])
if err != nil {
toLog(err.Error())
return
}
type slicefls struct {
Files []string `json:"files"`
}
sfls := slicefls{}
errs := yaml.Unmarshal([]byte(data), &sfls)
if errs != nil {
toLog(errs.Error())
return
}
copyFiles(sfls.Files)
w.Write([]byte("Process finished"))
} else {
w.Write([]byte("send path parameter"))
w.WriteHeader(http.StatusBadRequest)
}
}
func copyFiles(slicefls []string) {
toLog("process started")
toLog("logpath:" + logpath)
config := app.NewConfig()
var wg sync.WaitGroup
goroutines := make(chan int, config.FTP.Connections)
// Read data from input channel
countError := 0
if slicefls != nil {
toLog(fmt.Sprintf("%v files", len(slicefls)))
for _, value := range slicefls {
name := value
goroutines <- 1
wg.Add(1)
go func(name string, goroutines <-chan int, countError *int, wg *sync.WaitGroup) {
msg, success := oneFile(name, name, *config)
if msg != "" {
toLog(msg)
if success == false {
*countError++
}
}
<-goroutines
wg.Done()
}(name, goroutines, &countError, &wg)
}
}
wg.Wait()
close(goroutines)
toLog("process finished")
toLog(fmt.Sprintf("ошибки: %v", countError))
}
func check(e error) {
if e != nil {
toLog(e.Error())
}
}
func oneFile(fileIn string, fileOut string, cnf app.Config) (result string, success bool) {
src := cnf.FTP.SourcePath + "/" + fileIn
destination := cnf.FTP.DestinationPath + "/" + fileOut
ok, err := exists(destination)
isExist := false
if ok && err == nil {
isExist = true
}
if cnf.FTP.IsRewrite == true || !isExist {
config := goftp.Config{
User: cnf.FTP.Login,
Password: cnf.FTP.Password,
ConnectionsPerHost: 10,
Timeout: 10 * time.Second,
Logger: os.Stderr,
}
client, err := goftp.DialConfig(config, cnf.FTP.Host)
if err != nil {
result = err.Error()
return
}
// download to a buffer instead of file
buf := new(bytes.Buffer)
err = client.Retrieve(src, buf)
if err != nil {
result = err.Error()
return
}
fl, err := os.Create(destination)
if err != nil {
result = err.Error()
return
}
defer fl.Close()
n2, err := fl.Write(buf.Bytes())
if err != nil {
result = err.Error()
return
}
result = fmt.Sprintf("загрузка %v bytes in %v", n2, fileOut)
}
success = true
return
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}