-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl.go
More file actions
203 lines (175 loc) · 3.86 KB
/
url.go
File metadata and controls
203 lines (175 loc) · 3.86 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
package rbxfetch
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/anaminus/iofl"
)
// FilterURL is an iofl.Filter that fetches from a URL.
type FilterURL struct {
URL string
GUID string
Client *http.Client
CacheMode CacheMode
CacheLocation string
r io.ReadCloser
err error
}
// NewFilterURL is an iofl.NewFilter that returns a FilterURL.
func NewFilterURL(params iofl.Params, r io.ReadCloser) (f iofl.Filter, err error) {
return &FilterURL{r: r,
URL: params.GetString("URL"),
}, nil
}
func (f *FilterURL) SetGUID(guid string) {
f.GUID = guid
}
func (f *FilterURL) SetClient(client *http.Client) {
f.Client = client
}
func (f *FilterURL) SetCache(mode CacheMode, loc string) {
f.CacheMode = mode
f.CacheLocation = loc
}
func (f *FilterURL) Source() io.ReadCloser {
return f.r
}
func (f *FilterURL) Close() error {
if f.err != nil {
return f.err
}
if f.err = f.r.Close(); f.err == nil {
f.err = iofl.Closed
return nil
}
return f.err
}
type statusError struct {
status int
msg string
}
func (s statusError) Error() string {
return s.msg + " (" + strconv.Itoa(s.status) + ")"
}
func (s statusError) Status() int {
return s.status
}
func hasStatusError(resp *http.Response) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err := statusError{status: resp.StatusCode, msg: resp.Status}
return fmt.Errorf("download from %s: %w", resp.Request.URL.String(), err)
}
return nil
}
func (f *FilterURL) download(url string) (rc io.ReadCloser, err error) {
c := f.Client
if c == nil {
c = http.DefaultClient
}
resp, err := c.Get(url)
if err != nil {
return nil, err
}
if err := hasStatusError(resp); err != nil {
return nil, err
}
return resp.Body, nil
}
const cacheDirName = "roblox-fetch"
func expandGUID(s, guid string) string {
return os.Expand(s, func(v string) string {
switch strings.ToLower(v) {
case "guid":
return guid
}
return ""
})
}
func (f *FilterURL) fetch() (rc io.ReadCloser, err error) {
u := expandGUID(f.URL, f.GUID)
loc, err := url.Parse(u)
if err != nil {
return nil, err
}
var cacheDir string
var cachedFilePath string
var downloaded bool
switch f.CacheMode {
case CacheTemp:
cacheDir = filepath.Join(os.TempDir(), cacheDirName)
case CachePerm:
dir, err := os.UserCacheDir()
if err != nil {
dir = os.TempDir()
}
cacheDir = filepath.Join(dir, cacheDirName)
case CacheCustom:
cacheDir = f.CacheLocation
default:
goto direct
}
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return nil, err
}
cachedFilePath = filepath.Join(cacheDir, url.PathEscape(loc.Host+loc.Path))
tryCache:
if cachedFile, err := os.Open(cachedFilePath); err == nil {
return cachedFile, nil
}
if !downloaded {
if tempFile, err := ioutil.TempFile(cacheDir, "temp"); err == nil {
tempName := tempFile.Name()
// Download response body.
rc, err := f.download(u)
if err != nil {
tempFile.Close()
os.Remove(tempFile.Name())
return nil, err
}
// Write to temp file.
_, err = io.Copy(tempFile, rc)
rc.Close()
if err != nil {
tempFile.Close()
os.Remove(tempFile.Name())
return nil, err
}
// Sync temp file.
err = tempFile.Sync()
tempFile.Close()
if err != nil {
os.Remove(tempFile.Name())
return nil, err
}
downloaded = true
// Attempt to relocate temp file to cache file.
if err := os.Rename(tempName, cachedFilePath); err != nil {
// Rename failed. Data is still in temp file, so we'll reuse that.
cachedFilePath = tempName
}
goto tryCache
}
}
direct:
// Return response body directly.
return f.download(u)
}
func (f *FilterURL) Read(p []byte) (n int, err error) {
if f.err != nil {
return 0, f.err
}
if f.r == nil {
f.r, err = f.fetch()
if err != nil {
f.err = err
return 0, err
}
}
return f.r.Read(p)
}