mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-20 05:56:10 +05:00
add cache
This commit is contained in:
@@ -61,7 +61,7 @@ type TorrentStatus struct {
|
|||||||
PiecesDirtiedGood int64 `json:"pieces_dirtied_good,omitempty"`
|
PiecesDirtiedGood int64 `json:"pieces_dirtied_good,omitempty"`
|
||||||
PiecesDirtiedBad int64 `json:"pieces_dirtied_bad,omitempty"`
|
PiecesDirtiedBad int64 `json:"pieces_dirtied_bad,omitempty"`
|
||||||
|
|
||||||
FileStats []TorrentFileStat `json:"file_stats,omitempty"`
|
FileStats []*TorrentFileStat `json:"file_stats,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TorrentFileStat struct {
|
type TorrentFileStat struct {
|
||||||
|
|||||||
18
src/server/torr/storage/state/state.go
Normal file
18
src/server/torr/storage/state/state.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package state
|
||||||
|
|
||||||
|
type CacheState struct {
|
||||||
|
Hash string
|
||||||
|
Capacity int64
|
||||||
|
Filled int64
|
||||||
|
PiecesLength int64
|
||||||
|
PiecesCount int
|
||||||
|
DownloadSpeed float64
|
||||||
|
Pieces map[int]ItemState
|
||||||
|
}
|
||||||
|
|
||||||
|
type ItemState struct {
|
||||||
|
Id int
|
||||||
|
Length int64
|
||||||
|
Size int64
|
||||||
|
Completed bool
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"server/log"
|
"server/log"
|
||||||
"server/settings"
|
"server/settings"
|
||||||
|
"server/torr/storage/state"
|
||||||
"server/torr/utils"
|
"server/torr/utils"
|
||||||
|
|
||||||
"github.com/anacrolix/torrent"
|
"github.com/anacrolix/torrent"
|
||||||
@@ -53,7 +54,7 @@ func NewCache(capacity int64, storage *Storage) *Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
||||||
log.TLogln("Create cache for:", info.Name)
|
log.TLogln("Create cache for:", info.Name, hash.HexString())
|
||||||
if c.capacity == 0 {
|
if c.capacity == 0 {
|
||||||
c.capacity = info.PieceLength * 6
|
c.capacity = info.PieceLength * 6
|
||||||
}
|
}
|
||||||
@@ -204,3 +205,35 @@ func (c *Cache) AdjustRA(readahead int64) {
|
|||||||
r.SetReadahead(readahead)
|
r.SetReadahead(readahead)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cache) GetState() *state.CacheState {
|
||||||
|
cState := new(state.CacheState)
|
||||||
|
cState.Capacity = c.capacity
|
||||||
|
cState.PiecesLength = c.pieceLength
|
||||||
|
cState.PiecesCount = c.pieceCount
|
||||||
|
cState.Hash = c.hash.HexString()
|
||||||
|
|
||||||
|
stats := make(map[int]state.ItemState, 0)
|
||||||
|
c.muPiece.Lock()
|
||||||
|
var fill int64 = 0
|
||||||
|
for _, p := range c.pieces {
|
||||||
|
if p.Size > 0 {
|
||||||
|
fill += p.Length
|
||||||
|
stats[p.Id] = state.ItemState{
|
||||||
|
Id: p.Id,
|
||||||
|
Size: p.Size,
|
||||||
|
Length: p.Length,
|
||||||
|
Completed: p.complete,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.filled = fill
|
||||||
|
c.muPiece.Unlock()
|
||||||
|
cState.Filled = c.filled
|
||||||
|
cState.Pieces = stats
|
||||||
|
return cState
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cache) GetCapacity() int64 {
|
||||||
|
return c.capacity
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"server/log"
|
"server/log"
|
||||||
"server/settings"
|
"server/settings"
|
||||||
"server/torr/state"
|
"server/torr/state"
|
||||||
|
cacheSt "server/torr/storage/state"
|
||||||
"server/torr/utils"
|
"server/torr/utils"
|
||||||
utils2 "server/utils"
|
utils2 "server/utils"
|
||||||
|
|
||||||
@@ -258,10 +259,10 @@ func (t *Torrent) Preload(index int, size int64) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if index < 0 || index >= len(t.Files()) {
|
file := t.findFileIndex(index)
|
||||||
index = 0
|
if file == nil {
|
||||||
|
file = t.Files()[0]
|
||||||
}
|
}
|
||||||
file := t.Files()[index]
|
|
||||||
|
|
||||||
buff5mb := int64(5 * 1024 * 1024)
|
buff5mb := int64(5 * 1024 * 1024)
|
||||||
startPreloadLength := size
|
startPreloadLength := size
|
||||||
@@ -393,7 +394,7 @@ func (t *Torrent) Status() *state.TorrentStatus {
|
|||||||
return files[i].Path() < files[j].Path()
|
return files[i].Path() < files[j].Path()
|
||||||
})
|
})
|
||||||
for i, f := range files {
|
for i, f := range files {
|
||||||
st.FileStats = append(st.FileStats, state.TorrentFileStat{
|
st.FileStats = append(st.FileStats, &state.TorrentFileStat{
|
||||||
Id: i + 1,
|
Id: i + 1,
|
||||||
Path: f.Path(),
|
Path: f.Path(),
|
||||||
Length: f.Length(),
|
Length: f.Length(),
|
||||||
@@ -403,3 +404,32 @@ func (t *Torrent) Status() *state.TorrentStatus {
|
|||||||
}
|
}
|
||||||
return st
|
return st
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Torrent) CacheState() *cacheSt.CacheState {
|
||||||
|
if t.Torrent != nil && t.cache != nil {
|
||||||
|
st := t.cache.GetState()
|
||||||
|
st.DownloadSpeed = t.DownloadSpeed
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Torrent) findFileIndex(index int) *torrent.File {
|
||||||
|
st := t.Status()
|
||||||
|
var stFile *state.TorrentFileStat
|
||||||
|
for _, f := range st.FileStats {
|
||||||
|
if index == f.Id {
|
||||||
|
stFile = f
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if stFile == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, file := range t.Files() {
|
||||||
|
if file.Path() == stFile.Path {
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -78,8 +78,8 @@ func GetMimeType(filename string) string {
|
|||||||
return "*/*"
|
return "*/*"
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPlayableFiles(st state.TorrentStatus) []state.TorrentFileStat {
|
func GetPlayableFiles(st state.TorrentStatus) []*state.TorrentFileStat {
|
||||||
files := make([]state.TorrentFileStat, 0)
|
files := make([]*state.TorrentFileStat, 0)
|
||||||
for _, f := range st.FileStats {
|
for _, f := range st.FileStats {
|
||||||
if GetMimeType(f.Path) != "*/*" {
|
if GetMimeType(f.Path) != "*/*" {
|
||||||
files = append(files, f)
|
files = append(files, f)
|
||||||
|
|||||||
51
src/server/web/api/cache.go
Normal file
51
src/server/web/api/cache.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,8 @@ func SetupRoute(route *gin.Engine) {
|
|||||||
route.POST("/torrents", torrents)
|
route.POST("/torrents", torrents)
|
||||||
route.POST("/torrent/upload", torrentUpload)
|
route.POST("/torrent/upload", torrentUpload)
|
||||||
|
|
||||||
|
route.POST("/cache", cache)
|
||||||
|
|
||||||
route.GET("/stream", stream)
|
route.GET("/stream", stream)
|
||||||
route.GET("/stream/*fname", stream)
|
route.GET("/stream/*fname", stream)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user