forked from itchio/go-itchio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itchfs.go
64 lines (51 loc) · 1.32 KB
/
itchfs.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
package itchfs
import (
"fmt"
"net/http"
"net/url"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/httpkit/httpfile"
)
type ItchFS struct {
ItchServer string
}
func (ifs *ItchFS) Scheme() string {
return "itchfs"
}
func needsRenewal(res *http.Response, body []byte) bool {
if res.StatusCode == 400 {
// XXX: could parse XML / make sure it's expired URL and not something else,
// but 400 is a good enough indicator for GCS
return true
}
if res.StatusCode == 403 {
// 403 is a good indicator for Highwinds - additionally, we could parse the URL
// and compare the expires timestamp ourselves
return true
}
return false
}
func (ifs *ItchFS) MakeResource(u *url.URL) (httpfile.GetURLFunc, httpfile.NeedsRenewalFunc, error) {
if u.Host != "" {
return nil, nil, fmt.Errorf("invalid itchfs URL (must start with itchfs:///): %s", u.String())
}
vals := u.Query()
apiKey := vals.Get("api_key")
if apiKey == "" {
return nil, nil, fmt.Errorf("missing API key")
}
vals.Del("api_key")
itchClient := itchio.ClientWithKey(apiKey)
if ifs.ItchServer != "" {
itchClient.SetServer(ifs.ItchServer)
}
source, err := ObtainSource(itchClient, u.Path, vals)
if err != nil {
return nil, nil, err
}
getURL, err := source.makeGetURL()
if err != nil {
return nil, nil, err
}
return getURL, needsRenewal, nil
}