Skip to content

Commit

Permalink
fix(qbittorrent): remove client cache (#357)
Browse files Browse the repository at this point in the history
* fix(qbittorrent): permit multiple uploads again

* fix(qbittorrent): remove client cache
  • Loading branch information
KyleSanderson committed Jul 17, 2022
1 parent a63e022 commit dfe2ac5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 59 deletions.
50 changes: 22 additions & 28 deletions internal/action/qbittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,35 @@ func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]s
return nil, errors.New("could not find client by id: %v", action.ClientID)
}

qbt, exists := s.qbitClients[qbitKey{client.ID, client.Name}]
if !exists {
qbtSettings := qbittorrent.Settings{
Name: client.Name,
Hostname: client.Host,
Port: uint(client.Port),
Username: client.Username,
Password: client.Password,
TLS: client.TLS,
TLSSkipVerify: client.TLSSkipVerify,
}

// setup sub logger adapter which is compatible with *log.Logger
qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel)
qbtSettings := qbittorrent.Settings{
Name: client.Name,
Hostname: client.Host,
Port: uint(client.Port),
Username: client.Username,
Password: client.Password,
TLS: client.TLS,
TLSSkipVerify: client.TLSSkipVerify,
}

// only set basic auth if enabled
if client.Settings.Basic.Auth {
qbtSettings.BasicAuth = client.Settings.Basic.Auth
qbtSettings.Basic.Username = client.Settings.Basic.Username
qbtSettings.Basic.Password = client.Settings.Basic.Password
}
// setup sub logger adapter which is compatible with *log.Logger
qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel)

qbt = qbittorrent.NewClient(qbtSettings)
// only set basic auth if enabled
if client.Settings.Basic.Auth {
qbtSettings.BasicAuth = client.Settings.Basic.Auth
qbtSettings.Basic.Username = client.Settings.Basic.Username
qbtSettings.Basic.Password = client.Settings.Basic.Password
}

s.qbitClients[qbitKey{client.ID, client.Name}] = qbt
qbt := qbittorrent.NewClient(qbtSettings)

// only login if we have a password
if qbtSettings.Password != "" {
if err = qbt.Login(); err != nil {
return nil, errors.Wrap(err, "could not log into client: %v at %v", client.Name, client.Host)
}
}

if qbt == nil {
return nil, errors.New("qbit client does not exist")
}

rejections, err := s.qbittorrentCheckRulesCanDownload(action, client, qbt)
if err != nil {
return nil, errors.Wrap(err, "error checking client rules: %v", action.Name)
Expand Down Expand Up @@ -157,7 +151,7 @@ func (s *service) prepareQbitOptions(action domain.Action, m Macro) (map[string]
return options, nil
}

func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt qbittorrent.Client) ([]string, error) {
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)

// check for active downloads and other rules
Expand Down Expand Up @@ -202,7 +196,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client
return nil, nil
}

func (s *service) reannounceTorrent(qb qbittorrent.Client, action domain.Action, hash string) error {
func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action, hash string) error {
announceOK := false
attempts := 0

Expand Down
29 changes: 8 additions & 21 deletions pkg/qbittorrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ import (
"github.com/autobrr/autobrr/pkg/errors"
)

type Client interface {
Login() error
GetTorrents() ([]Torrent, error)
GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error)
GetTorrentsActiveDownloads() ([]Torrent, error)
GetTorrentsRaw() (string, error)
GetTorrentTrackers(hash string) ([]TorrentTracker, error)
AddTorrentFromFile(file string, options map[string]string) error
DeleteTorrents(hashes []string, deleteFiles bool) error
ReAnnounceTorrents(hashes []string) error
GetTransferInfo() (*TransferInfo, error)
}

var (
backoffSchedule = []time.Duration{
5 * time.Second,
Expand All @@ -42,7 +29,7 @@ var (
timeout = 60 * time.Second
)

type client struct {
type Client struct {
Name string
settings Settings
http *http.Client
Expand All @@ -69,8 +56,8 @@ type Basic struct {
Password string
}

func NewClient(settings Settings) Client {
c := &client{
func NewClient(settings Settings) *Client {
c := &Client{
settings: settings,
Name: settings.Name,
log: log.New(io.Discard, "", log.LstdFlags),
Expand Down Expand Up @@ -110,7 +97,7 @@ func NewClient(settings Settings) Client {
return c
}

func (c *client) get(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response

Expand Down Expand Up @@ -146,7 +133,7 @@ func (c *client) get(endpoint string, opts map[string]string) (*http.Response, e
return resp, nil
}

func (c *client) post(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *Client) post(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
Expand Down Expand Up @@ -193,7 +180,7 @@ func (c *client) post(endpoint string, opts map[string]string) (*http.Response,
return resp, nil
}

func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *Client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
Expand Down Expand Up @@ -227,7 +214,7 @@ func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Respo
return resp, nil
}

func (c *client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
func (c *Client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response

Expand Down Expand Up @@ -308,7 +295,7 @@ func (c *client) postFile(endpoint string, fileName string, opts map[string]stri
return resp, nil
}

func (c *client) setCookies(cookies []*http.Cookie) {
func (c *Client) setCookies(cookies []*http.Cookie) {
cookieURL, _ := url.Parse(buildUrl(c.settings, ""))

c.http.Jar.SetCookies(cookieURL, cookies)
Expand Down
20 changes: 10 additions & 10 deletions pkg/qbittorrent/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication
func (c *client) Login() error {
func (c *Client) Login() error {
opts := map[string]string{
"username": c.settings.Username,
"password": c.settings.Password,
Expand Down Expand Up @@ -55,7 +55,7 @@ func (c *client) Login() error {
return nil
}

func (c *client) GetTorrents() ([]Torrent, error) {
func (c *Client) GetTorrents() ([]Torrent, error) {

resp, err := c.get("torrents/info", nil)
if err != nil {
Expand All @@ -78,7 +78,7 @@ func (c *client) GetTorrents() ([]Torrent, error) {
return torrents, nil
}

func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
opts := map[string]string{
"filter": string(filter),
}
Expand All @@ -104,7 +104,7 @@ func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
return torrents, nil
}

func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) {
func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
var filter = TorrentFilterDownloading

opts := map[string]string{
Expand Down Expand Up @@ -141,7 +141,7 @@ func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) {
return res, nil
}

func (c *client) GetTorrentsRaw() (string, error) {
func (c *Client) GetTorrentsRaw() (string, error) {
resp, err := c.get("torrents/info", nil)
if err != nil {
return "", errors.Wrap(err, "could not get torrents raw")
Expand All @@ -157,7 +157,7 @@ func (c *client) GetTorrentsRaw() (string, error) {
return string(data), nil
}

func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
opts := map[string]string{
"hash": hash,
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
}

// AddTorrentFromFile add new torrent from torrent file
func (c *client) AddTorrentFromFile(file string, options map[string]string) error {
func (c *Client) AddTorrentFromFile(file string, options map[string]string) error {

res, err := c.postFile("torrents/add", file, options)
if err != nil {
Expand All @@ -214,7 +214,7 @@ func (c *client) AddTorrentFromFile(file string, options map[string]string) erro
return nil
}

func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error {
func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")

Expand All @@ -235,7 +235,7 @@ func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error {
return nil
}

func (c *client) ReAnnounceTorrents(hashes []string) error {
func (c *Client) ReAnnounceTorrents(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
Expand All @@ -254,7 +254,7 @@ func (c *client) ReAnnounceTorrents(hashes []string) error {
return nil
}

func (c *client) GetTransferInfo() (*TransferInfo, error) {
func (c *Client) GetTransferInfo() (*TransferInfo, error) {
resp, err := c.get("transfer/info", nil)
if err != nil {
return nil, errors.Wrap(err, "could not get transfer info")
Expand Down

0 comments on commit dfe2ac5

Please sign in to comment.