2020-11-08 17:19:25 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-11-09 10:33:19 +00:00
|
|
|
"net/http"
|
2021-01-02 19:09:05 +00:00
|
|
|
"sort"
|
2020-11-08 17:19:25 +00:00
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/v2/filecache"
|
2021-11-16 12:13:58 +00:00
|
|
|
"github.com/distribyted/distribyted/torrent"
|
2020-11-08 17:19:25 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
var apiStatusHandler = func(fc *filecache.Cache, ss *torrent.Stats) gin.HandlerFunc {
|
2020-11-08 17:19:25 +00:00
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
// TODO move to a struct
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
2020-11-08 17:19:25 +00:00
|
|
|
"cacheItems": fc.Info().NumItems,
|
|
|
|
"cacheFilled": fc.Info().Filled / 1024 / 1024,
|
|
|
|
"cacheCapacity": fc.Info().Capacity / 1024 / 1024,
|
|
|
|
"torrentStats": ss.GlobalStats(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
var apiRoutesHandler = func(ss *torrent.Stats) gin.HandlerFunc {
|
2020-11-08 17:19:25 +00:00
|
|
|
return func(ctx *gin.Context) {
|
2021-01-02 19:09:05 +00:00
|
|
|
s := ss.RoutesStats()
|
2021-11-16 12:13:58 +00:00
|
|
|
sort.Sort(torrent.ByName(s))
|
2021-01-02 19:09:05 +00:00
|
|
|
ctx.JSON(http.StatusOK, s)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
var apiAddTorrentHandler = func(s *torrent.Service) gin.HandlerFunc {
|
2020-11-08 17:19:25 +00:00
|
|
|
return func(ctx *gin.Context) {
|
2021-11-16 12:13:58 +00:00
|
|
|
route := ctx.Param("route")
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
var json RouteAdd
|
|
|
|
if err := ctx.ShouldBindJSON(&json); err != nil {
|
|
|
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
if err := s.AddMagnet(route, json.Magnet); err != nil {
|
|
|
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
ctx.JSON(http.StatusOK, nil)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
var apiDelTorrentHandler = func(s *torrent.Service) gin.HandlerFunc {
|
2020-11-08 17:19:25 +00:00
|
|
|
return func(ctx *gin.Context) {
|
2021-11-16 12:13:58 +00:00
|
|
|
route := ctx.Param("route")
|
|
|
|
hash := ctx.Param("torrent_hash")
|
2020-11-09 10:33:19 +00:00
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
if err := s.RemoveFromHash(route, hash); err != nil {
|
|
|
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
2020-11-09 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
ctx.JSON(http.StatusOK, nil)
|
2020-11-09 10:33:19 +00:00
|
|
|
}
|
|
|
|
}
|