Skip to content

Commit

Permalink
fix: #800 resume and pause does not work with qbittorrent 5
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteout12 committed Oct 7, 2024
1 parent 69feefe commit 7ba949f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 18 additions & 2 deletions server/services/qBittorrent/clientRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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(),
}),
Expand All @@ -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(),
}),
Expand Down Expand Up @@ -608,6 +623,7 @@ class ClientRequestManager {
this.connectionSettings = connectionSettings;
this.apiBase = `${connectionSettings.url}/api/v2`;
this.updateAuthCookie().catch(() => undefined);
this.getApiVersion();
}
}

Expand Down
16 changes: 16 additions & 0 deletions server/services/qBittorrent/util/apiVersionCheck.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7ba949f

Please sign in to comment.