mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-04 01:03:47 +03:00
77 lines
1.7 KiB
TypeScript
77 lines
1.7 KiB
TypeScript
|
import { gameRepository } from "@main/repository";
|
||
|
|
||
|
import type { Game } from "@main/entity";
|
||
|
import { Downloader } from "@shared";
|
||
|
|
||
|
import { writePipe } from "./fifo";
|
||
|
import { HTTPDownloader } from "./downloaders";
|
||
|
|
||
|
export class DownloadManager {
|
||
|
private static gameDownloading: Game;
|
||
|
|
||
|
static async getGame(gameId: number) {
|
||
|
return gameRepository.findOne({
|
||
|
where: { id: gameId, isDeleted: false },
|
||
|
relations: {
|
||
|
repack: true,
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
|
||
|
static async cancelDownload() {
|
||
|
if (
|
||
|
this.gameDownloading &&
|
||
|
this.gameDownloading.downloader === Downloader.Torrent
|
||
|
) {
|
||
|
writePipe.write({ action: "cancel" });
|
||
|
} else {
|
||
|
HTTPDownloader.destroy();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static async pauseDownload() {
|
||
|
if (
|
||
|
this.gameDownloading &&
|
||
|
this.gameDownloading.downloader === Downloader.Torrent
|
||
|
) {
|
||
|
writePipe.write({ action: "pause" });
|
||
|
} else {
|
||
|
HTTPDownloader.destroy();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static async resumeDownload(gameId: number) {
|
||
|
const game = await this.getGame(gameId);
|
||
|
|
||
|
if (game!.downloader === Downloader.Torrent) {
|
||
|
writePipe.write({
|
||
|
action: "start",
|
||
|
game_id: game!.id,
|
||
|
magnet: game!.repack.magnet,
|
||
|
save_path: game!.downloadPath,
|
||
|
});
|
||
|
} else {
|
||
|
HTTPDownloader.startDownload(game!);
|
||
|
}
|
||
|
|
||
|
this.gameDownloading = game!;
|
||
|
}
|
||
|
|
||
|
static async downloadGame(gameId: number) {
|
||
|
const game = await this.getGame(gameId);
|
||
|
|
||
|
if (game!.downloader === Downloader.Torrent) {
|
||
|
writePipe.write({
|
||
|
action: "start",
|
||
|
game_id: game!.id,
|
||
|
magnet: game!.repack.magnet,
|
||
|
save_path: game!.downloadPath,
|
||
|
});
|
||
|
} else {
|
||
|
HTTPDownloader.startDownload(game!);
|
||
|
}
|
||
|
|
||
|
this.gameDownloading = game!;
|
||
|
}
|
||
|
}
|