forked from itchio/go-itchio
/
sources.go
209 lines (169 loc) · 4.79 KB
/
sources.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
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
204
205
206
207
208
209
package itchfs
import (
"fmt"
"net/url"
"path"
"strconv"
"strings"
"github.com/go-errors/errors"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/httpkit/httpfile"
)
// over-engineering follows
type SourceType int
const (
// 691 is the earliest Bernoulli number that's also an irregular prime.
// It's also sufficiently different from zero that it should surface
// coding errors more easily
SourceType_DownloadBuild SourceType = 691 + iota
SourceType_KeyDownloadBuild
SourceType_WharfDownloadBuild
SourceType_DownloadUpload
SourceType_KeyDownloadUpload
)
type Source struct {
Type SourceType
ItchClient *itchio.Client
Path string
QueryValues url.Values
}
var patterns = map[string]SourceType{
"/upload/*/download": SourceType_DownloadUpload,
"/download-key/*/download/*": SourceType_KeyDownloadUpload,
"/upload/*/download/builds/*/*": SourceType_DownloadBuild,
"/download-key/*/download/*/builds/*/*": SourceType_KeyDownloadBuild,
"/wharf/builds/*/files/*/download": SourceType_WharfDownloadBuild,
}
func ObtainSource(itchClient *itchio.Client, itchPath string, queryValues url.Values) (*Source, error) {
var matches bool
var err error
for pattern, sourceType := range patterns {
matches, err = path.Match(pattern, itchPath)
if err != nil {
return nil, err
}
if matches {
return &Source{
Type: sourceType,
ItchClient: itchClient,
Path: itchPath,
QueryValues: queryValues,
}, nil
}
}
return nil, fmt.Errorf("unrecognized itchfs pattern: %s", itchPath)
}
func serveBuildFile(r itchio.DownloadUploadBuildResponse, fileType string) (string, error) {
switch fileType {
case "archive":
return r.Archive.URL, nil
case "patch":
return r.Patch.URL, nil
case "signature":
return r.Signature.URL, nil
case "manifest":
return r.Manifest.URL, nil
}
return "", fmt.Errorf("unknown file type %s", fileType)
}
func (s *Source) makeGetURL() (httpfile.GetURLFunc, error) {
tokens := strings.Split(s.Path, "/")
var getter httpfile.GetURLFunc
switch s.Type {
case SourceType_DownloadBuild:
uploadID, err := strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
buildID, err := strconv.ParseInt(tokens[5], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
downloadKey := s.QueryValues.Get("download_key_id")
fileType := tokens[6]
getter = func() (string, error) {
r, err := s.ItchClient.DownloadUploadBuildWithKeyAndValues(downloadKey, uploadID, buildID, stripDownloadKey(s.QueryValues))
if err != nil {
return "", err
}
return serveBuildFile(r, fileType)
}
// deprecated
case SourceType_KeyDownloadBuild:
downloadKey := tokens[2]
uploadID, err := strconv.ParseInt(tokens[4], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
buildID, err := strconv.ParseInt(tokens[6], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
fileType := tokens[7]
getter = func() (string, error) {
r, err := s.ItchClient.DownloadUploadBuildWithKeyAndValues(downloadKey, uploadID, buildID, stripDownloadKey(s.QueryValues))
if err != nil {
return "", err
}
return serveBuildFile(r, fileType)
}
case SourceType_WharfDownloadBuild:
buildID, err := strconv.ParseInt(tokens[3], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
buildFileID, err := strconv.ParseInt(tokens[5], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
getter = func() (string, error) {
r, err := s.ItchClient.GetBuildFileDownloadURLWithValues(buildID, buildFileID, stripDownloadKey(s.QueryValues))
if err != nil {
return "", err
}
return r.URL, nil
}
case SourceType_DownloadUpload:
uploadID, err := strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
downloadKey := s.QueryValues.Get("download_key_id")
getter = func() (string, error) {
r, err := s.ItchClient.UploadDownloadWithKeyAndValues(downloadKey, uploadID, stripDownloadKey(s.QueryValues))
if err != nil {
return "", err
}
return r.URL, nil
}
// deprecated
case SourceType_KeyDownloadUpload:
downloadKey := tokens[2]
uploadID, err := strconv.ParseInt(tokens[4], 10, 64)
if err != nil {
return nil, errors.Wrap(err, 1)
}
getter = func() (string, error) {
r, err := s.ItchClient.UploadDownloadWithKeyAndValues(downloadKey, uploadID, stripDownloadKey(s.QueryValues))
if err != nil {
return "", err
}
return r.URL, nil
}
default:
return nil, fmt.Errorf("unsupported source type: %d", s.Type)
}
return getter, nil
}
func stripDownloadKey(in url.Values) url.Values {
res := url.Values{}
for k, vv := range in {
if k == "download_key_id" {
continue
}
for _, v := range vv {
res.Add(k, v)
}
}
return res
}