fix: resume and pause does not work with qbittorrent 5 (#801)

close #800
This commit is contained in:
Björn Carlsson
2024-10-07 20:04:54 +02:00
committed by GitHub
parent 69feefe2f9
commit ae7a5742b1
2 changed files with 34 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ import type {
QBittorrentTorrentTrackers,
} from './types/QBittorrentTorrentsMethods';
import type {QBittorrentTransferInfo} from './types/QBittorrentTransferMethods';
import {isApiVersionAtLeast} from './util/apiVersionCheck';
const EMPTY_SERVER_STATE = {
dl_info_speed: 0,
@@ -35,6 +36,7 @@ const EMPTY_SERVER_STATE = {
class ClientRequestManager {
private connectionSettings: QBittorrentConnectionSettings;
private apiBase: string;
private apiVersion: string | null = null;
private authCookie: Promise<string | undefined> = Promise.resolve(undefined);
private isMainDataPending = false;
@@ -118,6 +120,17 @@ class ClientRequestManager {
});
}
async getApiVersion(): Promise<void> {
try {
const {data} = await axios.get(`${this.apiBase}/app/webapiVersion`, {
headers: await this.getRequestHeaders(),
});
this.apiVersion = data;
} catch (error) {
this.apiVersion = null;
}
}
async getTorrentInfos(): Promise<QBittorrentTorrentInfos> {
return axios
.post<QBittorrentTorrentInfos>(`${this.apiBase}/torrents/info`, null, {
@@ -293,9 +306,10 @@ class ClientRequestManager {
}
async torrentsPause(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'stop' : 'pause';
return axios
.post(
`${this.apiBase}/torrents/pause`,
`${this.apiBase}/torrents/${method}`,
new URLSearchParams({
hashes: hashes.join('|').toLowerCase(),
}),
@@ -309,9 +323,10 @@ class ClientRequestManager {
}
async torrentsResume(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'start' : 'resume';
return axios
.post(
`${this.apiBase}/torrents/resume`,
`${this.apiBase}/torrents/${method}`,
new URLSearchParams({
hashes: hashes.join('|').toLowerCase(),
}),
@@ -608,6 +623,7 @@ class ClientRequestManager {
this.connectionSettings = connectionSettings;
this.apiBase = `${connectionSettings.url}/api/v2`;
this.updateAuthCookie().catch(() => undefined);
this.getApiVersion();
}
}

View File

@@ -0,0 +1,16 @@
export function isApiVersionAtLeast(currentVersion: string | null, targetVersion: string): boolean {
if (!currentVersion) {
return false;
}
const v1 = currentVersion.split('.').map(Number);
const v2 = targetVersion.split('.').map(Number);
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
const num1 = v1[i] || 0;
const num2 = v2[i] || 0;
if (num1 > num2) return true;
if (num1 < num2) return false;
}
return true;
}