-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
358 lines (315 loc) · 9.51 KB
/
Copy pathutils.go
File metadata and controls
358 lines (315 loc) · 9.51 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
)
func CheckValidFlag(f string, flags []string) bool {
return slices.Contains(flags, f)
}
func CatchOutputFile(args []string, comp *FlagsComponents, flags []string) (bool, error) {
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag -O")
}
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing output file while presence of the -O flag")
}
comp.OutputFile = sli[1]
return false, nil
} else {
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag -O")
}
if args[1] == "" || strings.HasPrefix(args[1], "http") {
return false, errors.New("missing output file while presence of the -O flag")
}
comp.OutputFile = args[1]
return true, nil
}
}
func CatchInput(args []string, comp *FlagsComponents, flags []string) (bool, error) {
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag -i")
}
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing input file while presence of the -i flag")
}
comp.InputFile = sli[1]
return false, nil
} else {
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag -i")
}
if args[1] == "" || strings.HasPrefix(args[1], "http") {
return false, errors.New("missing input file while presence of the -i flag")
}
comp.InputFile = args[1]
return true, nil
}
}
func CatchPath(args []string, comp *FlagsComponents, flags []string) (bool, error) {
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag -P")
}
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing path while presence of the -P flag")
}
comp.PathFile = sli[1]
return false, nil
} else {
// fmt.Println("haaaaaaaani")
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag -P")
}
if args[1] == "" || strings.HasPrefix(args[1], "http") {
return false, errors.New("missing path while presence of the -P flag")
}
comp.PathFile = args[1]
return true, nil
}
}
func CatchRate(args []string, comp *FlagsComponents, flags []string) (bool, error) {
// fmt.Println(args)
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag --rate-limit")
}
// fmt.Println(sli)
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing the rate while presence of the --rate-limit flag")
}
comp.RateLimite = sli[1]
return false, nil
} else {
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag --rate-limit")
}
if args[1] == "" {
return false, errors.New("missing the rate while presence of the --rate-limit flag")
}
comp.RateLimite = args[1]
return true, nil
}
}
func CatchTheRejectedSuffix(args []string, comp *FlagsComponents, flags []string) (bool, error) {
if !comp.isMirror {
return false, errors.New("flag --mirror is missing")
}
var rejectEx string
var checker bool
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag -R || --reject")
}
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing rejected extensions while presence of the -R || --reject flag")
}
rejectEx = sli[1]
checker = false
} else {
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag -R || --reject")
}
if args[1] == "" || strings.HasPrefix(args[1], "http") {
return false, errors.New("missing rejected extensions while presence of the -R || --reject flag")
}
rejectEx = args[1]
checker = true
}
// Split and normalize: remove quotes, spaces, and leading '*' from each suffix
rawList := strings.Split(rejectEx, ",")
normalized := make([]string, 0, len(rawList))
for _, r := range rawList {
r = strings.TrimSpace(r)
r = strings.Trim(r, `"'`) // remove quotes if any
r = strings.TrimPrefix(r, "*") // remove leading '*'
r = strings.ToLower(r) // normalize case
normalized = append(normalized, r)
}
comp.Reject = normalized
return checker, nil
}
func CatchTheRExcludedFolders(args []string, comp *FlagsComponents, flags []string) (bool, error) {
if !comp.isMirror {
return false, errors.New("flag --mirror is missing")
}
var excludeEx string
var checker bool
if strings.Contains(args[0], "=") {
sli := strings.Split(args[0], "=")
if !CheckValidFlag(sli[0], flags) {
return false, errors.New("invalid flag -X || --exclude")
}
if len(sli) != 2 || sli[1] == "" {
return false, errors.New("missing exclude paths while presence of the -X || --exclude flag")
}
excludeEx = sli[1]
checker = false
} else {
if !CheckValidFlag(args[0], flags) {
return false, errors.New("invalid flag -X || --exclude")
}
if args[1] == "" || strings.HasPrefix(args[1], "http") {
return false, errors.New("missing exclude paths while presence of the -X || --exclude flag")
}
excludeEx = args[1]
checker = true
}
rawList := strings.Split(excludeEx, ",")
normalized := make([]string, 0, len(rawList))
for _, r := range rawList {
r = strings.TrimSpace(r)
r = strings.Trim(r, `"'`) // remove quotes if any
if !strings.HasPrefix(r, "/") {
r = "/" + r
}
if !strings.HasSuffix(r, "/") {
r = r + "/"
}
normalized = append(normalized, r)
}
comp.Exclude = normalized
return checker, nil
}
func (c *FlagsComponents) Validate() error {
// Check for conflicting flags
if c.InputFile != "" && len(c.Links) != 0 {
return fmt.Errorf("cannot use both -i (input file) and direct URL")
}
if c.OutputFile != "" && c.InputFile != "" {
return fmt.Errorf("cannot use -O (output file) with -i (batch download)")
}
if c.isMirror && c.OutputFile != "" {
return fmt.Errorf("cannot use -O (output file) with --mirror")
}
// Mirror-specific validations
if (len(c.Reject) > 0 || len(c.Exclude) > 0 || c.Convert) && !c.isMirror {
return fmt.Errorf("-R, -X, and --convert-links can only be used with --mirror")
}
// Require either URL or input file
if len(c.Links) == 0 && c.InputFile == "" {
return fmt.Errorf("must provide either URL or -i input file")
}
return nil
}
func logOrPrint(logger *log.Logger, background bool, message string) {
if background {
logger.Printf("%s", message)
} else {
fmt.Printf("%s", message)
}
}
func parseRateLimit(rateLimitStr string) (int64, error) {
if rateLimitStr == "" {
return 0, nil
}
// Remove whitespace and convert to lowercase
rateLimitStr = strings.ToLower(strings.TrimSpace(rateLimitStr))
// Default multiplier (bytes)
var multiplier int64 = 1
// Check for suffix and remove it
if strings.HasSuffix(rateLimitStr, "k") {
multiplier = 1024
rateLimitStr = rateLimitStr[:len(rateLimitStr)-1]
} else if strings.HasSuffix(rateLimitStr, "m") {
multiplier = 1024 * 1024
rateLimitStr = rateLimitStr[:len(rateLimitStr)-1]
}
// Parse the numeric part
value, err := strconv.ParseInt(rateLimitStr, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid rate limit format: %s", rateLimitStr)
}
return value * multiplier, nil
}
func formatSpeed(speedMBps float64) string {
// // Cap extremely high speeds to avoid scientific notation
// if speedMBps > 999.99 {
// speedMBps = 999.99
// }
// if speedMBps < 0.001 {
// speedMBps = 0.001
// }
// Fixed logic: show KB/s when speed is LESS than 1 MB/s
if speedMBps < 1 {
// return fmt.Sprintf("%.0f KB/s", speedMBps*1024)
return fmt.Sprintf("%.2f KB/s", speedMBps*1024)
}
// return fmt.Sprintf("%.2f MB/s", speedMBps)
return fmt.Sprintf("%.2f MB/s", speedMBps)
}
func Create_Output_file(Overide bool, filename string) (*os.File, error) {
var out *os.File
var err error
if Overide {
out, err = os.Create(filename)
if err != nil {
return nil, fmt.Errorf("failed to create file: %v", err)
}
} else {
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
out, err = os.Create(filename)
if err != nil {
return nil, fmt.Errorf("failed to create file: %v", err)
}
} else {
return nil, err
}
} else {
// File exists - create with number suffix
dir := filepath.Dir(filename)
ext := filepath.Ext(filename)
base := strings.TrimSuffix(filepath.Base(filename), ext)
i := 1
for {
if ext != "" {
filename = filepath.Join(dir, fmt.Sprintf("%s.%d%s", base, i, ext))
} else {
filename = filepath.Join(dir, fmt.Sprintf("%s.%d", base, i))
}
// Check if this numbered version exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
// This name is available
out, err = os.Create(filename)
if err != nil {
return nil, fmt.Errorf("failed to create file: %v", err)
}
break
}
i += 1
}
}
}
return out, nil
}
func formatETA(d time.Duration) string {
if d < 0 {
return "0s"
}
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
if hours > 0 {
return fmt.Sprintf("%dh%02dm", hours, minutes)
} else if minutes > 0 {
return fmt.Sprintf("%dm%02ds", minutes, seconds)
} else {
return fmt.Sprintf("%ds", seconds)
}
}