add cache

This commit is contained in:
YouROK
2020-12-07 15:55:54 +03:00
parent 30604c6e94
commit a5c1d5b4de
7 changed files with 142 additions and 8 deletions

View File

@@ -0,0 +1,51 @@
package api
import (
"net/http"
"server/torr"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
//Action: get
type cacheReqJS struct {
requestI
Hash string `json:"hash,omitempty"`
}
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)
}
}

View File

@@ -23,6 +23,8 @@ func SetupRoute(route *gin.Engine) {
route.POST("/torrents", torrents)
route.POST("/torrent/upload", torrentUpload)
route.POST("/cache", cache)
route.GET("/stream", stream)
route.GET("/stream/*fname", stream)