feat: add initial seeding logic and separation between seeding from downloading

This commit is contained in:
Hachi-R 2024-11-04 03:13:17 -03:00
parent 83b7fb83ab
commit bd8974c7cb
6 changed files with 64 additions and 3 deletions

View file

@ -2,7 +2,12 @@ import { Game } from "@main/entity";
import { Downloader } from "@shared";
import { PythonInstance } from "./python-instance";
import { WindowManager } from "../window-manager";
import { downloadQueueRepository, gameRepository } from "@main/repository";
import {
downloadQueueRepository,
gameRepository,
seedListRepository,
userPreferencesRepository,
} from "@main/repository";
import { publishDownloadCompleteNotification } from "../notifications";
import { RealDebridDownloader } from "./real-debrid-downloader";
import type { DownloadProgress } from "@types";
@ -50,6 +55,22 @@ export class DownloadManager {
await downloadQueueRepository.delete({ game });
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
if (
userPreferences?.seedAfterDownloadCompletes &&
this.currentDownloader === Downloader.Torrent
) {
await seedListRepository.save({
downloadUri: game.uri!,
shouldSeed: true,
});
this.startSeedDownload(game);
}
const [nextQueueItem] = await downloadQueueRepository.find({
order: {
id: "DESC",
@ -136,4 +157,10 @@ export class DownloadManager {
this.currentDownloader = game.downloader;
this.downloadingGameId = game.id;
}
static async startSeedDownload(game: Game) {
if (game) {
await PythonInstance.startSeeding(game);
}
}
}

View file

@ -62,6 +62,7 @@ export class PythonInstance {
public static async getStatus() {
if (this.downloadingGameId === -1) return null;
console.log("getting status");
const response = await this.rpc.get<LibtorrentPayload | null>("/status");
@ -129,7 +130,7 @@ export class PythonInstance {
action: "pause",
game_id: this.downloadingGameId,
} as PauseDownloadPayload)
.catch(() => {});
.catch(() => { });
this.downloadingGameId = -1;
}
@ -161,7 +162,7 @@ export class PythonInstance {
action: "cancel",
game_id: gameId,
} as CancelDownloadPayload)
.catch(() => {});
.catch(() => { });
this.downloadingGameId = -1;
}
@ -174,6 +175,21 @@ export class PythonInstance {
.then((response) => response.data);
}
static async startSeeding(game: Game) {
if (!this.pythonProcess) {
this.spawn();
}
await this.rpc
.post("/action", {
action: "start-seeding",
game_id: game.id,
magnet: game.uri,
save_path: game.downloadPath,
} as StartDownloadPayload)
.catch(() => { });
}
private static async handleRpcError(_error: unknown) {
await this.rpc.get("/healthcheck").catch(() => {
logger.error(

View file

@ -36,3 +36,8 @@ export interface ProcessPayload {
exe: string;
pid: number;
}
export interface SeedPayload {
should_seed: boolean;
}

View file

@ -26,6 +26,8 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.invoke("pauseGameDownload", gameId),
resumeGameDownload: (gameId: number) =>
ipcRenderer.invoke("resumeGameDownload", gameId),
startSeeding: (gameId: number, magnet: string, savePath: string) =>
ipcRenderer.invoke("startSeeding", gameId, magnet, savePath),
onDownloadProgress: (cb: (value: DownloadProgress) => void) => {
const listener = (
_event: Electron.IpcRendererEvent,