-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
54 lines (49 loc) · 1.72 KB
/
node.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
package storage
import (
"net/http"
"github.com/Nitecon/gosync/utils"
"github.com/Nitecon/gosync/nodeinfo"
"strings"
"time"
log "github.com/cihub/seelog"
)
func GetNodeCopy(item utils.DataTable, listener string, uid, gid int, perms string) bool {
cfg := utils.GetConfig()
aliveNodes := nodeinfo.GetNodes()
for _, node := range aliveNodes{
log.Infof("Trying download from: %s", node.NodeIPs)
nIPs := strings.Split(node.NodeIPs, ",")
for _, ipAddress := range nIPs{
resp, err := getData(ipAddress, cfg.ServerConfig.ListenPort, listener, utils.GetRelativePath(listener, item.Path))
if err == nil{
defer resp.Body.Close()
if resp.Status == "404" {
log.Infof("File not found: %s", item.Path)
return false
}
size, err := utils.FileWrite(item.Path, resp.Body, uid, gid, perms)
if err != nil{
log.Infof("Cannot write file: %s", item.Path)
return false
}else{
log.Infof("%s with %v bytes downloaded", item.Path, size)
return true
}
return false
}else{
return false
}
}
}
return false
}
func getData(hostname, port, listener, path string) (*http.Response, error){
rawURL := "http://" + hostname + ":" + port + "/" + listener + path
log.Infof("Download attempt on: %s", rawURL)
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(rawURL) // add a filter to check redirect
return resp, err
}