Skip to content

Commit

Permalink
fix(qBittorrent): api version state handling (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
LXGaming authored Nov 25, 2024
1 parent 843b9c9 commit 80825db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
8 changes: 5 additions & 3 deletions server/services/qBittorrent/clientGatewayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ class QBittorrentClientGatewayService extends ClientGatewayService {
throw new Error();
}

const method = isApiVersionAtLeast(await this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused';
await this.clientRequestManager
.torrentsAddFiles(fileBuffers, {
savepath: destination,
tags: tags.join(','),
[isApiVersionAtLeast(this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused']: !start,
[method]: !start,
root_folder: !isBasePath,
contentLayout: isBasePath ? 'NoSubfolder' : undefined,
sequentialDownload: isSequential,
Expand Down Expand Up @@ -119,11 +120,12 @@ class QBittorrentClientGatewayService extends ClientGatewayService {
throw new Error();
}

const method = isApiVersionAtLeast(await this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused';
await this.clientRequestManager
.torrentsAddURLs(urls, {
savepath: destination,
tags: tags.join(','),
[isApiVersionAtLeast(this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused']: !start,
[method]: !start,
root_folder: !isBasePath,
contentLayout: isBasePath ? 'NoSubfolder' : undefined,
sequentialDownload: isSequential,
Expand Down Expand Up @@ -526,7 +528,7 @@ class QBittorrentClientGatewayService extends ClientGatewayService {

async testGateway(): Promise<void> {
return this.clientRequestManager
.updateAuthCookie()
.updateConnection()
.then(() => this.processClientRequestSuccess(undefined), this.processClientRequestError);
}
}
Expand Down
40 changes: 23 additions & 17 deletions server/services/qBittorrent/clientRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const EMPTY_SERVER_STATE = {
class ClientRequestManager {
private connectionSettings: QBittorrentConnectionSettings;
private apiBase: string;
apiVersion: string | null = null;
apiVersion: Promise<string | undefined> = Promise.resolve(undefined);
private authCookie: Promise<string | undefined> = Promise.resolve(undefined);
private isMainDataPending = false;

Expand Down Expand Up @@ -80,17 +80,27 @@ class ClientRequestManager {
});
}

async updateAuthCookie(connectionSettings?: QBittorrentConnectionSettings): Promise<void> {
let authFailed = false;
async updateConnection(connectionSettings?: QBittorrentConnectionSettings): Promise<void> {
let failed = false;

this.authCookie = this.authenticate(connectionSettings).catch(() => {
authFailed = true;
failed = true;
return undefined;
});

this.apiVersion = this.authCookie
.then(() => {
return !failed ? this.getApiVersion() : Promise.resolve(undefined);
})
.catch(() => {
failed = true;
return undefined;
});

await this.authCookie;
await this.apiVersion;

if (authFailed) {
if (failed) {
throw new Error();
}
}
Expand Down Expand Up @@ -120,15 +130,12 @@ class ClientRequestManager {
});
}

async getApiVersion(): Promise<void> {
try {
const {data} = await axios.get(`${this.apiBase}/app/webapiVersion`, {
async getApiVersion(): Promise<string> {
return axios
.get<string>(`${this.apiBase}/app/webapiVersion`, {
headers: await this.getRequestHeaders(),
});
this.apiVersion = data;
} catch (error) {
this.apiVersion = null;
}
})
.then((res) => res.data);
}

async getTorrentInfos(): Promise<QBittorrentTorrentInfos> {
Expand Down Expand Up @@ -306,7 +313,7 @@ class ClientRequestManager {
}

async torrentsPause(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'stop' : 'pause';
const method = isApiVersionAtLeast(await this.apiVersion, '2.11.0') ? 'stop' : 'pause';
return axios
.post(
`${this.apiBase}/torrents/${method}`,
Expand All @@ -323,7 +330,7 @@ class ClientRequestManager {
}

async torrentsResume(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'start' : 'resume';
const method = isApiVersionAtLeast(await this.apiVersion, '2.11.0') ? 'start' : 'resume';
return axios
.post(
`${this.apiBase}/torrents/${method}`,
Expand Down Expand Up @@ -622,8 +629,7 @@ class ClientRequestManager {
constructor(connectionSettings: QBittorrentConnectionSettings) {
this.connectionSettings = connectionSettings;
this.apiBase = `${connectionSettings.url}/api/v2`;
this.updateAuthCookie().catch(() => undefined);
this.getApiVersion();
this.updateConnection().catch(() => undefined);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/services/qBittorrent/util/apiVersionCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isApiVersionAtLeast(currentVersion: string | null, targetVersion: string): boolean {
export function isApiVersionAtLeast(currentVersion: string | undefined, targetVersion: string): boolean {
if (!currentVersion) {
return false;
}
Expand Down

0 comments on commit 80825db

Please sign in to comment.