diff --git a/cmd/distribyted/main.go b/cmd/distribyted/main.go index a97eab8..87de4d4 100644 --- a/cmd/distribyted/main.go +++ b/cmd/distribyted/main.go @@ -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 { diff --git a/config/model.go b/config/model.go index 1c57292..c227cd5 100644 --- a/config/model.go +++ b/config/model.go @@ -46,8 +46,9 @@ type FuseGlobal struct { } type Route struct { - Name string `yaml:"name"` - Torrents []*Torrent `yaml:"torrents"` + Name string `yaml:"name"` + Torrents []*Torrent `yaml:"torrents"` + TorrentFolder string `yaml:"torrent_folder"` } type Server struct { diff --git a/templates/config_template.yaml b/templates/config_template.yaml index 440885a..abd4d5e 100644 --- a/templates/config_template.yaml +++ b/templates/config_template.yaml @@ -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 diff --git a/torrent/loader/config.go b/torrent/loader/config.go index a49dcd2..ea9f202 100644 --- a/torrent/loader/config.go +++ b/torrent/loader/config.go @@ -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 diff --git a/torrent/loader/folder.go b/torrent/loader/folder.go new file mode 100644 index 0000000..f79e5d5 --- /dev/null +++ b/torrent/loader/folder.go @@ -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 +} diff --git a/torrent/service.go b/torrent/service.go index 1684de3..8aa79be 100644 --- a/torrent/service.go +++ b/torrent/service.go @@ -23,21 +23,21 @@ type Service struct { mu sync.Mutex fss map[string]fs.Filesystem - cfgLoader loader.Loader - db loader.LoaderAdder + 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,8 +47,10 @@ 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 { - return nil, err + for _, loader := range s.loaders { + if err := s.load(loader); err != nil { + return nil, err + } } // Load from DB