Feature: Download client rules (#18)

* feat(web): add and update download client rules

* feat: add and update download client rules

* feat: add active downloads check

* chore: update pkg

* feat: deluge max active downloads

* feat: use basic rules for deluge

* feat: add as paused

* refactor: download file if needed

* feat: better errors qbit
This commit is contained in:
Ludvig Lundgren 2021-09-10 16:54:30 +02:00 committed by GitHub
parent 09eb0b1716
commit c02f16b64d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 628 additions and 228 deletions

View file

@ -1,24 +1,17 @@
package action
import (
"io"
"os"
"path"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/download_client"
"github.com/rs/zerolog/log"
)
const REANNOUNCE_MAX_ATTEMPTS = 30
const REANNOUNCE_INTERVAL = 7000
type Service interface {
RunActions(torrentFile string, hash string, filter domain.Filter, announce domain.Announce) error
Store(action domain.Action) (*domain.Action, error)
Fetch() ([]domain.Action, error)
Delete(actionID int) error
ToggleEnabled(actionID int) error
RunActions(actions []domain.Action, announce domain.Announce) error
}
type service struct {
@ -30,72 +23,6 @@ func NewService(repo domain.ActionRepo, clientSvc download_client.Service) Servi
return &service{repo: repo, clientSvc: clientSvc}
}
func (s *service) RunActions(torrentFile string, hash string, filter domain.Filter, announce domain.Announce) error {
for _, action := range filter.Actions {
if !action.Enabled {
// only run active actions
continue
}
log.Trace().Msgf("process action: %v", action.Name)
switch action.Type {
case domain.ActionTypeTest:
go s.test(torrentFile)
case domain.ActionTypeWatchFolder:
go s.watchFolder(action.WatchFolder, torrentFile)
case domain.ActionTypeQbittorrent:
go func() {
err := s.qbittorrent(action, hash, torrentFile)
if err != nil {
log.Error().Err(err).Msg("error sending torrent to client")
}
}()
case domain.ActionTypeExec:
go s.execCmd(announce, action, torrentFile)
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
go func() {
err := s.deluge(action, torrentFile)
if err != nil {
log.Error().Err(err).Msg("error sending torrent to client")
}
}()
case domain.ActionTypeRadarr:
go func() {
err := s.radarr(announce, action)
if err != nil {
log.Error().Err(err).Msg("error sending torrent to radarr")
}
}()
case domain.ActionTypeSonarr:
go func() {
err := s.sonarr(announce, action)
if err != nil {
log.Error().Err(err).Msg("error sending torrent to sonarr")
}
}()
case domain.ActionTypeLidarr:
go func() {
err := s.lidarr(announce, action)
if err != nil {
log.Error().Err(err).Msg("error sending torrent to lidarr")
}
}()
default:
log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
}
}
return nil
}
func (s *service) Store(action domain.Action) (*domain.Action, error) {
// validate data
@ -131,36 +58,3 @@ func (s *service) ToggleEnabled(actionID int) error {
return nil
}
func (s *service) test(torrentFile string) {
log.Info().Msgf("action TEST: %v", torrentFile)
}
func (s *service) watchFolder(dir string, torrentFile string) {
log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", dir, torrentFile)
// Open original file
original, err := os.Open(torrentFile)
if err != nil {
log.Fatal().Err(err)
}
defer original.Close()
_, tmpFileName := path.Split(torrentFile)
fullFileName := path.Join(dir, tmpFileName)
// Create new file
newFile, err := os.Create(fullFileName)
if err != nil {
log.Fatal().Err(err)
}
defer newFile.Close()
// Copy file
_, err = io.Copy(newFile, original)
if err != nil {
log.Fatal().Err(err)
}
log.Info().Msgf("saved file to watch folder: %v", fullFileName)
}