Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions server/torr/apihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ func ListTorrent() []*Torrent {
return ret
}

func ListTorrentFiltered(filter string) []*Torrent {
switch filter {
case "last":
all := ListTorrent()
if len(all) > 0 {
return all[:1]
}
return nil
case "active":
btlist := bts.ListTorrents()
var ret []*Torrent
for _, t := range btlist {
ret = append(ret, t)
}
sort.Slice(ret, func(i, j int) bool {
if ret[i].Timestamp != ret[j].Timestamp {
return ret[i].Timestamp > ret[j].Timestamp
}
return ret[i].Title > ret[j].Title
})
return ret
default:
return ListTorrent()
}
}

func DropTorrent(hashHex string) {
hash := metainfo.NewHashFromHex(hashHex)
bts.RemoveTorrent(hash)
Expand Down
39 changes: 39 additions & 0 deletions server/torr/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,45 @@ func (t *Torrent) Status() *state.TorrentStatus {
return st
}

func (t *Torrent) StatusLight() *state.TorrentStatus {
t.muTorrent.Lock()
defer t.muTorrent.Unlock()

st := new(state.TorrentStatus)

st.Stat = t.Stat
st.StatString = t.Stat.String()
st.Title = t.Title
st.Category = t.Category
st.Poster = t.Poster
st.Data = t.Data
st.Timestamp = t.Timestamp
st.TorrentSize = t.Size
st.BitRate = t.BitRate
st.DurationSeconds = t.DurationSeconds

if t.TorrentSpec != nil {
st.Hash = t.TorrentSpec.InfoHash.HexString()
}
if t.Torrent != nil {
st.Name = t.Torrent.Name()
st.Hash = t.Torrent.InfoHash().HexString()
st.DownloadSpeed = t.DownloadSpeed
st.UploadSpeed = t.UploadSpeed

tst := t.Torrent.Stats()
st.TotalPeers = tst.TotalPeers
st.ActivePeers = tst.ActivePeers
st.ConnectedSeeders = tst.ConnectedSeeders

if t.Torrent.Info() != nil {
st.TorrentSize = t.Torrent.Length()
}
}

return st
}

func (t *Torrent) CacheState() *cacheSt.CacheState {
if t.Torrent != nil && t.cache != nil {
st := t.cache.GetState()
Expand Down
43 changes: 39 additions & 4 deletions server/web/api/torrents.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"server/torrshash"
"strings"
"sync"
"time"

"server/dlna"
"server/log"
Expand All @@ -27,8 +29,16 @@ type torrReqJS struct {
Poster string `json:"poster,omitempty"`
Data string `json:"data,omitempty"`
SaveToDB bool `json:"save_to_db,omitempty"`
Filter string `json:"filter,omitempty"`
}

var (
listCache []*state.TorrentStatus
listCacheTime time.Time
listCacheMu sync.Mutex
listCacheTTL = 2 * time.Second
)

// torrents godoc
//
// @Summary Handle torrents informations
Expand Down Expand Up @@ -69,7 +79,7 @@ func torrents(c *gin.Context) {
}
case "list":
{
listTorrents(c)
listTorrents(c, req.Filter)
}
case "drop":
{
Expand Down Expand Up @@ -193,16 +203,41 @@ func remTorrent(req torrReqJS, c *gin.Context) {
c.Status(200)
}

func listTorrents(c *gin.Context) {
list := torr.ListTorrent()
func listTorrents(c *gin.Context, filter string) {
if filter == "" {
filter = "all"
}

// Return cached response for "all" filter if fresh enough
if filter == "all" {
listCacheMu.Lock()
if listCache != nil && time.Since(listCacheTime) < listCacheTTL {
cached := listCache
listCacheMu.Unlock()
c.JSON(200, cached)
return
}
listCacheMu.Unlock()
}

list := torr.ListTorrentFiltered(filter)
if len(list) == 0 {
c.JSON(200, []*state.TorrentStatus{})
return
}
var stats []*state.TorrentStatus
for _, tr := range list {
stats = append(stats, tr.Status())
stats = append(stats, tr.StatusLight())
}

// Cache "all" results
if filter == "all" {
listCacheMu.Lock()
listCache = stats
listCacheTime = time.Now()
listCacheMu.Unlock()
}

c.JSON(200, stats)
}

Expand Down