forked from YouROK/TorrServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
63 lines (56 loc) · 1.09 KB
/
cache.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
package api
import (
"net/http"
"github.com/Terisback/TorrServer/server/torr"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// Action: get
type cacheReqJS struct {
requestI
Hash string `json:"hash,omitempty"`
}
// cache godoc
//
// @Summary Return cache stats
// @Description Return cache stats.
//
// @Tags API
//
// @Param request body cacheReqJS true "Cache stats request"
//
// @Produce json
// @Success 200 {object} state.CacheState "Cache stats"
// @Router /cache [post]
func cache(c *gin.Context) {
var req cacheReqJS
err := c.ShouldBindJSON(&req)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Status(http.StatusBadRequest)
switch req.Action {
case "get":
{
getCache(req, c)
}
}
}
func getCache(req cacheReqJS, 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.CacheState()
if st == nil {
c.JSON(200, struct{}{})
} else {
c.JSON(200, st)
}
} else {
c.Status(http.StatusNotFound)
}
}