-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
http.go
69 lines (57 loc) 路 1.34 KB
/
http.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
package helper
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
log "github.com/sirupsen/logrus"
)
const (
// HTTPTimeout is a timeout for HTTP requests.
HTTPTimeout = 30 * time.Second
)
// Download downloads uri to file.
func Download(file, uri string) error {
f, err := CreateFile(file)
if err != nil {
return err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Errorf("failed to close file %s: %v", f.Name(), err)
}
}(f)
ctx, cancel := context.WithTimeout(context.Background(), HTTPTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, http.NoBody)
if err != nil {
return fmt.Errorf("failed to create request to %s: %w", uri, err)
}
r, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download %s: %w", uri, err)
}
defer func(b io.Closer) {
err := b.Close()
if err != nil {
log.Errorf("failed to close HTTP body: %v", err)
}
}(r.Body)
if r.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", r.Status)
}
_, err = io.Copy(f, r.Body)
if err != nil {
return fmt.Errorf("failed to copy body of %s: %w", uri, err)
}
return nil
}
// IsURL reports whether provided string is a valid URL.
func IsURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}