fix(downloads): support Transmission and aria2 end to end

Fixes #65
This commit is contained in:
ShukeBta
2026-08-10 21:04:04 +08:00
parent c0d743d89d
commit 42ef19b7e5
43 changed files with 2350 additions and 480 deletions
+45
View File
@@ -40,6 +40,8 @@ type QBitConfig struct {
// QBitTorrent is the subset of /torrents/info we surface to the API.
type QBitTorrent struct {
Hash string `json:"hash"`
ClientID string `json:"client_id,omitempty"`
Source string `json:"source,omitempty"`
Name string `json:"name"`
State string `json:"state"`
Progress float32 `json:"progress"`
@@ -196,6 +198,49 @@ func (q *QBitClient) Delete(ctx context.Context, hash string, deleteFiles bool)
return nil
}
func (q *QBitClient) Pause(ctx context.Context, hash string) error {
return q.torrentAction(ctx, hash, "pause", "stop")
}
func (q *QBitClient) Resume(ctx context.Context, hash string) error {
return q.torrentAction(ctx, hash, "resume", "start")
}
func (q *QBitClient) torrentAction(ctx context.Context, hash string, actions ...string) error {
q.mu.Lock()
defer q.mu.Unlock()
if err := q.ensureAuth(ctx); err != nil {
return err
}
baseURL := strings.TrimRight(q.cfg.BaseURL, "/")
form := url.Values{"hashes": []string{hash}}
var lastErr error
for _, action := range actions {
req, err := newDownloadClientHTTPRequest(ctx, http.MethodPost,
baseURL+"/api/v2/torrents/"+action, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", baseURL)
req.Header.Set("Origin", baseURL)
resp, err := q.client.Do(req)
if err != nil {
return err
}
body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if resp.StatusCode < 400 {
return nil
}
lastErr = fmt.Errorf("qbittorrent %s: %d: %s", action, resp.StatusCode, strings.TrimSpace(string(body)))
if resp.StatusCode != http.StatusNotFound && resp.StatusCode != http.StatusMethodNotAllowed {
break
}
}
return lastErr
}
// SetLocation moves a torrent's data to a new save directory via
// POST /api/v2/torrents/setLocation. qBittorrent performs the physical move
// itself and keeps seeding from the new location — this is the seeding-safe