New Expandable file formats ()

This commit is contained in:
Antonio Navarro Perez 2021-11-29 11:07:54 +01:00 committed by GitHub
parent 15c72452de
commit 8d9a9281c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 808 additions and 346 deletions

View file

@ -17,6 +17,7 @@ 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
@ -32,6 +33,7 @@ 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

View file

@ -26,20 +26,20 @@ type Service struct {
cfgLoader loader.Loader
db loader.LoaderAdder
log zerolog.Logger
timeout int
log zerolog.Logger
addTimeout, readTimeout int
}
func NewService(cfg loader.Loader, db loader.LoaderAdder, stats *Stats, c *torrent.Client, timeout int) *Service {
func NewService(cfg 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,
db: db,
timeout: timeout,
log: l,
s: stats,
c: c,
fss: make(map[string]fs.Filesystem),
cfgLoader: cfg,
db: db,
addTimeout: addTimeout,
}
}
@ -61,6 +61,7 @@ func (s *Service) load(l loader.Loader) error {
return err
}
for r, ms := range list {
s.addRoute(r)
for _, m := range ms {
if err := s.addMagnet(r, m); err != nil {
return err
@ -73,6 +74,7 @@ func (s *Service) load(l loader.Loader) error {
return err
}
for r, ms := range list {
s.addRoute(r)
for _, p := range ms {
if err := s.addTorrentPath(r, p); err != nil {
return err
@ -113,12 +115,25 @@ func (s *Service) addMagnet(r, m string) error {
}
func (s *Service) addRoute(r string) {
s.s.AddRoute(r)
// Add to filesystems
folder := path.Join("/", r)
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.fss[folder]
if !ok {
s.fss[folder] = fs.NewTorrent(s.readTimeout)
}
}
func (s *Service) addTorrent(r string, t *torrent.Torrent) error {
// only get info if name is not available
if t.Info() == nil {
s.log.Info().Str("hash", t.InfoHash().String()).Msg("getting torrent info")
select {
case <-time.After(time.Duration(s.timeout) * time.Second):
case <-time.After(time.Duration(s.addTimeout) * time.Second):
s.log.Error().Str("hash", t.InfoHash().String()).Msg("timeout getting torrent info")
return errors.New("timeout getting torrent info")
case <-t.GotInfo():
@ -134,10 +149,6 @@ func (s *Service) addTorrent(r string, t *torrent.Torrent) error {
folder := path.Join("/", r)
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.fss[folder]
if !ok {
s.fss[folder] = fs.NewTorrent()
}
tfs, ok := s.fss[folder].(*fs.Torrent)
if !ok {

View file

@ -90,6 +90,13 @@ func NewStats() *Stats {
}
}
func (s *Stats) AddRoute(route string) {
_, ok := s.torrentsByRoute[route]
if !ok {
s.torrentsByRoute[route] = make(map[string]*torrent.Torrent)
}
}
func (s *Stats) Add(route string, t *torrent.Torrent) {
s.mut.Lock()
defer s.mut.Unlock()