mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
refactor: prettier changes
This commit is contained in:
parent
a16a30761e
commit
e79b6f1391
16 changed files with 380 additions and 332 deletions
|
@ -21,6 +21,5 @@ export namespace GameStatus {
|
|||
GameStatus.Decompressing == status;
|
||||
|
||||
export const isReady = (status: GameStatus | null) =>
|
||||
status === GameStatus.Finished ||
|
||||
status === GameStatus.Seeding;
|
||||
status === GameStatus.Finished || status === GameStatus.Seeding;
|
||||
}
|
|
@ -35,4 +35,3 @@ export class UserPreferences {
|
|||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ const cancelGameDownload = async (
|
|||
game.status !== GameStatus.Seeding
|
||||
) {
|
||||
Downloader.cancelDownload();
|
||||
if (result.affected) WindowManager.mainWindow.setProgressBar(-1);
|
||||
if (result.affected) WindowManager.mainWindow?.setProgressBar(-1);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { getSteamGameIconUrl, writePipe } from "@main/services";
|
||||
import { getSteamGameIconUrl } from "@main/services";
|
||||
import { gameRepository, repackRepository } from "@main/repository";
|
||||
|
||||
import { registerEvent } from "../register-event";
|
||||
|
|
|
@ -10,7 +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';
|
||||
import path from "node:path";
|
||||
|
||||
interface DownloadStatus {
|
||||
numPeers: number;
|
||||
|
@ -23,12 +23,14 @@ export class Downloader {
|
|||
private static lastHttpDownloader: HTTPDownloader | null = null;
|
||||
|
||||
static async usesRealDebrid() {
|
||||
const userPreferences = await userPreferencesRepository.findOne({ where: { id: 1 } });
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
return userPreferences!.realDebridApiToken !== null;
|
||||
}
|
||||
|
||||
static async cancelDownload() {
|
||||
if (!await this.usesRealDebrid()) {
|
||||
if (!(await this.usesRealDebrid())) {
|
||||
writePipe.write({ action: "cancel" });
|
||||
} else {
|
||||
if (this.lastHttpDownloader) {
|
||||
|
@ -38,7 +40,7 @@ export class Downloader {
|
|||
}
|
||||
|
||||
static async pauseDownload() {
|
||||
if (!await this.usesRealDebrid()) {
|
||||
if (!(await this.usesRealDebrid())) {
|
||||
writePipe.write({ action: "pause" });
|
||||
} else {
|
||||
if (this.lastHttpDownloader) {
|
||||
|
@ -48,7 +50,7 @@ export class Downloader {
|
|||
}
|
||||
|
||||
static async resumeDownload() {
|
||||
if (!await this.usesRealDebrid()) {
|
||||
if (!(await this.usesRealDebrid())) {
|
||||
writePipe.write({ action: "pause" });
|
||||
} else {
|
||||
if (this.lastHttpDownloader) {
|
||||
|
@ -58,7 +60,7 @@ export class Downloader {
|
|||
}
|
||||
|
||||
static async downloadGame(game: Game, repack: Repack) {
|
||||
if (!await this.usesRealDebrid()) {
|
||||
if (!(await this.usesRealDebrid())) {
|
||||
writePipe.write({
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
|
@ -73,7 +75,11 @@ 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);
|
||||
|
@ -81,7 +87,11 @@ export class Downloader {
|
|||
}
|
||||
}
|
||||
|
||||
static async updateGameProgress(gameId: number, gameUpdate: QueryDeepPartialEntity<Game>, downloadStatus: DownloadStatus) {
|
||||
static async updateGameProgress(
|
||||
gameId: number,
|
||||
gameUpdate: QueryDeepPartialEntity<Game>,
|
||||
downloadStatus: DownloadStatus
|
||||
) {
|
||||
await gameRepository.update({ id: gameId }, gameUpdate);
|
||||
|
||||
const game = await gameRepository.findOne({
|
||||
|
@ -89,7 +99,10 @@ export class Downloader {
|
|||
relations: { repack: true },
|
||||
});
|
||||
|
||||
if (gameUpdate.progress === 1 && gameUpdate.status !== GameStatus.Decompressing) {
|
||||
if (
|
||||
gameUpdate.progress === 1 &&
|
||||
gameUpdate.status !== GameStatus.Decompressing
|
||||
) {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
|
@ -109,13 +122,24 @@ export class Downloader {
|
|||
}
|
||||
}
|
||||
|
||||
if (game && gameUpdate.decompressionProgress === 0 && gameUpdate.status === GameStatus.Decompressing) {
|
||||
const unrar = await Unrar.fromFilePath(game.rarPath!, path.join(game.downloadPath!, game.folderName!));
|
||||
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, {
|
||||
this.updateGameProgress(
|
||||
gameId,
|
||||
{
|
||||
decompressionProgress: 1,
|
||||
status: GameStatus.Finished,
|
||||
}, downloadStatus);
|
||||
},
|
||||
downloadStatus
|
||||
);
|
||||
}
|
||||
|
||||
if (WindowManager.mainWindow && game) {
|
||||
|
@ -124,8 +148,9 @@ export class Downloader {
|
|||
|
||||
WindowManager.mainWindow.webContents.send(
|
||||
"on-download-progress",
|
||||
JSON.parse(JSON.stringify({
|
||||
...{
|
||||
JSON.parse(
|
||||
JSON.stringify({
|
||||
...({
|
||||
progress: gameUpdate.progress,
|
||||
bytesDownloaded: gameUpdate.bytesDownloaded,
|
||||
fileSize: gameUpdate.fileSize,
|
||||
|
@ -134,15 +159,19 @@ export class Downloader {
|
|||
numSeeds: downloadStatus.numSeeds,
|
||||
downloadSpeed: downloadStatus.downloadSpeed,
|
||||
timeRemaining: downloadStatus.timeRemaining,
|
||||
} as TorrentUpdate, game
|
||||
}))
|
||||
} as TorrentUpdate),
|
||||
game,
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static getGameProgress(game: Game) {
|
||||
if (game.status === GameStatus.CheckingFiles) return game.fileVerificationProgress;
|
||||
if (game.status === GameStatus.Decompressing) return game.decompressionProgress;
|
||||
if (game.status === GameStatus.CheckingFiles)
|
||||
return game.fileVerificationProgress;
|
||||
if (game.status === GameStatus.Decompressing)
|
||||
return game.decompressionProgress;
|
||||
return game.progress;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
import { Game } from '@main/entity';
|
||||
import { ElectronDownloadManager } from 'electron-dl-manager';
|
||||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
|
||||
import { WindowManager } from '../window-manager';
|
||||
import { Downloader } from './downloader';
|
||||
import { GameStatus } from '@globals';
|
||||
import { Game } from "@main/entity";
|
||||
import { ElectronDownloadManager } from "electron-dl-manager";
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import { Downloader } from "./downloader";
|
||||
import { GameStatus } from "@globals";
|
||||
|
||||
function dropExtension(fileName: string) {
|
||||
return fileName.split('.').slice(0, -1).join('.');
|
||||
return fileName.split(".").slice(0, -1).join(".");
|
||||
}
|
||||
|
||||
export class HTTPDownloader {
|
||||
|
@ -31,7 +31,7 @@ export class HTTPDownloader {
|
|||
bytesDownloaded: 0,
|
||||
fileSize: ev.item.getTotalBytes(),
|
||||
rarPath: `${destination}/.rd/${ev.resolvedFilename}`,
|
||||
folderName: dropExtension(ev.resolvedFilename)
|
||||
folderName: dropExtension(ev.resolvedFilename),
|
||||
};
|
||||
const downloadStatus = {
|
||||
numPeers: 0,
|
||||
|
@ -39,7 +39,11 @@ export class HTTPDownloader {
|
|||
downloadSpeed: 0,
|
||||
timeRemaining: Number.POSITIVE_INFINITY,
|
||||
};
|
||||
await Downloader.updateGameProgress(gameId, updatePayload, downloadStatus);
|
||||
await Downloader.updateGameProgress(
|
||||
gameId,
|
||||
updatePayload,
|
||||
downloadStatus
|
||||
);
|
||||
},
|
||||
onDownloadCompleted: async (ev) => {
|
||||
const updatePayload: QueryDeepPartialEntity<Game> = {
|
||||
|
@ -54,7 +58,11 @@ export class HTTPDownloader {
|
|||
downloadSpeed: 0,
|
||||
timeRemaining: 0,
|
||||
};
|
||||
await Downloader.updateGameProgress(gameId, updatePayload, downloadStatus);
|
||||
await Downloader.updateGameProgress(
|
||||
gameId,
|
||||
updatePayload,
|
||||
downloadStatus
|
||||
);
|
||||
},
|
||||
onDownloadProgress: async (ev) => {
|
||||
const updatePayload: QueryDeepPartialEntity<Game> = {
|
||||
|
@ -67,8 +75,12 @@ export class HTTPDownloader {
|
|||
downloadSpeed: ev.downloadRateBytesPerSecond,
|
||||
timeRemaining: ev.estimatedTimeRemainingSeconds,
|
||||
};
|
||||
await Downloader.updateGameProgress(gameId, updatePayload, downloadStatus);
|
||||
}
|
||||
await Downloader.updateGameProgress(
|
||||
gameId,
|
||||
updatePayload,
|
||||
downloadStatus
|
||||
);
|
||||
},
|
||||
},
|
||||
directory: `${destination}/.rd/`,
|
||||
});
|
||||
|
|
|
@ -13,41 +13,41 @@ export interface RealDebridUnrestrictLink {
|
|||
}
|
||||
|
||||
export interface RealDebridAddMagnet {
|
||||
"id": string,
|
||||
id: string;
|
||||
// URL of the created ressource
|
||||
"uri": string
|
||||
uri: string;
|
||||
}
|
||||
|
||||
export interface RealDebridTorrentInfo {
|
||||
"id": string,
|
||||
"filename": string,
|
||||
"original_filename": string, // Original name of the torrent
|
||||
"hash": string, // SHA1 Hash of the torrent
|
||||
"bytes": number, // Size of selected files only
|
||||
"original_bytes": number, // Total size of the torrent
|
||||
"host": string, // Host main domain
|
||||
"split": number, // Split size of links
|
||||
"progress": number, // Possible values: 0 to 100
|
||||
"status": "downloaded", // Current status of the torrent: magnet_error, magnet_conversion, waiting_files_selection, queued, downloading, downloaded, error, virus, compressing, uploading, dead
|
||||
"added": string, // jsonDate
|
||||
"files": [
|
||||
id: string;
|
||||
filename: string;
|
||||
original_filename: string; // Original name of the torrent
|
||||
hash: string; // SHA1 Hash of the torrent
|
||||
bytes: number; // Size of selected files only
|
||||
original_bytes: number; // Total size of the torrent
|
||||
host: string; // Host main domain
|
||||
split: number; // Split size of links
|
||||
progress: number; // Possible values: 0 to 100
|
||||
status: "downloaded"; // Current status of the torrent: magnet_error, magnet_conversion, waiting_files_selection, queued, downloading, downloaded, error, virus, compressing, uploading, dead
|
||||
added: string; // jsonDate
|
||||
files: [
|
||||
{
|
||||
"id": number,
|
||||
"path": string, // Path to the file inside the torrent, starting with "/"
|
||||
"bytes": number,
|
||||
"selected": number // 0 or 1
|
||||
id: number;
|
||||
path: string; // Path to the file inside the torrent, starting with "/"
|
||||
bytes: number;
|
||||
selected: number; // 0 or 1
|
||||
},
|
||||
{
|
||||
"id": number,
|
||||
"path": string, // Path to the file inside the torrent, starting with "/"
|
||||
"bytes": number,
|
||||
"selected": number // 0 or 1
|
||||
}
|
||||
],
|
||||
"links": [
|
||||
"string" // Host URL
|
||||
],
|
||||
"ended": string, // !! Only present when finished, jsonDate
|
||||
"speed": number, // !! Only present in "downloading", "compressing", "uploading" status
|
||||
"seeders": number // !! Only present in "downloading", "magnet_conversion" status
|
||||
id: number;
|
||||
path: string; // Path to the file inside the torrent, starting with "/"
|
||||
bytes: number;
|
||||
selected: number; // 0 or 1
|
||||
},
|
||||
];
|
||||
links: [
|
||||
"string", // Host URL
|
||||
];
|
||||
ended: string; // !! Only present when finished, jsonDate
|
||||
speed: number; // !! Only present in "downloading", "compressing", "uploading" status
|
||||
seeders: number; // !! Only present in "downloading", "magnet_conversion" status
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
import { userPreferencesRepository } from "@main/repository";
|
||||
import fetch from "node-fetch";
|
||||
import { RealDebridAddMagnet, RealDebridTorrentInfo, RealDebridUnrestrictLink } from "./real-debrid-types";
|
||||
import {
|
||||
RealDebridAddMagnet,
|
||||
RealDebridTorrentInfo,
|
||||
RealDebridUnrestrictLink,
|
||||
} from "./real-debrid-types";
|
||||
|
||||
const base = "https://api.real-debrid.com/rest/1.0";
|
||||
|
||||
|
@ -9,9 +13,9 @@ export class RealDebridClient {
|
|||
const response = await fetch(`${base}/torrents/addMagnet`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${await this.getApiToken()}`,
|
||||
Authorization: `Bearer ${await this.getApiToken()}`,
|
||||
},
|
||||
body: `magnet=${encodeURIComponent(magnet)}`
|
||||
body: `magnet=${encodeURIComponent(magnet)}`,
|
||||
});
|
||||
|
||||
return response.json() as Promise<RealDebridAddMagnet>;
|
||||
|
@ -20,20 +24,20 @@ export class RealDebridClient {
|
|||
static async getInfo(id: string) {
|
||||
const response = await fetch(`${base}/torrents/info/${id}`, {
|
||||
headers: {
|
||||
"Authorization": `Bearer ${await this.getApiToken()}`
|
||||
}
|
||||
Authorization: `Bearer ${await this.getApiToken()}`,
|
||||
},
|
||||
});
|
||||
|
||||
return response.json() as Promise<RealDebridTorrentInfo>;
|
||||
}
|
||||
|
||||
static async selectAllFiles(id: string) {
|
||||
const response = await fetch(`${base}/torrents/selectFiles/${id}`, {
|
||||
await fetch(`${base}/torrents/selectFiles/${id}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${await this.getApiToken()}`,
|
||||
Authorization: `Bearer ${await this.getApiToken()}`,
|
||||
},
|
||||
body: "files=all"
|
||||
body: "files=all",
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -41,15 +45,17 @@ export class RealDebridClient {
|
|||
const response = await fetch(`${base}/unrestrict/link`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${await this.getApiToken()}`,
|
||||
Authorization: `Bearer ${await this.getApiToken()}`,
|
||||
},
|
||||
body: `link=${link}`
|
||||
body: `link=${link}`,
|
||||
});
|
||||
|
||||
return response.json() as Promise<RealDebridUnrestrictLink>;
|
||||
}
|
||||
|
||||
static getApiToken() {
|
||||
return userPreferencesRepository.findOne({ where: { id: 1 } }).then(userPreferences => userPreferences!.realDebridApiToken);
|
||||
return userPreferencesRepository
|
||||
.findOne({ where: { id: 1 } })
|
||||
.then((userPreferences) => userPreferences!.realDebridApiToken);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
import { Extractor, createExtractorFromFile } from 'node-unrar-js';
|
||||
import fs from 'node:fs';
|
||||
import { Extractor, createExtractorFromFile } from "node-unrar-js";
|
||||
import fs from "node:fs";
|
||||
|
||||
const wasmBinary = fs.readFileSync(require.resolve('node-unrar-js/esm/js/unrar.wasm'));
|
||||
const wasmBinary = fs.readFileSync(
|
||||
require.resolve("node-unrar-js/esm/js/unrar.wasm")
|
||||
);
|
||||
|
||||
export class Unrar {
|
||||
private constructor(private extractor: Extractor<Uint8Array>) { }
|
||||
private constructor(private extractor: Extractor<Uint8Array>) {}
|
||||
|
||||
static async fromFilePath(filePath: string, targetFolder: string) {
|
||||
const extractor = await createExtractorFromFile({
|
||||
|
|
|
@ -118,7 +118,8 @@ export function Sidebar() {
|
|||
}, [isResizing]);
|
||||
|
||||
const getGameTitle = (game: Game) => {
|
||||
if (game.status === GameStatus.Paused) return t("paused", { title: game.title });
|
||||
if (game.status === GameStatus.Paused)
|
||||
return t("paused", { title: game.title });
|
||||
|
||||
if (gameDownloading?.id === game.id) {
|
||||
const isVerifying = GameStatus.isVerifying(gameDownloading.status);
|
||||
|
|
|
@ -99,7 +99,7 @@ export function useDownload() {
|
|||
dispatch(setGameDeleting(gameId));
|
||||
return window.electron.deleteGameFolder(gameId);
|
||||
})
|
||||
.catch(() => { })
|
||||
.catch(() => {})
|
||||
.finally(() => {
|
||||
updateLibrary();
|
||||
dispatch(removeGameFromDeleting(gameId));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue