/
torrents.go
217 lines (201 loc) · 6.79 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/i96751414/torrest/bittorrent"
)
const (
startBufferPercent = 0.005
endBufferSize = 10 * 1024 * 1024 // 10MB
)
type FileInfoResponse struct {
*bittorrent.FileInfo
Status *bittorrent.FileStatus `json:"status,omitempty"`
}
type TorrentInfoResponse struct {
*bittorrent.TorrentInfo
Status *bittorrent.TorrentStatus `json:"status,omitempty"`
}
// @Summary List Torrents
// @Description list all torrents from service
// @ID list-torrents
// @Produce json
// @Param status query boolean false "get torrents status"
// @Success 200 {array} TorrentInfoResponse
// @Router /torrents [get]
func listTorrents(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
torrents := service.Torrents()
response := make([]TorrentInfoResponse, len(torrents))
for i, torrent := range torrents {
response[i].TorrentInfo = torrent.GetInfo()
}
if ctx.DefaultQuery("status", "false") == "true" {
for i, torrent := range torrents {
response[i].Status = torrent.GetStatus()
}
}
ctx.JSON(http.StatusOK, response)
}
}
// @Summary Remove Torrent
// @Description remove torrent from service
// @ID remove-torrent
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Param delete query boolean false "delete files"
// @Success 200 {object} MessageResponse
// @Failure 404 {object} ErrorResponse
// @Router /torrents/{infoHash}/remove [get]
func removeTorrent(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
infoHash := ctx.Param("infoHash")
removeFiles := ctx.DefaultQuery("delete", "true") != "false"
if err := service.RemoveTorrent(infoHash, removeFiles); err == nil {
ctx.JSON(http.StatusOK, NewMessageResponse("Torrent '%s' deleted", infoHash))
} else {
ctx.JSON(http.StatusNotFound, NewErrorResponse(err))
}
}
}
// @Summary Resume Torrent
// @Description resume a paused torrent
// @ID resume-torrent
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} MessageResponse
// @Failure 404 {object} ErrorResponse
// @Router /torrents/{infoHash}/resume [get]
func resumeTorrent(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
torrent.Resume()
ctx.JSON(http.StatusOK, NewMessageResponse("Torrent '%s' resumed", torrent.InfoHash()))
})
}
}
// @Summary Pause Torrent
// @Description pause torrent from service
// @ID pause-torrent
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} MessageResponse
// @Failure 404 {object} ErrorResponse
// @Router /torrents/{infoHash}/pause [get]
func pauseTorrent(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
torrent.Pause()
ctx.JSON(http.StatusOK, NewMessageResponse("Torrent '%s' paused", torrent.InfoHash()))
})
}
}
// @Summary Get Torrent Info
// @Description get torrent info
// @ID torrent-info
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} bittorrent.TorrentInfo
// @Failure 404 {object} ErrorResponse
// @Router /torrents/{infoHash}/info [get]
func torrentInfo(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
ctx.JSON(http.StatusOK, torrent.GetInfo())
})
}
}
// @Summary Get Torrent Status
// @Description get torrent status
// @ID torrent-status
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} bittorrent.TorrentStatus
// @Failure 404 {object} ErrorResponse
// @Router /torrents/{infoHash}/status [get]
func torrentStatus(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
ctx.JSON(http.StatusOK, torrent.GetStatus())
})
}
}
// @Summary Get Torrent Files
// @Description get a list of the torrent files and its details
// @ID torrent-files
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Param status query boolean false "get files status"
// @Success 200 {array} FileInfoResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /torrents/{infoHash}/files [get]
func torrentFiles(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
if files, err := torrent.Files(); err == nil {
response := make([]FileInfoResponse, len(files))
for i, file := range files {
response[i].FileInfo = file.Info()
}
if ctx.DefaultQuery("status", "false") == "true" {
for i, file := range files {
response[i].Status = file.Status()
}
}
ctx.JSON(http.StatusOK, response)
} else {
ctx.JSON(http.StatusInternalServerError, NewErrorResponse(err))
}
})
}
}
// @Summary Download
// @Description download all files from torrent
// @ID download-torrent
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} MessageResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /torrents/{infoHash}/download [get]
func downloadTorrent(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
if err := torrent.SetPriority(bittorrent.DefaultPriority); err == nil {
ctx.JSON(http.StatusOK, NewMessageResponse("torrent '%s' is downloading", torrent.InfoHash()))
} else {
ctx.JSON(http.StatusInternalServerError, NewErrorResponse(err))
}
})
}
}
// @Summary Stop Download
// @Description stop downloading torrent
// @ID stop-torrent
// @Produce json
// @Param infoHash path string true "torrent info hash"
// @Success 200 {object} MessageResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /torrents/{infoHash}/stop [get]
func stopTorrent(service *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
onGetTorrent(ctx, service, func(torrent *bittorrent.Torrent) {
if err := torrent.SetPriority(bittorrent.DontDownloadPriority); err == nil {
ctx.JSON(http.StatusOK, NewMessageResponse("stopped torrent '%s' download", torrent.InfoHash()))
} else {
ctx.JSON(http.StatusInternalServerError, NewErrorResponse(err))
}
})
}
}
// Can produce 404 (StatusNotFound) http error
func onGetTorrent(ctx *gin.Context, service *bittorrent.Service, f func(*bittorrent.Torrent)) {
infoHash := ctx.Param("infoHash")
if torrent, err := service.GetTorrent(infoHash); err == nil {
f(torrent)
} else {
ctx.JSON(http.StatusNotFound, NewErrorResponse(err))
}
}