forked from ItsRanveer-zz/goDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodm.go
150 lines (114 loc) · 2.96 KB
/
godm.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"gopkg.in/qml.v1"
)
var Receiver = make(chan int64)
type Downloader struct {
URLField qml.Object
Download qml.Object
Pause qml.Object
Resume qml.Object
Cancel qml.Object
ProgressBar qml.Object
Percent qml.Object
}
type Pipe struct {
io.ReadCloser
total int64 // Total no. of bytes transferred
}
func (pt *Pipe) Read(p []byte) (int, error) {
n, err := pt.ReadCloser.Read(p)
pt.total += int64(n)
Receiver <- pt.total
return n, err
}
func (d *Downloader) StartDownload(urlField, progress, percent, download, pause, resume, cancel qml.Object) {
d.ProgressBar = progress
d.Percent = percent
d.Download = download
d.Pause = pause
d.Resume = resume
d.Cancel = cancel
d.ProgressBar.Set("value", 0)
d.Percent.Set("text", "")
url := urlField.String("text")
if url == "" {
fmt.Println("Please specify a url to download a file from")
return
}
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Error While Getting file from URL: ", err)
return
}
destination, err := os.Create(fileName)
if err != nil {
fmt.Println("Error While Creating File: ", err)
return
}
fmt.Println("Download Started for : ", url)
contentLength := resp.ContentLength
resp.Body = &Pipe{ReadCloser: resp.Body}
d.ButtonClicked(0, 0, 0, 0)
go d.Copier(destination, resp, int(contentLength))
}
func (d *Downloader) Copier(destination *os.File, resp *http.Response, contentLength int) {
defer resp.Body.Close()
defer destination.Close()
go d.Progresser(contentLength)
noOfBytesWritten, err := io.Copy(destination, resp.Body)
if err != nil {
fmt.Println("Error While Downloading file from URL: ", err)
os.Exit(1)
}
fmt.Println("Transferred", noOfBytesWritten, "bytes")
}
func (d *Downloader) Progresser(contentLength int) {
for {
select {
case downloaded := <-Receiver:
percent := int(float64(downloaded) / (float64(contentLength) / float64(100)))
d.Percent.Set("text", strconv.Itoa(percent)+"%")
d.ProgressBar.Set("value", float64(downloaded)/float64(contentLength))
if downloaded >= int64(contentLength) {
fmt.Println("Done...")
d.ButtonClicked(1, 0, 0, 0)
return
}
}
}
}
func (d *Downloader) ButtonClicked(download, pause, resume, cancel int) {
d.Download.Set("enabled", download)
d.Pause.Set("enabled", pause)
d.Resume.Set("enabled", resume)
d.Cancel.Set("enabled", cancel)
}
func main() {
if err := qml.Run(godmUI); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func godmUI() error {
engine := qml.NewEngine()
controls, err := engine.LoadFile("main.qml")
if err != nil {
fmt.Println("Error While Loading QML File", err)
}
downloader := Downloader{}
context := engine.Context()
context.SetVar("download", &downloader)
window := controls.CreateWindow(nil)
window.Show()
window.Wait()
return err
}