mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
refactor and to go mod
This commit is contained in:
151
server/torr/apihelper.go
Normal file
151
server/torr/apihelper.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package torr
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"server/log"
|
||||
sets "server/settings"
|
||||
)
|
||||
|
||||
var (
|
||||
bts *BTServer
|
||||
)
|
||||
|
||||
func InitApiHelper(bt *BTServer) {
|
||||
bts = bt
|
||||
}
|
||||
|
||||
func AddTorrent(spec *torrent.TorrentSpec, title, poster string, data string) (*Torrent, error) {
|
||||
torr, err := NewTorrent(spec, bts)
|
||||
if err != nil {
|
||||
log.TLogln("error add torrent:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
torDB := GetTorrentDB(spec.InfoHash)
|
||||
|
||||
if torr.Title == "" {
|
||||
torr.Title = title
|
||||
if title == "" && torDB != nil {
|
||||
torr.Title = torDB.Title
|
||||
}
|
||||
}
|
||||
if torr.Poster == "" {
|
||||
torr.Poster = poster
|
||||
if torr.Poster == "" && torDB != nil {
|
||||
torr.Poster = torDB.Poster
|
||||
}
|
||||
}
|
||||
if torr.Data == "" {
|
||||
torr.Data = data
|
||||
if torr.Data == "" && torDB != nil {
|
||||
torr.Data = torDB.Data
|
||||
}
|
||||
}
|
||||
|
||||
return torr, nil
|
||||
}
|
||||
|
||||
func SaveTorrentToDB(torr *Torrent) {
|
||||
log.TLogln("save to db:", torr.Hash())
|
||||
AddTorrentDB(torr)
|
||||
}
|
||||
|
||||
func GetTorrent(hashHex string) *Torrent {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
tor := bts.GetTorrent(hash)
|
||||
if tor != nil {
|
||||
tor.AddExpiredTime(time.Minute)
|
||||
return tor
|
||||
}
|
||||
|
||||
tr := GetTorrentDB(hash)
|
||||
if tr != nil {
|
||||
tor = tr
|
||||
go func() {
|
||||
tr, _ := NewTorrent(tor.TorrentSpec, bts)
|
||||
if tr != nil {
|
||||
tr.Title = tor.Title
|
||||
tr.Poster = tor.Poster
|
||||
tr.Size = tor.Size
|
||||
tr.Timestamp = tor.Timestamp
|
||||
tr.GotInfo()
|
||||
}
|
||||
}()
|
||||
}
|
||||
return tor
|
||||
}
|
||||
|
||||
func RemTorrent(hashHex string) {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
bts.RemoveTorrent(hash)
|
||||
RemTorrentDB(hash)
|
||||
}
|
||||
|
||||
func ListTorrent() []*Torrent {
|
||||
btlist := bts.ListTorrents()
|
||||
dblist := ListTorrentsDB()
|
||||
|
||||
for hash, t := range dblist {
|
||||
if _, ok := btlist[hash]; !ok {
|
||||
btlist[hash] = t
|
||||
}
|
||||
}
|
||||
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
|
||||
} else {
|
||||
return ret[i].Title > ret[j].Title
|
||||
}
|
||||
})
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func DropTorrent(hashHex string) {
|
||||
hash := metainfo.NewHashFromHex(hashHex)
|
||||
bts.RemoveTorrent(hash)
|
||||
}
|
||||
|
||||
func SetSettings(set *sets.BTSets) {
|
||||
if sets.ReadOnly {
|
||||
return
|
||||
}
|
||||
bts.Disconnect()
|
||||
sets.SetBTSets(set)
|
||||
bts.Connect()
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
bts.Disconnect()
|
||||
sets.CloseDB()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func WriteStatus(w io.Writer) {
|
||||
bts.client.WriteStatus(w)
|
||||
}
|
||||
|
||||
func Preload(torr *Torrent, index int) {
|
||||
if !sets.BTsets.PreloadBuffer {
|
||||
size := int64(32 * 1024 * 1024)
|
||||
if size > sets.BTsets.CacheSize {
|
||||
size = sets.BTsets.CacheSize
|
||||
}
|
||||
torr.Preload(index, size)
|
||||
} else {
|
||||
size := int64(float32(sets.BTsets.ReaderReadAHead) / 100.0 * float32(sets.BTsets.CacheSize))
|
||||
torr.Preload(index, size)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user