package controller import ( "slices" "strings" "git.kmsign.ru/royalcat/tstor/src/host/store" "github.com/anacrolix/torrent" ) type Torrent struct { torrentFilePath string t *torrent.Torrent rep *store.ExlcudedFiles } func NewTorrent(t *torrent.Torrent, rep *store.ExlcudedFiles) *Torrent { return &Torrent{t: t, rep: rep} } func (s *Torrent) TorrentFilePath() string { return s.torrentFilePath } func (s *Torrent) Torrent() *torrent.Torrent { return s.t } func (s *Torrent) Name() string { <-s.t.GotInfo() if name := s.t.Name(); name != "" { return name } return s.InfoHash() } 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() } func (s *Torrent) Files() ([]*torrent.File, error) { excludedFiles, err := s.rep.ExcludedFiles(s.t.InfoHash()) if err != nil { return nil, err } <-s.t.GotInfo() files := s.t.Files() files = slices.DeleteFunc(files, func(file *torrent.File) bool { p := file.Path() if strings.Contains(p, "/.pad/") { return false } if !slices.Contains(excludedFiles, p) { return false } return true }) for _, tf := range files { s.isFileComplete(tf.BeginPieceIndex(), tf.EndPieceIndex()) } return files, nil } 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 } func (s *Torrent) ExcludedFiles() ([]*torrent.File, error) { excludedFiles, err := s.rep.ExcludedFiles(s.t.InfoHash()) if err != nil { return nil, err } <-s.t.GotInfo() files := s.t.Files() files = slices.DeleteFunc(files, func(file *torrent.File) bool { p := file.Path() if strings.Contains(p, "/.pad/") { return false } if slices.Contains(excludedFiles, p) { return false } return true }) return files, nil } func (s *Torrent) ExcludeFile(f *torrent.File) error { return s.rep.ExcludeFile(f) } func (s *Torrent) ValidateTorrent() error { <-s.t.GotInfo() s.t.VerifyData() return nil }