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
import (
"errors"
"sort"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"server/log"
sets "server/settings"
)
var (
@@ -24,13 +24,12 @@ func AddTorrent(spec *torrent.TorrentSpec, title, poster string) (*Torrent, erro
return nil, err
}
if !torr.GotInfo() {
log.TLogln("error add torrent:", "timeout connection torrent")
return nil, errors.New("timeout connection torrent")
if torr.Title == "" {
torr.Title = title
}
if torr.Poster == "" {
torr.Poster = poster
}
torr.Title = title
torr.Poster = poster
if torr.Title == "" {
torr.Title = torr.Name()
@@ -86,3 +85,9 @@ func DropTorrent(hashHex string) {
hash := metainfo.NewHashFromHex(hashHex)
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 {
t.WaitInfo()
files := t.Files()
if fileIndex < 1 || fileIndex > len(files) {
return errors.New("file index out of range")

View File

@@ -27,5 +27,5 @@ func SetupRoute(route *gin.Engine) {
}
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/pkg/errors"
sets "server/settings"
"server/torr"
)
//Action: get, set
@@ -27,7 +28,7 @@ func settings(c *gin.Context) {
return
}
if req.Action == "set" {
sets.SetBTSets(req.Sets)
torr.SetSettings(req.Sets)
c.Status(200)
return
}

View File

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

View File

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