Fixing downloads by replacing the torrent client

This commit is contained in:
tuesday 2024-06-26 17:58:05 +02:00
parent 2aeeeaf15b
commit fea59514bc
2 changed files with 51 additions and 49 deletions

View File

@ -3,35 +3,37 @@ import { spawn } from "node:child_process";
import { app } from "electron"; import { app } from "electron";
export const startAria2 = () => { export const startAria2 = () => {
const binaryPath = app.isPackaged const binaryPath = app.isPackaged
? path.join(process.resourcesPath, "aria2", "aria2c") ? path.join(process.resourcesPath, "aria2", "aria2c")
: path.join(__dirname, "..", "..", "aria2", "aria2c"); : path.join(__dirname, "..", "..", "aria2", "aria2c");
const aria2Process = spawn( const aria2Process = spawn(
binaryPath, binaryPath,
[ [
"--enable-rpc", "--enable-rpc",
"--rpc-listen-all", "--rpc-listen-all",
"--file-allocation=none", "--file-allocation=none",
"--allow-overwrite=true", "--allow-overwrite=true",
"--log-level=debug", "--log-level=debug",
"--no-conf", "--no-conf",
"--disk-cache=128M" "--disk-cache=128M",
], ],
{ stdio: "inherit", windowsHide: true } { stdio: "inherit", windowsHide: true }
); );
aria2Process.on("error", (err) => { aria2Process.on("error", (err) => {
console.error("Aria2 process error:", err); console.error("Aria2 process error:", err);
}); });
aria2Process.on("exit", (code, signal) => { aria2Process.on("exit", (code, signal) => {
if (code !== 0) { if (code !== 0) {
console.error(`Aria2 process exited with code ${code} and signal ${signal}`); console.error(
} else { `Aria2 process exited with code ${code} and signal ${signal}`
console.log("Aria2 process exited successfully"); );
} } else {
}); console.log("Aria2 process exited successfully");
}
});
return aria2Process; return aria2Process;
}; };

View File

@ -55,9 +55,9 @@ export class DownloadManager {
} }
private static getETA( private static getETA(
totalLength: number, totalLength: number,
completedLength: number, completedLength: number,
speed: number speed: number
) { ) {
const remainingBytes = totalLength - completedLength; const remainingBytes = totalLength - completedLength;
@ -80,7 +80,7 @@ export class DownloadManager {
private static async getRealDebridDownloadUrl() { private static async getRealDebridDownloadUrl() {
if (this.realDebridTorrentId) { if (this.realDebridTorrentId) {
const torrentInfo = await RealDebridClient.getTorrentInfo( const torrentInfo = await RealDebridClient.getTorrentInfo(
this.realDebridTorrentId this.realDebridTorrentId
); );
const { status, links } = torrentInfo; const { status, links } = torrentInfo;
@ -107,9 +107,9 @@ export class DownloadManager {
numSeeds: torrentInfo.seeders, numSeeds: torrentInfo.seeders,
downloadSpeed: torrentInfo.speed, downloadSpeed: torrentInfo.speed,
timeRemaining: this.getETA( timeRemaining: this.getETA(
torrentInfo.bytes, torrentInfo.bytes,
totalDownloaded, totalDownloaded,
torrentInfo.speed torrentInfo.speed
), ),
isDownloadingMetadata: status === "magnet_conversion", isDownloadingMetadata: status === "magnet_conversion",
game: { game: {
@ -120,8 +120,8 @@ export class DownloadManager {
} as DownloadProgress; } as DownloadProgress;
WindowManager.mainWindow.webContents.send( WindowManager.mainWindow.webContents.send(
"on-download-progress", "on-download-progress",
JSON.parse(JSON.stringify(payload)) JSON.parse(JSON.stringify(payload))
); );
} }
} }
@ -156,7 +156,7 @@ export class DownloadManager {
} }
const progress = const progress =
Number(status.completedLength) / Number(status.totalLength); Number(status.completedLength) / Number(status.totalLength);
if (!isDownloadingMetadata) { if (!isDownloadingMetadata) {
const update: QueryDeepPartialEntity<Game> = { const update: QueryDeepPartialEntity<Game> = {
@ -168,12 +168,12 @@ export class DownloadManager {
if (!isNaN(progress)) update.progress = progress; if (!isNaN(progress)) update.progress = progress;
await gameRepository.update( await gameRepository.update(
{ id: this.game.id }, { id: this.game.id },
{ {
...update, ...update,
status: status.status, status: status.status,
folderName: this.getFolderName(status), folderName: this.getFolderName(status),
} }
); );
} }
@ -190,17 +190,17 @@ export class DownloadManager {
numSeeds: Number(status.numSeeders ?? 0), numSeeds: Number(status.numSeeders ?? 0),
downloadSpeed: Number(status.downloadSpeed), downloadSpeed: Number(status.downloadSpeed),
timeRemaining: this.getETA( timeRemaining: this.getETA(
Number(status.totalLength), Number(status.totalLength),
Number(status.completedLength), Number(status.completedLength),
Number(status.downloadSpeed) Number(status.downloadSpeed)
), ),
isDownloadingMetadata: !!isDownloadingMetadata, isDownloadingMetadata: !!isDownloadingMetadata,
game, game,
} as DownloadProgress; } as DownloadProgress;
WindowManager.mainWindow.webContents.send( WindowManager.mainWindow.webContents.send(
"on-download-progress", "on-download-progress",
JSON.parse(JSON.stringify(payload)) JSON.parse(JSON.stringify(payload))
); );
} }
@ -292,7 +292,7 @@ export class DownloadManager {
if (game.downloader === Downloader.RealDebrid) { if (game.downloader === Downloader.RealDebrid) {
this.realDebridTorrentId = await RealDebridClient.getTorrentId( this.realDebridTorrentId = await RealDebridClient.getTorrentId(
game!.uri! game!.uri!
); );
} else { } else {
this.gid = await this.aria2.call("addUri", [game.uri!], options); this.gid = await this.aria2.call("addUri", [game.uri!], options);