fix(qbittorrent): remove client cache (#357)

* fix(qbittorrent): permit multiple uploads again

* fix(qbittorrent): remove client cache
This commit is contained in:
Kyle Sanderson 2022-07-17 08:47:42 -07:00 committed by GitHub
parent a63e022c15
commit dfe2ac56a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 59 deletions

View file

@ -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) return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
qbt, exists := s.qbitClients[qbitKey{client.ID, client.Name}] qbtSettings := qbittorrent.Settings{
if !exists { Name: client.Name,
qbtSettings := qbittorrent.Settings{ Hostname: client.Host,
Name: client.Name, Port: uint(client.Port),
Hostname: client.Host, Username: client.Username,
Port: uint(client.Port), Password: client.Password,
Username: client.Username, TLS: client.TLS,
Password: client.Password, TLSSkipVerify: client.TLSSkipVerify,
TLS: client.TLS, }
TLSSkipVerify: client.TLSSkipVerify,
}
// setup sub logger adapter which is compatible with *log.Logger // 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.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel)
// only set basic auth if enabled // only set basic auth if enabled
if client.Settings.Basic.Auth { if client.Settings.Basic.Auth {
qbtSettings.BasicAuth = client.Settings.Basic.Auth qbtSettings.BasicAuth = client.Settings.Basic.Auth
qbtSettings.Basic.Username = client.Settings.Basic.Username qbtSettings.Basic.Username = client.Settings.Basic.Username
qbtSettings.Basic.Password = client.Settings.Basic.Password qbtSettings.Basic.Password = client.Settings.Basic.Password
} }
qbt = qbittorrent.NewClient(qbtSettings) qbt := qbittorrent.NewClient(qbtSettings)
s.qbitClients[qbitKey{client.ID, client.Name}] = qbt
// only login if we have a password
if qbtSettings.Password != "" {
if err = qbt.Login(); err != nil { if err = qbt.Login(); err != nil {
return nil, errors.Wrap(err, "could not log into client: %v at %v", client.Name, client.Host) 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) rejections, err := s.qbittorrentCheckRulesCanDownload(action, client, qbt)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error checking client rules: %v", action.Name) return nil, errors.Wrap(err, "error checking client rules: %v", action.Name)
@ -157,7 +151,7 @@ func (s *service) prepareQbitOptions(action domain.Action, m Macro) (map[string]
return options, nil 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) s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
// check for active downloads and other rules // check for active downloads and other rules
@ -202,7 +196,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client
return nil, nil 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 announceOK := false
attempts := 0 attempts := 0

View file

@ -20,19 +20,6 @@ import (
"github.com/autobrr/autobrr/pkg/errors" "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 ( var (
backoffSchedule = []time.Duration{ backoffSchedule = []time.Duration{
5 * time.Second, 5 * time.Second,
@ -42,7 +29,7 @@ var (
timeout = 60 * time.Second timeout = 60 * time.Second
) )
type client struct { type Client struct {
Name string Name string
settings Settings settings Settings
http *http.Client http *http.Client
@ -69,8 +56,8 @@ type Basic struct {
Password string Password string
} }
func NewClient(settings Settings) Client { func NewClient(settings Settings) *Client {
c := &client{ c := &Client{
settings: settings, settings: settings,
Name: settings.Name, Name: settings.Name,
log: log.New(io.Discard, "", log.LstdFlags), log: log.New(io.Discard, "", log.LstdFlags),
@ -110,7 +97,7 @@ func NewClient(settings Settings) Client {
return c 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 err error
var resp *http.Response var resp *http.Response
@ -146,7 +133,7 @@ func (c *client) get(endpoint string, opts map[string]string) (*http.Response, e
return resp, nil 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 // add optional parameters that the user wants
form := url.Values{} form := url.Values{}
if opts != nil { if opts != nil {
@ -193,7 +180,7 @@ func (c *client) post(endpoint string, opts map[string]string) (*http.Response,
return resp, nil 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 // add optional parameters that the user wants
form := url.Values{} form := url.Values{}
if opts != nil { if opts != nil {
@ -227,7 +214,7 @@ func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Respo
return resp, nil 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 err error
var resp *http.Response var resp *http.Response
@ -308,7 +295,7 @@ func (c *client) postFile(endpoint string, fileName string, opts map[string]stri
return resp, nil return resp, nil
} }
func (c *client) setCookies(cookies []*http.Cookie) { func (c *Client) setCookies(cookies []*http.Cookie) {
cookieURL, _ := url.Parse(buildUrl(c.settings, "")) cookieURL, _ := url.Parse(buildUrl(c.settings, ""))
c.http.Jar.SetCookies(cookieURL, cookies) c.http.Jar.SetCookies(cookieURL, cookies)

View file

@ -12,7 +12,7 @@ import (
) )
// Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication // 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{ opts := map[string]string{
"username": c.settings.Username, "username": c.settings.Username,
"password": c.settings.Password, "password": c.settings.Password,
@ -55,7 +55,7 @@ func (c *client) Login() error {
return nil return nil
} }
func (c *client) GetTorrents() ([]Torrent, error) { func (c *Client) GetTorrents() ([]Torrent, error) {
resp, err := c.get("torrents/info", nil) resp, err := c.get("torrents/info", nil)
if err != nil { if err != nil {
@ -78,7 +78,7 @@ func (c *client) GetTorrents() ([]Torrent, error) {
return torrents, nil return torrents, nil
} }
func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) { func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
opts := map[string]string{ opts := map[string]string{
"filter": string(filter), "filter": string(filter),
} }
@ -104,7 +104,7 @@ func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
return torrents, nil return torrents, nil
} }
func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) { func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
var filter = TorrentFilterDownloading var filter = TorrentFilterDownloading
opts := map[string]string{ opts := map[string]string{
@ -141,7 +141,7 @@ func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) {
return res, nil return res, nil
} }
func (c *client) GetTorrentsRaw() (string, error) { func (c *Client) GetTorrentsRaw() (string, error) {
resp, err := c.get("torrents/info", nil) resp, err := c.get("torrents/info", nil)
if err != nil { if err != nil {
return "", errors.Wrap(err, "could not get torrents raw") return "", errors.Wrap(err, "could not get torrents raw")
@ -157,7 +157,7 @@ func (c *client) GetTorrentsRaw() (string, error) {
return string(data), nil return string(data), nil
} }
func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) { func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
opts := map[string]string{ opts := map[string]string{
"hash": hash, "hash": hash,
} }
@ -200,7 +200,7 @@ func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
} }
// AddTorrentFromFile add new torrent from torrent file // 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) res, err := c.postFile("torrents/add", file, options)
if err != nil { if err != nil {
@ -214,7 +214,7 @@ func (c *client) AddTorrentFromFile(file string, options map[string]string) erro
return nil 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 // Add hashes together with | separator
hv := strings.Join(hashes, "|") hv := strings.Join(hashes, "|")
@ -235,7 +235,7 @@ func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error {
return nil return nil
} }
func (c *client) ReAnnounceTorrents(hashes []string) error { func (c *Client) ReAnnounceTorrents(hashes []string) error {
// Add hashes together with | separator // Add hashes together with | separator
hv := strings.Join(hashes, "|") hv := strings.Join(hashes, "|")
opts := map[string]string{ opts := map[string]string{
@ -254,7 +254,7 @@ func (c *client) ReAnnounceTorrents(hashes []string) error {
return nil return nil
} }
func (c *client) GetTransferInfo() (*TransferInfo, error) { func (c *Client) GetTransferInfo() (*TransferInfo, error) {
resp, err := c.get("transfer/info", nil) resp, err := c.get("transfer/info", nil)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not get transfer info") return nil, errors.Wrap(err, "could not get transfer info")