-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (131 loc) · 3.41 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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"sync"
"time"
)
/*
HTTP HEAD => content length int > 0 != -1
HTTP HEAD => header["accept-ranges"] == [bytes] != []
resp is a pointer
*/
func downloadParti(wg *sync.WaitGroup, min, max, ind int, folder string, all *[]bool, url string) {
defer wg.Done()
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
range_header := "bytes=" + strconv.Itoa(min) + "-" + strconv.Itoa(max)
req.Header.Add("Range", range_header)
resp, err := client.Do(req)
if err != nil {
log.Fatalln("download part "+strconv.Itoa(ind)+"failed!\nerr: ", err)
}
defer resp.Body.Close()
out, err := os.Create(folder + "/" + strconv.Itoa(ind))
if err != nil {
log.Fatalln("can't create output file (probably permission denied)!\nerr: ", err)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Fatalln("can't insert data to output file "+strconv.Itoa(ind)+"!\nerr: ", err)
}
(*all)[ind] = true
fmt.Println("done part ", ind)
}
func Find(slice []bool, val bool) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func merge1(all []bool, files []string) {
_, found := Find(all, false)
if !found {
var buf bytes.Buffer
for _, file := range files {
b, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln("can't read file "+file+"\nerr: ", err)
}
buf.Write(b)
time.Sleep(1000 * time.Millisecond)
}
err := ioutil.WriteFile("output_file", buf.Bytes(), 0777) // 0644
if err != nil {
log.Fatalln("can't merge files\nerr: ", err)
}
} else {
fmt.Println("not all parts are available!")
}
}
func merge2(all []bool, files []string) {
_, found := Find(all, false)
if !found {
out, err := os.OpenFile("output", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln("can't merge files\nerr: ", err)
}
defer out.Close()
for _, file := range files {
n, _ := os.Open(file)
defer n.Close()
io.Copy(out, n)
time.Sleep(1000 * time.Millisecond)
}
} else {
fmt.Println("not all parts are available!")
}
}
func main() {
// TODO : DOWNLOAD PROGRESS
var url string
var nParts int
fmt.Println("enter URL of the file : ")
fmt.Scanln(&url)
fmt.Println("how many partitions ? (recommended : 5)")
fmt.Scanln(&nParts)
allDone := make([]bool, nParts)
files := make([]string, nParts)
cwd, _ := os.Getwd()
os.Mkdir("parts", 0755)
for i := 0; i < nParts; i++ {
files[i] = path.Join(cwd, "parts", strconv.Itoa(i))
}
fmt.Println("filenames are : ")
fmt.Println(files)
var wg sync.WaitGroup
resp, err := http.Head(url)
// TODO : FILE FORMAT FOR THE OUTPUT
if err != nil {
log.Fatalln(err)
}
fmt.Println("file byte length : ", int(resp.ContentLength))
fmt.Println("headers : ", resp.Header["Accept-Ranges"])
if resp.Header["Accept-Ranges"] == nil || int(resp.ContentLength) == -1 {
log.Fatalln("requested server does not allow Range Requests or the file is out of reach!")
}
rng := int(resp.ContentLength)
for i := 0; i < nParts-1; i++ {
mn := i * (rng / nParts)
mx := (i + 1) * (rng / nParts)
wg.Add(1)
go downloadParti(&wg, mn, mx, i, "parts", &allDone, url)
}
mn := (nParts - 1) * (rng / nParts)
mx := rng
wg.Add(1)
go downloadParti(&wg, mn, mx, nParts-1, "parts", &allDone, url)
fmt.Println("Main: Waiting for workers to finish")
wg.Wait()
merge2(allDone, files)
fmt.Println("Main: Completed")
}