|
1 | 1 | package native |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
4 | 5 | "fmt" |
| 6 | + "log" |
5 | 7 | "time" |
6 | 8 |
|
7 | 9 | "github.com/anacrolix/torrent" |
@@ -43,22 +45,90 @@ func (n *Native) SetConfig(obj interface{}) error { |
43 | 45 | return nil |
44 | 46 | } |
45 | 47 |
|
46 | | -func (n *Native) Magnet(uri string) error { |
47 | | - _, err := n.client.AddMagnet(uri) |
| 48 | +func (n *Native) NewTorrent(magnetURI string) error { |
| 49 | + _, err := n.client.AddMagnet(magnetURI) |
48 | 50 | if err != nil { |
49 | 51 | return err |
50 | 52 | } |
51 | 53 | return nil |
52 | 54 | } |
53 | 55 |
|
54 | | -func (n *Native) Torrents() <-chan *shared.Torrent { |
| 56 | +func (n *Native) getTorrent(infohash string) (torrent.Torrent, error) { |
| 57 | + var t torrent.Torrent |
| 58 | + ih, err := str2ih(infohash) |
| 59 | + if err != nil { |
| 60 | + return t, err |
| 61 | + } |
| 62 | + t, ok := n.client.Torrent(ih) |
| 63 | + if !ok { |
| 64 | + return t, fmt.Errorf("Missing torrent %x", ih) |
| 65 | + } |
| 66 | + return t, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (n *Native) StartTorrent(infohash string) error { |
| 70 | + log.Printf("start %s", infohash) |
| 71 | + t, err := n.getTorrent(infohash) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + t.DownloadAll() |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (n *Native) StopTorrent(infohash string) error { |
| 80 | + return fmt.Errorf("Unsupported") |
| 81 | +} |
| 82 | + |
| 83 | +func (n *Native) DeleteTorrent(infohash string) error { |
| 84 | + t, err := n.getTorrent(infohash) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + t.Drop() |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (n *Native) getFile(infohash, filepath string) (file torrent.File, err error) { |
| 93 | + t, err := n.getTorrent(infohash) |
| 94 | + if err != nil { |
| 95 | + return |
| 96 | + } |
| 97 | + for _, f := range t.Files() { |
| 98 | + if filepath == f.Path() { |
| 99 | + file = f |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + err = fmt.Errorf("File not found") |
| 104 | + return |
| 105 | +} |
| 106 | + |
| 107 | +func (n *Native) StartFile(infohash, filepath string) error { |
| 108 | + f, err := n.getFile(infohash, filepath) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + f.PrioritizeRegion(0, f.Length()) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (n *Native) StopFile(infohash, filepath string) error { |
| 117 | + return fmt.Errorf("Unsupported") |
| 118 | +} |
| 119 | + |
| 120 | +func (n *Native) GetTorrents() <-chan *shared.Torrent { |
55 | 121 | n.queue = make(chan *shared.Torrent) |
56 | 122 | go n.pollTorrents() |
57 | 123 | return n.queue |
58 | 124 | } |
59 | 125 |
|
60 | 126 | func (n *Native) pollTorrents() { |
61 | 127 | for { |
| 128 | + time.Sleep(time.Second) |
| 129 | + if n.client == nil { |
| 130 | + continue |
| 131 | + } |
62 | 132 | for _, t := range n.client.Torrents() { |
63 | 133 | //copy torrent info |
64 | 134 | st := &shared.Torrent{ |
@@ -89,11 +159,22 @@ func (n *Native) pollTorrents() { |
89 | 159 | //enqueue update |
90 | 160 | n.queue <- st |
91 | 161 | } |
92 | | - time.Sleep(time.Second) |
93 | 162 | } |
94 | 163 | } |
95 | 164 |
|
96 | | -//mask over TorrentDataOpener to allow torrent.Config to be parsed |
| 165 | +func str2ih(str string) (torrent.InfoHash, error) { |
| 166 | + var ih torrent.InfoHash |
| 167 | + n, err := hex.Decode(ih[:], []byte(str)) |
| 168 | + if err != nil { |
| 169 | + return ih, fmt.Errorf("Invalid hex string") |
| 170 | + } |
| 171 | + if n != 20 { |
| 172 | + return ih, fmt.Errorf("Invalid length") |
| 173 | + } |
| 174 | + return ih, nil |
| 175 | +} |
| 176 | + |
| 177 | +//mask over TorrentDataOpener to allow torrent.Config to be marshalled |
97 | 178 | type config struct { |
98 | 179 | torrent.Config |
99 | 180 | TorrentDataOpener string `json:",omitempty"` //masks func |
|
0 commit comments