From a43768ce672bf7a4c78603f4bca6f6e89828a557 Mon Sep 17 00:00:00 2001 From: Chubby Granny Chaser Date: Tue, 2 Jul 2024 17:06:30 +0100 Subject: [PATCH] feat: supporting queue using aria2 --- src/main/services/aria2c.ts | 2 - .../services/download/download-manager.ts | 2 +- src/main/services/download/http-download.ts | 21 ++--- .../download/real-debrid-downloader.ts | 88 ++++++++++++------- 4 files changed, 64 insertions(+), 49 deletions(-) diff --git a/src/main/services/aria2c.ts b/src/main/services/aria2c.ts index ff15c3f2..b1b1da76 100644 --- a/src/main/services/aria2c.ts +++ b/src/main/services/aria2c.ts @@ -14,8 +14,6 @@ export const startAria2 = () => { "--rpc-listen-all", "--file-allocation=none", "--allow-overwrite=true", - "--disable-bittorrent", - "--disable-metalink", ], { stdio: "inherit", windowsHide: true } ); diff --git a/src/main/services/download/download-manager.ts b/src/main/services/download/download-manager.ts index 6d28eee0..35ff0f2d 100644 --- a/src/main/services/download/download-manager.ts +++ b/src/main/services/download/download-manager.ts @@ -63,7 +63,7 @@ export class DownloadManager { static async pauseDownload() { if (this.currentDownloader === Downloader.RealDebrid) { - RealDebridDownloader.pauseDownload(); + await RealDebridDownloader.pauseDownload(); } else { await TorrentDownloader.pauseDownload(); } diff --git a/src/main/services/download/http-download.ts b/src/main/services/download/http-download.ts index 10cf52ae..4553a6cb 100644 --- a/src/main/services/download/http-download.ts +++ b/src/main/services/download/http-download.ts @@ -5,7 +5,6 @@ import { startAria2 } from "../aria2c"; import Aria2 from "aria2"; export class HttpDownload { - private static gid: string | null = null; private static connected = false; private static aria2c: ChildProcess | null = null; @@ -30,9 +29,9 @@ export class HttpDownload { } } - public static getStatus() { - if (this.connected && this.gid) { - return this.aria2.call("tellStatus", this.gid); + public static getStatus(gid: string) { + if (this.connected) { + return this.aria2.call("tellStatus", gid); } return null; @@ -47,21 +46,14 @@ export class HttpDownload { static async cancelDownload(gid: string) { await this.aria2.call("forceRemove", gid); - if (this.gid === gid) { - this.gid = null; - } } - static async pauseDownload() { - if (this.gid) { - await this.aria2.call("forcePause", this.gid); - this.gid = null; - } + static async pauseDownload(gid: string) { + await this.aria2.call("forcePause", gid); } static async resumeDownload(gid: string) { await this.aria2.call("unpause", gid); - this.gid = gid; } static async startDownload(downloadPath: string, downloadUrl: string) { @@ -71,7 +63,6 @@ export class HttpDownload { dir: downloadPath, }; - this.gid = await this.aria2.call("addUri", [downloadUrl], options); - return this.gid; + return this.aria2.call("addUri", [downloadUrl], options); } } diff --git a/src/main/services/download/real-debrid-downloader.ts b/src/main/services/download/real-debrid-downloader.ts index 58b088d8..8ead0067 100644 --- a/src/main/services/download/real-debrid-downloader.ts +++ b/src/main/services/download/real-debrid-downloader.ts @@ -35,40 +35,47 @@ export class RealDebridDownloader { } public static async getStatus() { - const status = await HttpDownload.getStatus(); + if (this.downloadingGame) { + const gid = this.downloads.get(this.downloadingGame.id)!; + const status = await HttpDownload.getStatus(gid); - if (status) { - const progress = - Number(status.completedLength) / Number(status.totalLength); + if (status) { + const progress = + Number(status.completedLength) / Number(status.totalLength); - await gameRepository.update( - { id: this.downloadingGame!.id }, - { - bytesDownloaded: Number(status.completedLength), - fileSize: Number(status.totalLength), + await gameRepository.update( + { id: this.downloadingGame!.id }, + { + bytesDownloaded: Number(status.completedLength), + fileSize: Number(status.totalLength), + progress, + status: "active", + } + ); + + const result = { + numPeers: 0, + numSeeds: 0, + downloadSpeed: Number(status.downloadSpeed), + timeRemaining: calculateETA( + Number(status.totalLength), + Number(status.completedLength), + Number(status.downloadSpeed) + ), + isDownloadingMetadata: false, + isCheckingFiles: false, progress, - status: "active", + gameId: this.downloadingGame!.id, + } as DownloadProgress; + + if (progress === 1) { + this.downloads.delete(this.downloadingGame.id); + this.realDebridTorrentId = null; + this.downloadingGame = null; } - ); - if (progress === 1) { - await this.pauseDownload(); + return result; } - - return { - numPeers: 0, - numSeeds: 0, - downloadSpeed: Number(status.downloadSpeed), - timeRemaining: calculateETA( - Number(status.totalLength), - Number(status.completedLength), - Number(status.downloadSpeed) - ), - isDownloadingMetadata: false, - isCheckingFiles: false, - progress, - gameId: this.downloadingGame!.id, - } as DownloadProgress; } if (this.realDebridTorrentId && this.downloadingGame) { @@ -102,21 +109,31 @@ export class RealDebridDownloader { } static async pauseDownload() { - await HttpDownload.pauseDownload(); + const gid = this.downloads.get(this.downloadingGame!.id!); + if (gid) { + await HttpDownload.pauseDownload(gid); + } this.realDebridTorrentId = null; this.downloadingGame = null; - this.downloads.delete(this.downloadingGame!.id!); } static async startDownload(game: Game) { - this.realDebridTorrentId = await RealDebridClient.getTorrentId(game!.uri!); this.downloadingGame = game; + if (this.downloads.has(game.id)) { + await this.resumeDownload(game.id!); + + return; + } + + this.realDebridTorrentId = await RealDebridClient.getTorrentId(game!.uri!); + const downloadUrl = await this.getRealDebridDownloadUrl(); if (downloadUrl) { this.realDebridTorrentId = null; + const gid = await HttpDownload.startDownload( game.downloadPath!, downloadUrl @@ -128,9 +145,18 @@ export class RealDebridDownloader { static async cancelDownload(gameId: number) { const gid = this.downloads.get(gameId); + if (gid) { await HttpDownload.cancelDownload(gid); this.downloads.delete(gameId); } } + + static async resumeDownload(gameId: number) { + const gid = this.downloads.get(gameId); + + if (gid) { + await HttpDownload.resumeDownload(gid); + } + } }