diff --git a/src/main/services/donwloaders/downloader.ts b/src/main/services/donwloaders/downloader.ts index 9c3f1a72..c5f29fae 100644 --- a/src/main/services/donwloaders/downloader.ts +++ b/src/main/services/donwloaders/downloader.ts @@ -10,6 +10,7 @@ import { TorrentUpdate } from "./torrent-client"; import { HTTPDownloader } from "./http-downloader"; import { Unrar } from "../unrar"; import { GameStatus } from "@globals"; +import path from 'node:path'; interface DownloadStatus { numPeers: number; @@ -23,7 +24,7 @@ export class Downloader { static async usesRealDebrid() { const userPreferences = await userPreferencesRepository.findOne({ where: { id: 1 } }); - return userPreferences.realDebridApiToken !== null; + return userPreferences!.realDebridApiToken !== null; } static async cancelDownload() { @@ -72,7 +73,7 @@ export class Downloader { const { links } = await RealDebridClient.getInfo(torrent.id); const { download } = await RealDebridClient.unrestrictLink(links[0]); this.lastHttpDownloader = new HTTPDownloader(); - this.lastHttpDownloader.download(download, game.downloadPath, game.id); + this.lastHttpDownloader.download(download, game.downloadPath!, game.id); } } catch (e) { console.error(e); @@ -102,14 +103,14 @@ export class Downloader { body: t("game_ready_to_install", { ns: "notifications", lng: userPreferences.language, - title: game.title, + title: game?.title, }), }).show(); } } - if (gameUpdate.decompressionProgress === 0 && gameUpdate.status === GameStatus.Decompressing) { - const unrar = await Unrar.fromFilePath(game.rarPath, game.downloadPath); + if (game && gameUpdate.decompressionProgress === 0 && gameUpdate.status === GameStatus.Decompressing) { + const unrar = await Unrar.fromFilePath(game.rarPath!, path.join(game.downloadPath!, game.folderName!)); unrar.extract(); this.updateGameProgress(gameId, { decompressionProgress: 1, @@ -117,7 +118,7 @@ export class Downloader { }, downloadStatus); } - if (WindowManager.mainWindow) { + if (WindowManager.mainWindow && game) { const progress = this.getGameProgress(game); WindowManager.mainWindow.setProgressBar(progress === 1 ? -1 : progress); diff --git a/src/main/services/donwloaders/http-downloader.ts b/src/main/services/donwloaders/http-downloader.ts index aec963e0..01c3218f 100644 --- a/src/main/services/donwloaders/http-downloader.ts +++ b/src/main/services/donwloaders/http-downloader.ts @@ -5,6 +5,10 @@ import { WindowManager } from '../window-manager'; import { Downloader } from './downloader'; import { GameStatus } from '@globals'; +function dropExtension(fileName: string) { + return fileName.split('.').slice(0, -1).join('.'); +} + export class HTTPDownloader { private downloadManager: ElectronDownloadManager; private downloadId: string | null = null; @@ -18,7 +22,7 @@ export class HTTPDownloader { this.downloadId = await this.downloadManager.download({ url, - window: window, + window: window!, callbacks: { onDownloadStarted: async (ev) => { const updatePayload: QueryDeepPartialEntity = { @@ -27,6 +31,7 @@ export class HTTPDownloader { bytesDownloaded: 0, fileSize: ev.item.getTotalBytes(), rarPath: `${destination}/.rd/${ev.resolvedFilename}`, + folderName: dropExtension(ev.resolvedFilename) }; const downloadStatus = { numPeers: 0,