forked from YouROK/TorrServer
/
torrents.go
203 lines (185 loc) · 4.26 KB
/
torrents.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
package api
import (
"net/http"
"strings"
"github.com/Terisback/TorrServer/server/dlna"
"github.com/Terisback/TorrServer/server/log"
set "github.com/Terisback/TorrServer/server/settings"
"github.com/Terisback/TorrServer/server/torr"
"github.com/Terisback/TorrServer/server/torr/state"
"github.com/Terisback/TorrServer/server/web/api/utils"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// Action: add, get, set, rem, list, drop
type torrReqJS struct {
requestI
Link string `json:"link,omitempty"`
Hash string `json:"hash,omitempty"`
Title string `json:"title,omitempty"`
Poster string `json:"poster,omitempty"`
Data string `json:"data,omitempty"`
SaveToDB bool `json:"save_to_db,omitempty"`
}
// torrents godoc
//
// @Summary Handle torrents informations
// @Description Allow to add, get or set torrents to server. The action depends of what has been asked.
//
// @Tags API
//
// @Param request body torrReqJS true "Torrent request"
//
// @Accept json
// @Produce json
// @Success 200
// @Router /torrents [post]
func torrents(c *gin.Context) {
var req torrReqJS
err := c.ShouldBindJSON(&req)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Status(http.StatusBadRequest)
switch req.Action {
case "add":
{
addTorrent(req, c)
}
case "get":
{
getTorrent(req, c)
}
case "set":
{
setTorrent(req, c)
}
case "rem":
{
remTorrent(req, c)
}
case "list":
{
listTorrent(req, c)
}
case "drop":
{
dropTorrent(req, c)
}
case "wipe":
{
wipeTorrents(req, c)
}
}
}
func addTorrent(req torrReqJS, c *gin.Context) {
if req.Link == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("link is empty"))
return
}
log.TLogln("add torrent", req.Link)
req.Link = strings.ReplaceAll(req.Link, "&", "&")
torrSpec, err := utils.ParseLink(req.Link)
if err != nil {
log.TLogln("error parse link:", err)
c.AbortWithError(http.StatusBadRequest, err)
return
}
tor, err := torr.AddTorrent(torrSpec, req.Title, req.Poster, req.Data)
if err != nil {
log.TLogln("error add torrent:", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
go func() {
if !tor.GotInfo() {
log.TLogln("error add torrent:", "timeout connection get torrent info")
return
}
if tor.Title == "" {
tor.Title = torrSpec.DisplayName // prefer dn over name
tor.Title = strings.ReplaceAll(tor.Title, "rutor.info", "")
tor.Title = strings.ReplaceAll(tor.Title, "_", " ")
tor.Title = strings.Trim(tor.Title, " ")
if tor.Title == "" {
tor.Title = tor.Name()
}
}
if req.SaveToDB {
torr.SaveTorrentToDB(tor)
}
}()
// TODO: remove
if set.BTsets.EnableDLNA {
dlna.Stop()
dlna.Start()
}
c.JSON(200, tor.Status())
}
func getTorrent(req torrReqJS, c *gin.Context) {
if req.Hash == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("hash is empty"))
return
}
tor := torr.GetTorrent(req.Hash)
if tor != nil {
st := tor.Status()
c.JSON(200, st)
} else {
c.Status(http.StatusNotFound)
}
}
func setTorrent(req torrReqJS, c *gin.Context) {
if req.Hash == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("hash is empty"))
return
}
torr.SetTorrent(req.Hash, req.Title, req.Poster, req.Data)
c.Status(200)
}
func remTorrent(req torrReqJS, c *gin.Context) {
if req.Hash == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("hash is empty"))
return
}
torr.RemTorrent(req.Hash)
// TODO: remove
if set.BTsets.EnableDLNA {
dlna.Stop()
dlna.Start()
}
c.Status(200)
}
func listTorrent(req torrReqJS, c *gin.Context) {
list := torr.ListTorrent()
if len(list) == 0 {
c.JSON(200, []*state.TorrentStatus{})
return
}
var stats []*state.TorrentStatus
for _, tr := range list {
stats = append(stats, tr.Status())
}
c.JSON(200, stats)
}
func dropTorrent(req torrReqJS, c *gin.Context) {
if req.Hash == "" {
c.AbortWithError(http.StatusBadRequest, errors.New("hash is empty"))
return
}
torr.DropTorrent(req.Hash)
c.Status(200)
}
func wipeTorrents(req torrReqJS, c *gin.Context) {
torrents := torr.ListTorrent()
for _, t := range torrents {
torr.RemTorrent(t.TorrentSpec.InfoHash.HexString())
}
// TODO: remove (copied todo from remTorrent())
if set.BTsets.EnableDLNA {
dlna.Stop()
dlna.Start()
}
c.Status(200)
}