Skip to content

Commit 98526bc

Browse files
committed
progress, engine is getting quite limiting, will abandon the engine interface and remote engines after this commit
1 parent 7f698e3 commit 98526bc

25 files changed

Lines changed: 502 additions & 284 deletions

ct/engines/engine.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ type Engine interface {
1010
Name() string //name (lower(name)->id)
1111
NewConfig() interface{} //*Config object
1212
SetConfig(interface{}) error
13-
Magnet(uri string) error
14-
Torrents() <-chan *shared.Torrent
13+
NewTorrent(magnetURI string) error
14+
StartTorrent(infohash string) error
15+
StopTorrent(infohash string) error
16+
DeleteTorrent(infohash string) error
17+
StartFile(infohash, filepath string) error
18+
StopFile(infohash, filepath string) error
19+
GetTorrents() <-chan *shared.Torrent
1520
}
1621

1722
//TODO engines which require polling
1823
type EnginePoller interface {
24+
//Polls the status of all torrents and all files, passes updated torrents
25+
//down the Torrents channel
1926
Poll() error
20-
PollTorrent(*shared.Torrent) error
2127
}
2228

2329
//insert each of the cloud-torrent bundled engines

ct/engines/native/native.go

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package native
22

33
import (
4+
"encoding/hex"
45
"fmt"
6+
"log"
57
"time"
68

79
"github.com/anacrolix/torrent"
@@ -43,22 +45,90 @@ func (n *Native) SetConfig(obj interface{}) error {
4345
return nil
4446
}
4547

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)
4850
if err != nil {
4951
return err
5052
}
5153
return nil
5254
}
5355

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 {
55121
n.queue = make(chan *shared.Torrent)
56122
go n.pollTorrents()
57123
return n.queue
58124
}
59125

60126
func (n *Native) pollTorrents() {
61127
for {
128+
time.Sleep(time.Second)
129+
if n.client == nil {
130+
continue
131+
}
62132
for _, t := range n.client.Torrents() {
63133
//copy torrent info
64134
st := &shared.Torrent{
@@ -89,11 +159,22 @@ func (n *Native) pollTorrents() {
89159
//enqueue update
90160
n.queue <- st
91161
}
92-
time.Sleep(time.Second)
93162
}
94163
}
95164

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
97178
type config struct {
98179
torrent.Config
99180
TorrentDataOpener string `json:",omitempty"` //masks func

ct/search-handlers.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package ct
22

3+
//see github.com/jpillora/scraper for config specification
4+
//cloud-torrent uses "<id>-item" handlers
35
var defaultSearchConfig = []byte(`{
4-
"google": {
5-
"name": "Google Search",
6-
"url": "https://www.google.com/search?q={{q}}",
7-
"list": "#search ol > li",
8-
"result": {
9-
"title": "li > h3 a",
10-
"url": ["li > h3 a", "@href", "/^\\/url\\?q=([^&]+)/"]
11-
}
12-
},
136
"kat": {
147
"name": "Kickass Torrents",
158
"url": "https://kat.cr/usearch/{{query}}/{{page:1}}/?field=seeders&sorder=desc",

ct/server-api.go

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"log"
78
"net/http"
89
"regexp"
10+
"strings"
911

1012
"github.com/anacrolix/torrent"
1113
"github.com/anacrolix/torrent/metainfo"
@@ -64,7 +66,7 @@ func (s *Server) api(r *http.Request) error {
6466
}
6567

6668
//convert torrent bytes into magnet
67-
if action == "torrent" {
69+
if action == "torrentfile" {
6870
reader := bytes.NewBuffer(data)
6971
info, err := metainfo.Load(reader)
7072
if err != nil {
@@ -94,28 +96,51 @@ func (s *Server) api(r *http.Request) error {
9496
}
9597
case "magnet":
9698
uri := string(data)
97-
if err := e.Magnet(uri); err != nil {
99+
if err := e.NewTorrent(uri); err != nil {
98100
return fmt.Errorf("Magnet error: %s", err)
99101
}
100-
// case "list":
101-
// torrents, err := e.List()
102-
// if err != nil {
103-
// return fmt.Errorf("List error: %s", err)
104-
// }
105-
// for _, t := range torrents {
106-
// s.state.Torrents[eid][t.InfoHash] = t
107-
// }
108-
// s.rt.Update() //state change
109-
// case "fetch":
110-
// ih := string(data)
111-
// t, ok := s.state.Torrents[eid][ih]
112-
// if !ok {
113-
// return fmt.Errorf("Invalid torrent: %s", ih)
114-
// }
115-
// if err := e.Fetch(t); err != nil {
116-
// return fmt.Errorf("Fetch error: %s", err)
117-
// }
118-
// s.rt.Update() //state change
102+
case "torrent":
103+
cmd := strings.SplitN(string(data), ":", 2)
104+
if len(cmd) != 2 {
105+
return fmt.Errorf("Invalid request")
106+
}
107+
state := cmd[0]
108+
infohash := cmd[1]
109+
log.Printf("torrent api: %s -> %s", state, infohash)
110+
if state == "start" {
111+
if err := e.StartTorrent(infohash); err != nil {
112+
return err
113+
}
114+
} else if state == "stop" {
115+
if err := e.StopTorrent(infohash); err != nil {
116+
return err
117+
}
118+
} else if state == "delete" {
119+
if err := e.DeleteTorrent(infohash); err != nil {
120+
return err
121+
}
122+
} else {
123+
return fmt.Errorf("Invalid state: %s", state)
124+
}
125+
case "file":
126+
cmd := strings.SplitN(string(data), ":", 3)
127+
if len(cmd) != 3 {
128+
return fmt.Errorf("Invalid request")
129+
}
130+
state := cmd[0]
131+
infohash := cmd[1]
132+
filepath := cmd[2]
133+
if state == "start" {
134+
if err := e.StartFile(infohash, filepath); err != nil {
135+
return err
136+
}
137+
} else if state == "stop" {
138+
if err := e.StopFile(infohash, filepath); err != nil {
139+
return err
140+
}
141+
} else {
142+
return fmt.Errorf("Invalid state: %s", state)
143+
}
119144
default:
120145
return fmt.Errorf("Invalid action: %s", action)
121146
}

ct/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (s *Server) AddEngine(e engine.Engine) error {
120120
Config: e.NewConfig(),
121121
Torrents: torrents,
122122
}
123-
go s.torrentsWatch(torrents, e.Torrents())
123+
go s.torrentsWatch(torrents, e.GetTorrents())
124124
return nil
125125
}
126126

ct/shared/torrent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package shared
33
//cloud torrent specific torrent structs
44

55
type Torrent struct {
6+
InfoHash string //hash of torrent
67
Name string
78
Loaded bool
89
Progress int64
910
Size int64
10-
InfoHash string //hash of torrent
1111
Files []*File
1212
}
1313

static/files.go

Lines changed: 122 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/files/css/Lato/Lato-0.woff

-65 KB
Binary file not shown.

static/files/css/Lato/Lato-1.woff

-30.1 KB
Binary file not shown.

static/files/css/Lato/Lato-2.woff

-31.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)