/
want.go
62 lines (52 loc) · 1.26 KB
/
want.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
package torrents
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
func Want(c *gin.Context, infohash, files string) {
if files == "none" {
WantNone(c, infohash)
return
}
ids, err := filesToIds(files)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err = app.Utorrent.Want(infohash, ids)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"error": false})
}
func filesToIds(files string) ([]int, error) {
list := strings.Split(files, ",")
ids := make([]int, len(list))
for i, v := range list {
num, err := strconv.Atoi(v)
if err != nil {
return ids, err
}
ids[i] = num
}
return ids, nil
}
func WantNone(c *gin.Context, infohash string) {
err := app.Utorrent.WantNone(infohash)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"error": false})
}
func Wanted(c *gin.Context, infohash string) {
want, err := app.Utorrent.Wanted(infohash)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"error": false, "wanted": want})
}