forked from aouchcha/wget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleDownload.go
More file actions
303 lines (264 loc) · 8.97 KB
/
Copy pathSingleDownload.go
File metadata and controls
303 lines (264 loc) · 8.97 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
func DownloadOneSource(c *FlagsComponents, logger *log.Logger) error {
for _, link := range c.Links {
filename := c.OutputFile
Overide := true
if c.OutputFile == "" {
Overide = false
filename = GetOutputFromUrl(link)
}
if c.PathFile != "" {
filename = filepath.Join(c.PathFile, filename)
if strings.HasPrefix(filename, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get the home directory: %v", err)
}
filename = strings.ReplaceAll(filename, "~", homeDir)
}
if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
}
err := Download(link, c, filename, logger, Overide)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
}
return nil
}
func GetOutputFromUrl(Link string) string {
sli := strings.Split(Link, "/")
filename := sli[len(sli)-1]
if filename == "" {
filename = "index.html"
}
return filename
}
func Download(Link string, c *FlagsComponents, filename string, logger *log.Logger, Overide bool) error {
// Print timestamp and URL
logOrPrint(logger, c.Background, fmt.Sprintf("--%s-- %s\n", time.Now().Format("2006-01-02 15:04:05"), Link))
// Parse URL to get host
url, err := url.Parse(Link)
if err != nil {
return fmt.Errorf("failed to parse URL: %v", err)
}
response, err := http.Get(Link)
if err != nil {
return err
}
// Get the host name
// Look up IP address
ips, err := net.LookupIP(url.Host)
if err != nil {
logOrPrint(logger, c.Background, "DNS resolution failed")
return fmt.Errorf("failed to resolve hostname: %v", err)
}
// Print all the ips
var IpsTotal []string
for _, ip := range ips {
IpsTotal = append(IpsTotal, ip.String())
}
IpStr := strings.Join(IpsTotal, ", ")
logOrPrint(logger, c.Background, fmt.Sprintf("Resolving %s (%s)... %s\n", url.Host, url.Host, IpStr))
// if len(ips) > 0 {
// logOrPrint(logger, c.Background, fmt.Sprintf("Resolved to: %s", IpStr))
// }
// Print connecting message
port := "80"
if url.Scheme == "https" {
port = "443"
}
// if !c.Background {
// fmt.Printf("Connecting to %s (%s)|%s|:%s...", url.Host, url.Host, ips[0].String(), port)
// } else {
logOrPrint(logger, c.Background, fmt.Sprintf("Connecting to %s (%s)|%s|:%s...", url.Host, url.Host, ips[0].String(), port))
// }
startTime := time.Now()
defer response.Body.Close()
// if !c.Background {
// fmt.Printf(" connected.")
// } else {
logOrPrint(logger, c.Background, " connected.\n")
// }
// Print HTTP request status
logOrPrint(logger, c.Background, fmt.Sprintf("HTTP request sent, awaiting response... %s\n", response.Status))
if response.StatusCode != http.StatusOK {
return fmt.Errorf("--%s-- Error %d: %s", time.Now().Format("2006-01-02 15:04:05"), response.StatusCode, response.Status)
}
// Print content length
fileSize := response.ContentLength
contentType := response.Header.Get("Content-Type")
if contentType == "" {
contentType = "application/octet-stream"
}
if fileSize > 0 {
logOrPrint(logger, c.Background, fmt.Sprintf("Length: %d [%s]\n", fileSize, contentType))
} else {
logOrPrint(logger, c.Background, fmt.Sprintf("Length: unspecified [%s]\n", contentType))
}
// Create directory if needed
if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
// Create output file and ovrid the old if needed
OutputFile, err := Create_Output_file(Overide, filename)
if err != nil {
return err
}
defer OutputFile.Close()
logOrPrint(logger, c.Background, fmt.Sprintf("Saving to: '%s'\n", filepath.Base(filename)))
// Download with progress - ALWAYS show progress unless in background mode
rate, err := parseRateLimit(c.RateLimite)
if err != nil {
return err
}
var downloaded int64
if rate > 0 {
downloaded, err = copyWithRateLimit(response.Body, OutputFile, rate, fileSize, filename, logger, c.Background)
} else {
// Show progress regardless of whether we know file size
downloaded, err = copyWithProgress(response.Body, OutputFile, fileSize, filepath.Base(filename), logger, c.Background)
}
if err != nil {
return fmt.Errorf("download failed: %v", err)
}
// Calculate download speed and time
duration := time.Since(startTime)
speed := float64(downloaded) / duration.Seconds() / (1024 * 1024) // MB/s
logOrPrint(logger, c.Background, fmt.Sprintf("%s (%s) - '%s' saved [%d]\n",
time.Now().Format("2006-01-02 15:04:05"),
formatSpeed(speed),
filepath.Base(filename),
downloaded))
return nil
}
func copyWithProgress(src io.Reader, dst io.Writer, total int64, filename string, logger *log.Logger, background bool) (int64, error) {
var written int64
buf := make([]byte, 32*1024)
startTime := time.Now()
lastUpdate := time.Now()
for {
number_of_bytes_readed, err := src.Read(buf)
if number_of_bytes_readed > 0 {
number_of_byte_writed, err2 := dst.Write(buf[0:number_of_bytes_readed])
if number_of_byte_writed > 0 {
written += int64(number_of_byte_writed)
}
if err2 != nil {
return written, err2
}
if number_of_bytes_readed != number_of_byte_writed {
return written, io.ErrShortWrite
}
// Update progress more frequently - every 10ms or when finished
now := time.Now()
if now.Sub(lastUpdate) > 500*time.Millisecond || err == io.EOF {
showProgress(written, total, filename, time.Since(startTime), logger, background)
lastUpdate = now
}
}
if err != nil {
if err != io.EOF {
return written, err
}
break
}
}
// Final progress update
showProgress(written, total, filename, time.Since(startTime), logger, background)
fmt.Println()
return written, nil
}
func showProgress(downloaded, total int64, filename string, duration time.Duration, logger *log.Logger, background bool) {
// If in background mode, log progress periodically instead of showing progress bar
// if background {
// // Log progress every MB or when complete
// if downloaded%(1024*1024) == 0 || (total > 0 && downloaded >= total) {
// speed := float64(downloaded) / duration.Seconds() / (1024 * 1024)
// logOrPrint(logger, background, fmt.Sprintf("Downloaded: %.2fMB, Speed: %.2fMB/s",
// float64(downloaded)/(1024*1024), speed))
// }
// }
speed := float64(downloaded) / duration.Seconds() / (1024 * 1024)
// Create progress bar similar to wget
barWidth := 80 // Reduced width to fit better
var progressBar string
if total > 0 {
// Known file size - show normal progress bar
percentage := float64(downloaded) / float64(total) * 100
filled := int(percentage / 100 * float64(barWidth))
progressBar = strings.Repeat("=", filled)
if filled < barWidth {
progressBar += ">"
progressBar += strings.Repeat(" ", barWidth-filled-1)
}
} else {
// Unknown file size - show indeterminate progress (like wget's <=>)
// Create a moving indicator
pos := int(time.Now().UnixMilli()/100) % (barWidth - 6)
progressBar = strings.Repeat(" ", pos) + " <=> " + strings.Repeat(" ", barWidth-pos-6)
}
// downloaded file size
var filesize string
if downloaded/(1024*1024) > 1 {
filesize = fmt.Sprintf("%.2fM", float64(downloaded)/(1024*1024))
} else {
filesize = fmt.Sprintf("%.2fK", float64(downloaded)/(1024))
}
// fmt.Println("bbbbbbbbbb", background)
// if !background {
// fmt.Printf("\r%-20s [%s] %s %s", filename, progressBar, filesize, formatSpeed(speed))
// // fmt.Printf("\r %s %s %s %s", filename, progressBar, filesize, formatSpeed(speed))
// } else {
// fmt.Println("hanni")
// fmt.Println("")
if background {
remaining := total - downloaded
// remaining_Sec := time.Duration(float64(remaining)/speed) * time.Second
// fmt.Println(time.Duration(float64(remaining)/speed).Seconds() * 10)
remainingStr := formatETA(time.Duration(float64(remaining)/speed) * 10)
logOrPrint(logger, background, fmt.Sprintf("%dK %s %.0f%% %s %s", downloaded, strings.ReplaceAll(progressBar, "=", "."), float64(downloaded*100)/float64(total), formatSpeed(speed), remainingStr))
} else {
logOrPrint(logger, background, fmt.Sprintf("\r%s %.0f%% [%s] %s %s", filename, float64(downloaded*100)/float64(total), progressBar, filesize, formatSpeed(speed)))
}
// }
// Add timing info
if total > 0 && downloaded >= total {
// Complete - show "in Xs"
// if !background {
// fmt.Printf(" in %.1fs", duration.Seconds())
// } else {
// if {
logOrPrint(logger, background, fmt.Sprintf(" in %.2fs", duration.Seconds()))
// }
// }
}
// else if total > 0 && downloaded < total && speed > 0 {
// // Show ETA
// // remaining := total - downloaded
// // eta := time.Duration(float64(remaining)/speed) * 1000
// // if !background {
// // fmt.Printf(" eta %.1f", eta.Seconds())
// // } else {
// // logOrPrint(logger, background, fmt.Sprintf(" in %.1fs", duration.Seconds()))
// // if background {
// // logOrPrint(logger, background, fmt.Sprintf(" %.2f", eta.Seconds()))
// // }
// // }
// }
os.Stdout.Sync()
}