Add and remove torrents from web interface (#84)
This commit is contained in:
parent
02842b1917
commit
2f18213660
49 changed files with 996 additions and 1170 deletions
|
@ -26,13 +26,19 @@ type storage struct {
|
|||
|
||||
func newStorage(factories map[string]FsFactory) *storage {
|
||||
return &storage{
|
||||
files: make(map[string]File, 0),
|
||||
children: make(map[string]map[string]File, 0),
|
||||
filesystems: make(map[string]Filesystem, 0),
|
||||
files: make(map[string]File),
|
||||
children: make(map[string]map[string]File),
|
||||
filesystems: make(map[string]Filesystem),
|
||||
factories: factories,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *storage) Clear() {
|
||||
s.files = make(map[string]File)
|
||||
s.children = make(map[string]map[string]File)
|
||||
s.filesystems = make(map[string]Filesystem)
|
||||
}
|
||||
|
||||
func (s *storage) Has(path string) bool {
|
||||
path = clean(path)
|
||||
|
||||
|
@ -100,7 +106,7 @@ func (s *storage) createParent(p string, f File) error {
|
|||
}
|
||||
|
||||
if _, ok := s.children[base]; !ok {
|
||||
s.children[base] = make(map[string]File, 0)
|
||||
s.children[base] = make(map[string]File)
|
||||
}
|
||||
|
||||
if filename != "" {
|
||||
|
@ -118,7 +124,7 @@ func (s *storage) Children(path string) map[string]File {
|
|||
return out
|
||||
}
|
||||
|
||||
l := make(map[string]File, 0)
|
||||
l := make(map[string]File)
|
||||
for n, f := range s.children[path] {
|
||||
l[n] = f
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/distribyted/distribyted/iio"
|
||||
)
|
||||
|
@ -8,18 +10,45 @@ import (
|
|||
var _ Filesystem = &Torrent{}
|
||||
|
||||
type Torrent struct {
|
||||
ts []*torrent.Torrent
|
||||
s *storage
|
||||
mu sync.Mutex
|
||||
ts map[string]*torrent.Torrent
|
||||
s *storage
|
||||
loaded bool
|
||||
}
|
||||
|
||||
func NewTorrent(ts []*torrent.Torrent) *Torrent {
|
||||
func NewTorrent() *Torrent {
|
||||
return &Torrent{
|
||||
ts: ts,
|
||||
s: newStorage(SupportedFactories),
|
||||
ts: make(map[string]*torrent.Torrent),
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *Torrent) AddTorrent(t *torrent.Torrent) {
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
fs.loaded = false
|
||||
fs.ts[t.InfoHash().HexString()] = t
|
||||
}
|
||||
|
||||
func (fs *Torrent) RemoveTorrent(h string) {
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
fs.s.Clear()
|
||||
|
||||
fs.loaded = false
|
||||
|
||||
delete(fs.ts, h)
|
||||
}
|
||||
|
||||
func (fs *Torrent) load() {
|
||||
if fs.loaded {
|
||||
return
|
||||
}
|
||||
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
for _, t := range fs.ts {
|
||||
<-t.GotInfo()
|
||||
for _, file := range t.Files() {
|
||||
|
@ -27,6 +56,7 @@ func (fs *Torrent) load() {
|
|||
}
|
||||
}
|
||||
|
||||
fs.loaded = true
|
||||
}
|
||||
|
||||
func (fs *Torrent) Open(filename string) (File, error) {
|
||||
|
|
|
@ -25,7 +25,8 @@ func TestTorrentFilesystem(t *testing.T) {
|
|||
to, err := client.AddMagnet(testMagnet)
|
||||
require.NoError(err)
|
||||
|
||||
tfs := NewTorrent([]*torrent.Torrent{to})
|
||||
tfs := NewTorrent()
|
||||
tfs.AddTorrent(to)
|
||||
|
||||
files, err := tfs.ReadDir("/")
|
||||
require.NoError(err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue