feat: supporting queue using aria2

This commit is contained in:
Chubby Granny Chaser 2024-07-02 17:06:30 +01:00
parent 16a8c28935
commit a43768ce67
No known key found for this signature in database
4 changed files with 64 additions and 49 deletions

View File

@ -14,8 +14,6 @@ export const startAria2 = () => {
"--rpc-listen-all", "--rpc-listen-all",
"--file-allocation=none", "--file-allocation=none",
"--allow-overwrite=true", "--allow-overwrite=true",
"--disable-bittorrent",
"--disable-metalink",
], ],
{ stdio: "inherit", windowsHide: true } { stdio: "inherit", windowsHide: true }
); );

View File

@ -63,7 +63,7 @@ export class DownloadManager {
static async pauseDownload() { static async pauseDownload() {
if (this.currentDownloader === Downloader.RealDebrid) { if (this.currentDownloader === Downloader.RealDebrid) {
RealDebridDownloader.pauseDownload(); await RealDebridDownloader.pauseDownload();
} else { } else {
await TorrentDownloader.pauseDownload(); await TorrentDownloader.pauseDownload();
} }

View File

@ -5,7 +5,6 @@ import { startAria2 } from "../aria2c";
import Aria2 from "aria2"; import Aria2 from "aria2";
export class HttpDownload { export class HttpDownload {
private static gid: string | null = null;
private static connected = false; private static connected = false;
private static aria2c: ChildProcess | null = null; private static aria2c: ChildProcess | null = null;
@ -30,9 +29,9 @@ export class HttpDownload {
} }
} }
public static getStatus() { public static getStatus(gid: string) {
if (this.connected && this.gid) { if (this.connected) {
return this.aria2.call("tellStatus", this.gid); return this.aria2.call("tellStatus", gid);
} }
return null; return null;
@ -47,21 +46,14 @@ export class HttpDownload {
static async cancelDownload(gid: string) { static async cancelDownload(gid: string) {
await this.aria2.call("forceRemove", gid); await this.aria2.call("forceRemove", gid);
if (this.gid === gid) {
this.gid = null;
}
} }
static async pauseDownload() { static async pauseDownload(gid: string) {
if (this.gid) { await this.aria2.call("forcePause", gid);
await this.aria2.call("forcePause", this.gid);
this.gid = null;
}
} }
static async resumeDownload(gid: string) { static async resumeDownload(gid: string) {
await this.aria2.call("unpause", gid); await this.aria2.call("unpause", gid);
this.gid = gid;
} }
static async startDownload(downloadPath: string, downloadUrl: string) { static async startDownload(downloadPath: string, downloadUrl: string) {
@ -71,7 +63,6 @@ export class HttpDownload {
dir: downloadPath, dir: downloadPath,
}; };
this.gid = await this.aria2.call("addUri", [downloadUrl], options); return this.aria2.call("addUri", [downloadUrl], options);
return this.gid;
} }
} }

View File

@ -35,40 +35,47 @@ export class RealDebridDownloader {
} }
public static async getStatus() { 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) { if (status) {
const progress = const progress =
Number(status.completedLength) / Number(status.totalLength); Number(status.completedLength) / Number(status.totalLength);
await gameRepository.update( await gameRepository.update(
{ id: this.downloadingGame!.id }, { id: this.downloadingGame!.id },
{ {
bytesDownloaded: Number(status.completedLength), bytesDownloaded: Number(status.completedLength),
fileSize: Number(status.totalLength), 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, 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) { return result;
await this.pauseDownload();
} }
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) { if (this.realDebridTorrentId && this.downloadingGame) {
@ -102,21 +109,31 @@ export class RealDebridDownloader {
} }
static async pauseDownload() { static async pauseDownload() {
await HttpDownload.pauseDownload(); const gid = this.downloads.get(this.downloadingGame!.id!);
if (gid) {
await HttpDownload.pauseDownload(gid);
}
this.realDebridTorrentId = null; this.realDebridTorrentId = null;
this.downloadingGame = null; this.downloadingGame = null;
this.downloads.delete(this.downloadingGame!.id!);
} }
static async startDownload(game: Game) { static async startDownload(game: Game) {
this.realDebridTorrentId = await RealDebridClient.getTorrentId(game!.uri!);
this.downloadingGame = game; 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(); const downloadUrl = await this.getRealDebridDownloadUrl();
if (downloadUrl) { if (downloadUrl) {
this.realDebridTorrentId = null; this.realDebridTorrentId = null;
const gid = await HttpDownload.startDownload( const gid = await HttpDownload.startDownload(
game.downloadPath!, game.downloadPath!,
downloadUrl downloadUrl
@ -128,9 +145,18 @@ export class RealDebridDownloader {
static async cancelDownload(gameId: number) { static async cancelDownload(gameId: number) {
const gid = this.downloads.get(gameId); const gid = this.downloads.get(gameId);
if (gid) { if (gid) {
await HttpDownload.cancelDownload(gid); await HttpDownload.cancelDownload(gid);
this.downloads.delete(gameId); this.downloads.delete(gameId);
} }
} }
static async resumeDownload(gameId: number) {
const gid = this.downloads.get(gameId);
if (gid) {
await HttpDownload.resumeDownload(gid);
}
}
} }