This commit is contained in:
YouROK
2020-11-13 15:42:23 +03:00
parent 4645e7226e
commit ba26982d6f
6 changed files with 36 additions and 14 deletions

View File

@@ -1,12 +1,12 @@
package torr package torr
import ( import (
"errors"
"sort" "sort"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"server/log" "server/log"
sets "server/settings"
) )
var ( var (
@@ -24,13 +24,12 @@ func AddTorrent(spec *torrent.TorrentSpec, title, poster string) (*Torrent, erro
return nil, err return nil, err
} }
if !torr.GotInfo() { if torr.Title == "" {
log.TLogln("error add torrent:", "timeout connection torrent")
return nil, errors.New("timeout connection torrent")
}
torr.Title = title torr.Title = title
}
if torr.Poster == "" {
torr.Poster = poster torr.Poster = poster
}
if torr.Title == "" { if torr.Title == "" {
torr.Title = torr.Name() torr.Title = torr.Name()
@@ -86,3 +85,9 @@ func DropTorrent(hashHex string) {
hash := metainfo.NewHashFromHex(hashHex) hash := metainfo.NewHashFromHex(hashHex)
bts.RemoveTorrent(hash) bts.RemoveTorrent(hash)
} }
func SetSettings(set *sets.BTSets) {
bts.Disconnect()
sets.SetBTSets(set)
bts.Connect()
}

View File

@@ -12,6 +12,7 @@ import (
) )
func (t *Torrent) Stream(fileIndex int, req *http.Request, resp http.ResponseWriter) error { func (t *Torrent) Stream(fileIndex int, req *http.Request, resp http.ResponseWriter) error {
t.WaitInfo()
files := t.Files() files := t.Files()
if fileIndex < 1 || fileIndex > len(files) { if fileIndex < 1 || fileIndex > len(files) {
return errors.New("file index out of range") return errors.New("file index out of range")

View File

@@ -27,5 +27,5 @@ func SetupRoute(route *gin.Engine) {
} }
func echo(c *gin.Context) { func echo(c *gin.Context) {
c.String(200, "{\"version\": \"%v\"}", version.Version) c.String(200, "%v", version.Version)
} }

View File

@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
sets "server/settings" sets "server/settings"
"server/torr"
) )
//Action: get, set //Action: get, set
@@ -27,7 +28,7 @@ func settings(c *gin.Context) {
return return
} }
if req.Action == "set" { if req.Action == "set" {
sets.SetBTSets(req.Sets) torr.SetSettings(req.Sets)
c.Status(200) c.Status(200)
return return
} }

View File

@@ -65,6 +65,11 @@ func stream(c *gin.Context) {
return return
} }
if !tor.GotInfo() {
c.AbortWithError(http.StatusInternalServerError, errors.New("timeout connection torrent"))
return
}
// save to db // save to db
if save { if save {
torr.SaveTorrentToDB(tor) torr.SaveTorrentToDB(tor)

View File

@@ -75,12 +75,18 @@ func addTorrent(req torrReqJS, c *gin.Context) {
return return
} }
go func() {
if !tor.GotInfo() {
log.TLogln("error add torrent:", "timeout connection torrent")
return
}
if req.SaveToDB { if req.SaveToDB {
torr.SaveTorrentToDB(tor) torr.SaveTorrentToDB(tor)
} }
}()
st := tor.Status() c.JSON(200, tor.Status())
c.JSON(200, st)
} }
func getTorrent(req torrReqJS, c *gin.Context) { func getTorrent(req torrReqJS, c *gin.Context) {
@@ -109,6 +115,10 @@ func remTorrent(req torrReqJS, c *gin.Context) {
func listTorrent(req torrReqJS, c *gin.Context) { func listTorrent(req torrReqJS, c *gin.Context) {
list := torr.ListTorrent() list := torr.ListTorrent()
if list == nil {
c.Status(http.StatusNotFound)
return
}
var stats []*state.TorrentStatus var stats []*state.TorrentStatus
for _, tr := range list { for _, tr := range list {
stats = append(stats, tr.Status()) stats = append(stats, tr.Status())