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) cl := loader.NewConfig(conf.Routes)
fl := loader.NewFolder(conf.Routes)
ss := torrent.NewStats() ss := torrent.NewStats()
dbl, err := loader.NewDB(filepath.Join(conf.Torrent.MetadataFolder, "magnetdb")) 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) 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 var mh *fuse.Handler
if conf.Fuse != nil { if conf.Fuse != nil {

View file

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

View file

@ -69,6 +69,8 @@ log:
# List of folders where torrents will be mounted as a filesystem. # List of folders where torrents will be mounted as a filesystem.
routes: routes:
- name: multimedia - name: multimedia
# Adding a folder will load all torrents on it:
# torrent_folder: "/path/to/torrent/folder"
torrents: torrents:
# You can also add torrents from a specific path # You can also add torrents from a specific path
# - torrent_path: /path/to/torrent/file.torrent # - 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) { func (l *Config) ListMagnets() (map[string][]string, error) {
out := make(map[string][]string) out := make(map[string][]string)
for _, r := range l.c { for _, r := range l.c {
out[r.Name] = make([]string, 0)
for _, t := range r.Torrents { for _, t := range r.Torrents {
if t.MagnetURI == "" { if t.MagnetURI == "" {
continue continue
@ -33,7 +32,6 @@ func (l *Config) ListMagnets() (map[string][]string, error) {
func (l *Config) ListTorrentPaths() (map[string][]string, error) { func (l *Config) ListTorrentPaths() (map[string][]string, error) {
out := make(map[string][]string) out := make(map[string][]string)
for _, r := range l.c { for _, r := range l.c {
out[r.Name] = make([]string, 0)
for _, t := range r.Torrents { for _, t := range r.Torrents {
if t.TorrentPath == "" { if t.TorrentPath == "" {
continue 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 mu sync.Mutex
fss map[string]fs.Filesystem fss map[string]fs.Filesystem
cfgLoader loader.Loader loaders []loader.Loader
db loader.LoaderAdder db loader.LoaderAdder
log zerolog.Logger log zerolog.Logger
addTimeout, readTimeout int 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() l := log.Logger.With().Str("component", "torrent-service").Logger()
return &Service{ return &Service{
log: l, log: l,
s: stats, s: stats,
c: c, c: c,
fss: make(map[string]fs.Filesystem), fss: make(map[string]fs.Filesystem),
cfgLoader: cfg, loaders: loaders,
db: db, db: db,
addTimeout: addTimeout, addTimeout: addTimeout,
readTimeout: readTimeout, 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) { func (s *Service) Load() (map[string]fs.Filesystem, error) {
// Load from config // Load from config
s.log.Info().Msg("adding torrents from configuration") s.log.Info().Msg("adding torrents from configuration")
if err := s.load(s.cfgLoader); err != nil { for _, loader := range s.loaders {
return nil, err if err := s.load(loader); err != nil {
return nil, err
}
} }
// Load from DB // Load from DB