This commit is contained in:
Alexander
2026-04-29 17:29:58 +02:00
parent 3ecc6aee62
commit 945aab82c2
24 changed files with 2038 additions and 822 deletions
+19 -3
View File
@@ -12,6 +12,8 @@ import (
"net/url"
"strings"
"sync"
"github.com/rs/zerolog/log"
)
type QBittorrentClient struct {
@@ -54,7 +56,7 @@ func (c *QBittorrentClient) apiURL(path string) string {
func (c *QBittorrentClient) mapState(state string) TorrentState {
switch state {
case "downloading", "forcedDL", "metaDL", "allocating":
case "downloading", "forcedDL", "metaDL", "allocating", "stalledDL":
return StateDownloading
case "uploading", "forcedUP", "stalledUP":
return StateSeeding
@@ -209,6 +211,8 @@ func (c *QBittorrentClient) AddTorrentURL(ctx context.Context, torrentURL string
return err
}
log.Debug().Str("url", torrentURL).Msg("[QBITTORRENT] adding torrent URL")
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
w.WriteField("urls", torrentURL)
@@ -225,15 +229,27 @@ func (c *QBittorrentClient) AddTorrentURL(ctx context.Context, torrentURL string
resp, err := c.client.Do(req)
if err != nil {
log.Error().Err(err).Msg("[QBITTORRENT] request failed")
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := strings.TrimSpace(string(body))
log.Debug().Int("status", resp.StatusCode).Str("body", bodyStr).Msg("[QBITTORRENT] add torrent response")
if !statusOK(resp.StatusCode) {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("%w: %s", ErrInvalidRequest, string(body))
log.Error().Int("status", resp.StatusCode).Str("body", bodyStr).Msg("[QBITTORRENT] add torrent failed")
return fmt.Errorf("%w: %s", ErrInvalidRequest, bodyStr)
}
if bodyStr == "Fails." {
log.Error().Str("url", torrentURL).Msg("[QBITTORRENT] torrent add rejected")
return fmt.Errorf("qBittorrent rejected torrent: %s", torrentURL)
}
log.Info().Msg("[QBITTORRENT] torrent added successfully")
return nil
}