-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
103 lines (84 loc) · 1.87 KB
/
utils.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
package radikocast
import (
"crypto/md5"
"encoding/hex"
"errors"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sync"
)
const (
maxAttempts = 4
maxConcurrents = 8
)
var sem = make(chan struct{}, maxConcurrents)
func BulkDownload(list []string, output string) error {
var errFlag bool
var wg sync.WaitGroup
for _, v := range list {
wg.Add(1)
go func(link string) {
defer wg.Done()
var err error
for i := 0; i < maxAttempts; i++ {
sem <- struct{}{}
err = download(link, output)
<-sem
if err == nil {
break
}
}
if err != nil {
log.Printf("Failed to download: %s", err)
errFlag = true
}
}(v)
}
wg.Wait()
if errFlag {
return errors.New("Lack of aac files")
}
return nil
}
func download(link, output string) error {
resp, err := http.Get(link)
if err != nil {
return err
}
defer resp.Body.Close()
_, fileName := filepath.Split(link)
file, err := os.Create(filepath.Join(output, fileName))
if err != nil {
return err
}
_, err = io.Copy(file, resp.Body)
if closeErr := file.Close(); err == nil {
err = closeErr
}
return err
}
func HashFileMd5(filePath string) (string, error) {
//Initialize variable returnMD5String now in case an error has to be returned
var returnMD5String string
//Open the passed argument and check for any error
file, err := os.Open(filePath)
if err != nil {
return returnMD5String, err
}
//Tell the program to call the following function when the current function returns
defer file.Close()
//Open a new hash interface to write to
hash := md5.New()
//Copy the file in the hash interface and check for any error
if _, err := io.Copy(hash, file); err != nil {
return returnMD5String, err
}
//Get the 16 bytes hash
hashInBytes := hash.Sum(nil)[:16]
//Convert the bytes to a string
returnMD5String = hex.EncodeToString(hashInBytes)
return returnMD5String, nil
}