-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorrent.go
More file actions
55 lines (47 loc) · 959 Bytes
/
torrent.go
File metadata and controls
55 lines (47 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package services
import (
"sync"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/anacrolix/torrent"
)
type Torrent struct {
t *torrent.Torrent
tcS *TorrentClient
miS *MetaInfo
mux sync.Mutex
err error
inited bool
}
func NewTorrent(tcS *TorrentClient, miS *MetaInfo) *Torrent {
return &Torrent{tcS: tcS, miS: miS, inited: false}
}
func (s *Torrent) Ready() bool {
return s.t != nil
}
func (s *Torrent) get() (*torrent.Torrent, error) {
log.Info("Initializing Torrent")
mi, err := s.miS.Get()
if err != nil {
return nil, err
}
cl, err := s.tcS.Get()
if err != nil {
return nil, err
}
t, err := cl.AddTorrent(mi)
if err != nil {
return nil, errors.Wrap(err, "Failed to add torrent")
}
return t, nil
}
func (s *Torrent) Get() (*torrent.Torrent, error) {
s.mux.Lock()
defer s.mux.Unlock()
if s.inited {
return s.t, s.err
}
s.t, s.err = s.get()
s.inited = true
return s.t, s.err
}