2024-01-28 20:22:49 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2024-03-17 21:00:34 +00:00
|
|
|
"context"
|
2024-01-28 20:22:49 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/store"
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Torrent struct {
|
|
|
|
torrentFilePath string
|
|
|
|
t *torrent.Torrent
|
2024-03-17 21:00:34 +00:00
|
|
|
rep *store.FilesMappings
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
func NewTorrent(t *torrent.Torrent, rep *store.FilesMappings) *Torrent {
|
2024-01-28 20:22:49 +00:00
|
|
|
return &Torrent{t: t, rep: rep}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) TorrentFilePath() string {
|
|
|
|
return s.torrentFilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) Torrent() *torrent.Torrent {
|
|
|
|
return s.t
|
|
|
|
}
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
func (c *Torrent) Name() string {
|
|
|
|
<-c.t.GotInfo()
|
|
|
|
if name := c.t.Name(); name != "" {
|
2024-02-22 22:54:56 +00:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
return c.InfoHash()
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) InfoHash() string {
|
|
|
|
<-s.t.GotInfo()
|
|
|
|
return s.t.InfoHash().HexString()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) BytesCompleted() int64 {
|
|
|
|
<-s.t.GotInfo()
|
|
|
|
return s.t.BytesCompleted()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) BytesMissing() int64 {
|
|
|
|
<-s.t.GotInfo()
|
|
|
|
return s.t.BytesMissing()
|
|
|
|
}
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
func (s *Torrent) Length() int64 {
|
|
|
|
<-s.t.GotInfo()
|
|
|
|
return s.t.Length()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Torrent) Files(ctx context.Context) ([]*torrent.File, error) {
|
|
|
|
fileMappings, err := s.rep.FileMappings(ctx, s.t.InfoHash())
|
2024-01-28 20:22:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-27 11:00:34 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case <-s.t.GotInfo():
|
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:49 +00:00
|
|
|
files := s.t.Files()
|
|
|
|
files = slices.DeleteFunc(files, func(file *torrent.File) bool {
|
2024-04-27 11:00:34 +00:00
|
|
|
if file == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:49 +00:00
|
|
|
p := file.Path()
|
|
|
|
if strings.Contains(p, "/.pad/") {
|
2024-03-17 21:00:34 +00:00
|
|
|
return true
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|
2024-03-17 21:00:34 +00:00
|
|
|
if target, ok := fileMappings[p]; ok && target == "" {
|
|
|
|
return true
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|
2024-03-17 21:00:34 +00:00
|
|
|
return false
|
2024-01-28 20:22:49 +00:00
|
|
|
})
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Map[T, U any](ts []T, f func(T) U) []U {
|
|
|
|
us := make([]U, len(ts))
|
|
|
|
for i := range ts {
|
|
|
|
us[i] = f(ts[i])
|
2024-02-22 22:54:56 +00:00
|
|
|
}
|
2024-03-17 21:00:34 +00:00
|
|
|
return us
|
|
|
|
}
|
2024-02-22 22:54:56 +00:00
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
func (s *Torrent) ExcludeFile(ctx context.Context, f *torrent.File) error {
|
|
|
|
return s.rep.ExcludeFile(ctx, f)
|
2024-01-28 20:22:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 22:54:56 +00:00
|
|
|
func (s *Torrent) isFileComplete(startIndex int, endIndex int) bool {
|
|
|
|
for i := startIndex; i < endIndex; i++ {
|
|
|
|
if !s.t.Piece(i).State().Complete {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:49 +00:00
|
|
|
func (s *Torrent) ValidateTorrent() error {
|
|
|
|
<-s.t.GotInfo()
|
|
|
|
s.t.VerifyData()
|
|
|
|
return nil
|
|
|
|
}
|