fix(downloadclient): qBittorrent url parse err handling (#1832)

* fix(downloadclient): qBittorrent url parse err handling

* fix(downloadclient): qBittorrent url parse err handling test
This commit is contained in:
ze0s 2024-11-24 00:53:59 +01:00 committed by GitHub
parent a18284ecc6
commit f54c51fa06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 8 deletions

View file

@ -158,17 +158,20 @@ func (c DownloadClient) Validate() error {
return nil return nil
} }
func (c DownloadClient) BuildLegacyHost() string { func (c DownloadClient) BuildLegacyHost() (string, error) {
if c.Type == DownloadClientTypeQbittorrent { if c.Type == DownloadClientTypeQbittorrent {
return c.qbitBuildLegacyHost() return c.qbitBuildLegacyHost()
} }
return "" return c.Host, nil
} }
// qbitBuildLegacyHost exists to support older configs // qbitBuildLegacyHost exists to support older configs
func (c DownloadClient) qbitBuildLegacyHost() string { func (c DownloadClient) qbitBuildLegacyHost() (string, error) {
// parse url // parse url
u, _ := url.Parse(c.Host) u, err := url.Parse(c.Host)
if err != nil {
return "", err
}
// reset Opaque // reset Opaque
u.Opaque = "" u.Opaque = ""
@ -200,5 +203,5 @@ func (c DownloadClient) qbitBuildLegacyHost() string {
} }
// make into new string and return // make into new string and return
return u.String() return u.String(), nil
} }

View file

@ -153,7 +153,8 @@ func TestDownloadClient_qbitBuildLegacyHost(t *testing.T) {
Password: tt.fields.Password, Password: tt.fields.Password,
Settings: tt.fields.Settings, Settings: tt.fields.Settings,
} }
assert.Equalf(t, tt.want, c.qbitBuildLegacyHost(), "qbitBuildLegacyHost()") got, _ := c.qbitBuildLegacyHost()
assert.Equalf(t, tt.want, got, "qbitBuildLegacyHost()")
}) })
} }
} }

View file

@ -71,8 +71,13 @@ func (s *service) testConnection(ctx context.Context, client domain.DownloadClie
} }
func (s *service) testQbittorrentConnection(ctx context.Context, client domain.DownloadClient) error { func (s *service) testQbittorrentConnection(ctx context.Context, client domain.DownloadClient) error {
clientHost, err := client.BuildLegacyHost()
if err != nil {
return errors.Wrap(err, "error building qBittorrent host url: %s", client.Host)
}
qbtSettings := qbittorrent.Config{ qbtSettings := qbittorrent.Config{
Host: client.BuildLegacyHost(), Host: clientHost,
TLSSkipVerify: client.TLSSkipVerify, TLSSkipVerify: client.TLSSkipVerify,
Username: client.Username, Username: client.Username,
Password: client.Password, Password: client.Password,

View file

@ -181,8 +181,13 @@ func (s *service) GetClient(ctx context.Context, clientId int32) (*domain.Downlo
switch client.Type { switch client.Type {
case domain.DownloadClientTypeQbittorrent: case domain.DownloadClientTypeQbittorrent:
clientHost, err := client.BuildLegacyHost()
if err != nil {
return nil, errors.Wrap(err, "error building qBittorrent host url: %v", client.Host)
}
client.Client = qbittorrent.NewClient(qbittorrent.Config{ client.Client = qbittorrent.NewClient(qbittorrent.Config{
Host: client.BuildLegacyHost(), Host: clientHost,
Username: client.Username, Username: client.Username,
Password: client.Password, Password: client.Password,
TLSSkipVerify: client.TLSSkipVerify, TLSSkipVerify: client.TLSSkipVerify,