This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
api.go
343 lines (284 loc) · 7.81 KB
/
api.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/storage"
"github.com/gorilla/mux"
"go.uber.org/zap"
"golang.org/x/text/encoding/charmap"
"github.com/boramalper/magnetico/pkg/persistence"
)
type ApiReadmeHandler struct {
client *torrent.Client
tempdir string
}
func NewApiReadmeHandler() (*ApiReadmeHandler, error) {
h := new(ApiReadmeHandler)
var err error
h.tempdir, err = ioutil.TempDir("", "magneticod_")
if err != nil {
return nil, err
}
config := torrent.NewDefaultClientConfig()
config.ListenPort = 0
config.DefaultStorage = storage.NewFileByInfoHash(h.tempdir)
h.client, err = torrent.NewClient(config)
if err != nil {
_ = os.RemoveAll(h.tempdir)
return nil, err
}
return h, nil
}
func (h *ApiReadmeHandler) Close() {
h.client.Close()
_ = os.RemoveAll(h.tempdir)
}
func (h *ApiReadmeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
infohashHex := mux.Vars(r)["infohash"]
infohash, err := hex.DecodeString(infohashHex)
if err != nil {
respondError(w, http.StatusBadRequest, "couldn't decode infohash: %s", err.Error())
return
}
files, err := database.GetFiles(infohash)
if err != nil {
zap.L().Error("GetFiles error", zap.Error(err))
respondError(w, http.StatusInternalServerError, "Internal Server Error")
}
ok := false
for _, file := range files {
if strings.HasSuffix(file.Path, ".nfo") {
ok = true
break
} else if strings.Contains(file.Path, "read") {
ok = true
break
}
}
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
zap.L().Warn("README")
t, err := h.client.AddMagnet("magnet:?xt=urn:btih:" + infohashHex)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
defer t.Drop()
zap.L().Warn("WAITING FOR INFO")
select {
case <-t.GotInfo():
case <-time.After(30 * time.Second):
respondError(w, http.StatusInternalServerError, "Timeout")
return
}
zap.L().Warn("GOT INFO!")
t.CancelPieces(0, t.NumPieces())
var file *torrent.File
for _, file = range t.Files() {
filePath := file.Path()
if strings.HasSuffix(filePath, ".nfo") {
break
} else if strings.Contains(filePath, "read") {
break
}
}
if file == nil {
w.WriteHeader(http.StatusNotFound)
return
}
// Cancel if the file is larger than 50 KiB
if file.Length() > 50*1024 {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
file.Download()
reader := file.NewReader()
// BEWARE:
// ioutil.ReadAll(reader)
// returns some adjancent garbage too, for reasons unknown...
content := make([]byte, file.Length())
_, err = io.ReadFull(reader, content)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer reader.Close()
if strings.HasSuffix(file.Path(), ".nfo") {
content, err = charmap.CodePage437.NewDecoder().Bytes(content)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// Because .nfo files are right padded with \x00'es.
content = bytes.TrimRight(content, "\x00")
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
_, _ = w.Write(content)
}
func apiTorrents(w http.ResponseWriter, r *http.Request) {
// @lastOrderedValue AND @lastID are either both supplied or neither of them should be supplied
// at all; and if that is NOT the case, then return an error.
if q := r.URL.Query(); !((q.Get("lastOrderedValue") != "" && q.Get("lastID") != "") ||
(q.Get("lastOrderedValue") == "" && q.Get("lastID") == "")) {
respondError(w, 400, "`lastOrderedValue`, `lastID` must be supplied altogether, if supplied.")
return
}
var tq struct {
Epoch *int64 `schema:"epoch"`
Query *string `schema:"query"`
OrderBy *string `schema:"orderBy"`
Ascending *bool `schema:"ascending"`
LastOrderedValue *float64 `schema:"lastOrderedValue"`
LastID *uint64 `schema:"lastID"`
Limit *uint `schema:"limit"`
}
if err := decoder.Decode(&tq, r.URL.Query()); err != nil {
respondError(w, 400, "error while parsing the URL: %s", err.Error())
return
}
if tq.Query == nil {
tq.Query = new(string)
*tq.Query = ""
}
if tq.Epoch == nil {
tq.Epoch = new(int64)
*tq.Epoch = time.Now().Unix() // epoch, if not supplied, is NOW.
} else if *tq.Epoch <= 0 {
respondError(w, 400, "epoch must be greater than 0")
return
}
if tq.Ascending == nil {
tq.Ascending = new(bool)
*tq.Ascending = true
}
var orderBy persistence.OrderingCriteria
if tq.OrderBy == nil {
if *tq.Query == "" {
orderBy = persistence.ByDiscoveredOn
} else {
orderBy = persistence.ByRelevance
}
} else {
var err error
orderBy, err = parseOrderBy(*tq.OrderBy)
if err != nil {
respondError(w, 400, err.Error())
return
}
}
if tq.Limit == nil {
tq.Limit = new(uint)
*tq.Limit = 20
}
torrents, err := database.QueryTorrents(
*tq.Query, *tq.Epoch, orderBy,
*tq.Ascending, *tq.Limit, tq.LastOrderedValue, tq.LastID)
if err != nil {
respondError(w, 400, "query error: %s", err.Error())
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err = json.NewEncoder(w).Encode(torrents); err != nil {
zap.L().Warn("JSON encode error", zap.Error(err))
}
}
func apiTorrent(w http.ResponseWriter, r *http.Request) {
infohashHex := mux.Vars(r)["infohash"]
infohash, err := hex.DecodeString(infohashHex)
if err != nil {
respondError(w, 400, "couldn't decode infohash: %s", err.Error())
return
}
torrent, err := database.GetTorrent(infohash)
if err != nil {
respondError(w, 500, "couldn't get torrent: %s", err.Error())
return
} else if torrent == nil {
respondError(w, 404, "not found")
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err = json.NewEncoder(w).Encode(torrent); err != nil {
zap.L().Warn("JSON encode error", zap.Error(err))
}
}
func apiFilelist(w http.ResponseWriter, r *http.Request) {
infohashHex := mux.Vars(r)["infohash"]
infohash, err := hex.DecodeString(infohashHex)
if err != nil {
respondError(w, 400, "couldn't decode infohash: %s", err.Error())
return
}
files, err := database.GetFiles(infohash)
if err != nil {
respondError(w, 500, "couldn't get files: %s", err.Error())
return
} else if files == nil {
respondError(w, 404, "not found")
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err = json.NewEncoder(w).Encode(files); err != nil {
zap.L().Warn("JSON encode error", zap.Error(err))
}
}
func apiStatistics(w http.ResponseWriter, r *http.Request) {
from := r.URL.Query().Get("from")
// TODO: use gorilla?
var n int64
nStr := r.URL.Query().Get("n")
if nStr == "" {
n = 0
} else {
var err error
n, err = strconv.ParseInt(nStr, 10, 32)
if err != nil {
respondError(w, 400, "couldn't parse n: %s", err.Error())
return
} else if n <= 0 {
respondError(w, 400, "n must be a positive number")
return
}
}
stats, err := database.GetStatistics(from, uint(n))
if err != nil {
respondError(w, 400, "error while getting statistics: %s", err.Error())
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err = json.NewEncoder(w).Encode(stats); err != nil {
zap.L().Warn("JSON encode error", zap.Error(err))
}
}
func parseOrderBy(s string) (persistence.OrderingCriteria, error) {
switch s {
case "RELEVANCE":
return persistence.ByRelevance, nil
case "TOTAL_SIZE":
return persistence.ByTotalSize, nil
case "DISCOVERED_ON":
return persistence.ByDiscoveredOn, nil
case "N_FILES":
return persistence.ByNFiles, nil
case "UPDATED_ON":
return persistence.ByUpdatedOn, nil
case "N_SEEDERS":
return persistence.ByNSeeders, nil
case "N_LEECHERS":
return persistence.ByNLeechers, nil
default:
return persistence.ByDiscoveredOn, fmt.Errorf("unknown orderBy string: %s", s)
}
}