2023-12-31 22:54:55 +00:00
|
|
|
package service
|
2023-10-16 09:18:40 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-21 23:15:39 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"time"
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-12-31 22:54:55 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/storage"
|
2023-10-16 09:18:40 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2023-12-21 23:15:39 +00:00
|
|
|
"github.com/anacrolix/torrent/types"
|
2023-10-16 09:18:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2023-12-25 22:11:03 +00:00
|
|
|
c *torrent.Client
|
2024-01-07 17:09:56 +00:00
|
|
|
rep storage.ExlcudedFiles
|
2023-10-16 09:18:40 +00:00
|
|
|
|
|
|
|
// stats *Stats
|
2023-12-21 23:15:39 +00:00
|
|
|
DefaultPriority types.PiecePriority
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-12-21 23:15:39 +00:00
|
|
|
log *slog.Logger
|
2023-10-16 09:18:40 +00:00
|
|
|
addTimeout, readTimeout int
|
|
|
|
}
|
|
|
|
|
2024-01-07 17:09:56 +00:00
|
|
|
func NewService(c *torrent.Client, rep storage.ExlcudedFiles, addTimeout, readTimeout int) *Service {
|
2023-12-21 23:15:39 +00:00
|
|
|
l := slog.With("component", "torrent-service")
|
2023-10-16 09:18:40 +00:00
|
|
|
return &Service{
|
2023-12-21 23:15:39 +00:00
|
|
|
log: l,
|
|
|
|
c: c,
|
|
|
|
DefaultPriority: types.PiecePriorityNone,
|
2023-12-25 22:11:03 +00:00
|
|
|
rep: rep,
|
2023-10-16 09:18:40 +00:00
|
|
|
// stats: newStats(), // TODO persistent
|
|
|
|
addTimeout: addTimeout,
|
|
|
|
readTimeout: readTimeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vfs.FsFactory = (*Service)(nil).NewTorrentFs
|
|
|
|
|
2024-01-07 17:09:56 +00:00
|
|
|
func (s *Service) NewTorrent(f vfs.File) (*torrent.Torrent, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*time.Duration(s.addTimeout))
|
|
|
|
defer cancel()
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
mi, err := metainfo.Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, ok := s.c.Torrent(mi.HashInfoBytes())
|
|
|
|
if !ok {
|
|
|
|
t, err = s.c.AddTorrent(mi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("creating torrent fs timed out")
|
|
|
|
case <-t.GotInfo():
|
|
|
|
}
|
|
|
|
for _, f := range t.Files() {
|
|
|
|
f.SetPriority(s.DefaultPriority)
|
|
|
|
}
|
|
|
|
t.AllowDataDownload()
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
func (s *Service) NewTorrentFs(f vfs.File) (vfs.Filesystem, error) {
|
2023-12-21 23:15:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*time.Duration(s.addTimeout))
|
|
|
|
defer cancel()
|
2023-10-16 09:18:40 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
mi, err := metainfo.Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-21 23:15:39 +00:00
|
|
|
|
|
|
|
t, ok := s.c.Torrent(mi.HashInfoBytes())
|
|
|
|
if !ok {
|
|
|
|
t, err = s.c.AddTorrent(mi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("creating torrent fs timed out")
|
|
|
|
case <-t.GotInfo():
|
|
|
|
}
|
|
|
|
for _, f := range t.Files() {
|
|
|
|
f.SetPriority(s.DefaultPriority)
|
|
|
|
}
|
|
|
|
t.AllowDataDownload()
|
2023-10-18 09:52:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 22:11:03 +00:00
|
|
|
return vfs.NewTorrentFs(t, s.rep, s.readTimeout), nil
|
2023-10-16 09:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Stats() (*Stats, error) {
|
|
|
|
return &Stats{}, nil
|
|
|
|
}
|
2024-01-07 17:09:56 +00:00
|
|
|
|
|
|
|
func (s *Service) GetStats() torrent.ConnStats {
|
|
|
|
return s.c.ConnStats()
|
|
|
|
}
|