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",
"--file-allocation=none",
"--allow-overwrite=true",
"--disable-bittorrent",
"--disable-metalink",
],
{ stdio: "inherit", windowsHide: true }
);

View File

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

View File

@ -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);
}
}

View File

@ -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);
}
}
}