Load torrent files from folder on start. (#149)

This commit is contained in:
Antonio Navarro Perez 2022-10-08 19:36:55 +02:00 committed by GitHub
parent b13425f370
commit fab0b8044a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 11 deletions

View file

@ -146,6 +146,7 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error {
}
cl := loader.NewConfig(conf.Routes)
fl := loader.NewFolder(conf.Routes)
ss := torrent.NewStats()
dbl, err := loader.NewDB(filepath.Join(conf.Torrent.MetadataFolder, "magnetdb"))
@ -153,7 +154,7 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error {
return fmt.Errorf("error starting magnet database: %w", err)
}
ts := torrent.NewService(cl, dbl, ss, c, conf.Torrent.AddTimeout, conf.Torrent.ReadTimeout)
ts := torrent.NewService([]loader.Loader{cl, fl}, dbl, ss, c, conf.Torrent.AddTimeout, conf.Torrent.ReadTimeout)
var mh *fuse.Handler
if conf.Fuse != nil {

View file

@ -48,6 +48,7 @@ type FuseGlobal struct {
type Route struct {
Name string `yaml:"name"`
Torrents []*Torrent `yaml:"torrents"`
TorrentFolder string `yaml:"torrent_folder"`
}
type Server struct {

View file

@ -69,6 +69,8 @@ log:
# List of folders where torrents will be mounted as a filesystem.
routes:
- name: multimedia
# Adding a folder will load all torrents on it:
# torrent_folder: "/path/to/torrent/folder"
torrents:
# You can also add torrents from a specific path
# - torrent_path: /path/to/torrent/file.torrent

View file

@ -17,7 +17,6 @@ func NewConfig(r []*config.Route) *Config {
func (l *Config) ListMagnets() (map[string][]string, error) {
out := make(map[string][]string)
for _, r := range l.c {
out[r.Name] = make([]string, 0)
for _, t := range r.Torrents {
if t.MagnetURI == "" {
continue
@ -33,7 +32,6 @@ func (l *Config) ListMagnets() (map[string][]string, error) {
func (l *Config) ListTorrentPaths() (map[string][]string, error) {
out := make(map[string][]string)
for _, r := range l.c {
out[r.Name] = make([]string, 0)
for _, t := range r.Torrents {
if t.TorrentPath == "" {
continue

55
torrent/loader/folder.go Normal file
View file

@ -0,0 +1,55 @@
package loader
import (
"io/fs"
"path"
"path/filepath"
"github.com/distribyted/distribyted/config"
)
var _ Loader = &Folder{}
type Folder struct {
c []*config.Route
}
func NewFolder(r []*config.Route) *Folder {
return &Folder{
c: r,
}
}
func (f *Folder) ListMagnets() (map[string][]string, error) {
return nil, nil
}
func (f *Folder) ListTorrentPaths() (map[string][]string, error) {
out := make(map[string][]string)
for _, r := range f.c {
if r.TorrentFolder == "" {
continue
}
err := filepath.WalkDir(r.TorrentFolder, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if path.Ext(p) == ".torrent" {
out[r.Name] = append(out[r.Name], p)
}
return nil
})
if err != nil {
return nil, err
}
}
return out, nil
}

View file

@ -23,21 +23,21 @@ type Service struct {
mu sync.Mutex
fss map[string]fs.Filesystem
cfgLoader loader.Loader
loaders []loader.Loader
db loader.LoaderAdder
log zerolog.Logger
addTimeout, readTimeout int
}
func NewService(cfg loader.Loader, db loader.LoaderAdder, stats *Stats, c *torrent.Client, addTimeout, readTimeout int) *Service {
func NewService(loaders []loader.Loader, db loader.LoaderAdder, stats *Stats, c *torrent.Client, addTimeout, readTimeout int) *Service {
l := log.Logger.With().Str("component", "torrent-service").Logger()
return &Service{
log: l,
s: stats,
c: c,
fss: make(map[string]fs.Filesystem),
cfgLoader: cfg,
loaders: loaders,
db: db,
addTimeout: addTimeout,
readTimeout: readTimeout,
@ -47,9 +47,11 @@ func NewService(cfg loader.Loader, db loader.LoaderAdder, stats *Stats, c *torre
func (s *Service) Load() (map[string]fs.Filesystem, error) {
// Load from config
s.log.Info().Msg("adding torrents from configuration")
if err := s.load(s.cfgLoader); err != nil {
for _, loader := range s.loaders {
if err := s.load(loader); err != nil {
return nil, err
}
}
// Load from DB
s.log.Info().Msg("adding torrents from database")