-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
101 lines (92 loc) · 2.68 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
package exturl
import (
contextpkg "context"
"io"
neturlpkg "net/url"
"os"
"github.com/tliron/commonlog"
"github.com/tliron/kutil/util"
)
func ToNetURL(url URL) (*neturlpkg.URL, error) {
return neturlpkg.ParseRequestURI(url.String())
}
func GetPath(url URL) (string, error) {
if url_, err := ToNetURL(url); err == nil {
if url_.Path != "" {
return neturlpkg.PathUnescape(url_.Path)
} else {
return neturlpkg.PathUnescape(url_.Opaque)
}
} else {
return "", err
}
}
func ReadBytes(context contextpkg.Context, url URL) ([]byte, error) {
if reader, err := url.Open(context); err == nil {
reader = util.NewContextualReadCloser(context, reader)
defer commonlog.CallAndLogWarning(reader.Close, "exturl.ReadBytes", log)
return io.ReadAll(reader)
} else {
return nil, err
}
}
func ReadString(context contextpkg.Context, url URL) (string, error) {
if bytes, err := ReadBytes(context, url); err == nil {
return util.BytesToString(bytes), nil
} else {
return "", err
}
}
func Size(context contextpkg.Context, url URL) (int64, error) {
if reader, err := url.Open(context); err == nil {
reader = util.NewContextualReadCloser(context, reader)
defer commonlog.CallAndLogWarning(reader.Close, "exturl.Size", log)
return util.ReaderSize(reader)
} else {
return 0, err
}
}
func DownloadTo(context contextpkg.Context, url URL, path string) error {
if writer, err := os.Create(path); err == nil {
if reader, err := url.Open(context); err == nil {
reader = util.NewContextualReadCloser(context, reader)
defer commonlog.CallAndLogWarning(reader.Close, "exturl.DownloadTo", log)
log.Infof("downloading from %q to file %q", url.String(), path)
if _, err = io.Copy(writer, reader); err == nil {
return nil
} else {
log.Warningf("failed to download from %q", url.String())
return err
}
} else {
return err
}
} else {
return err
}
}
func Download(context contextpkg.Context, url URL, temporaryPathPattern string) (*os.File, error) {
if file, err := os.CreateTemp("", temporaryPathPattern); err == nil {
path := file.Name()
if reader, err := url.Open(context); err == nil {
reader = util.NewContextualReadCloser(context, reader)
defer commonlog.CallAndLogWarning(reader.Close, "exturl.Download", log)
log.Infof("downloading from %q to temporary file %q", url.String(), path)
if _, err = io.Copy(file, reader); err == nil {
util.OnExitError(func() error {
return DeleteTemporaryFile(path)
})
return file, nil
} else {
log.Warningf("failed to download from %q", url.String())
DeleteTemporaryFile(path)
return nil, err
}
} else {
DeleteTemporaryFile(path)
return nil, err
}
} else {
return nil, err
}
}