diff --git a/src/main/services/download/download-manager.ts b/src/main/services/download/download-manager.ts index 1f9383e1..6a5205f0 100644 --- a/src/main/services/download/download-manager.ts +++ b/src/main/services/download/download-manager.ts @@ -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); + } + } } diff --git a/src/main/services/download/python-instance.ts b/src/main/services/download/python-instance.ts index f59b20b8..ad08e773 100644 --- a/src/main/services/download/python-instance.ts +++ b/src/main/services/download/python-instance.ts @@ -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("/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( diff --git a/src/main/services/download/types.ts b/src/main/services/download/types.ts index fd8009a2..ec8209a1 100644 --- a/src/main/services/download/types.ts +++ b/src/main/services/download/types.ts @@ -36,3 +36,8 @@ export interface ProcessPayload { exe: string; pid: number; } + +export interface SeedPayload { + should_seed: boolean; +} + diff --git a/src/preload/index.ts b/src/preload/index.ts index 9baa8325..52e713fa 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -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, diff --git a/torrent-client/main.py b/torrent-client/main.py index d62150b8..ee976518 100644 --- a/torrent-client/main.py +++ b/torrent-client/main.py @@ -107,6 +107,8 @@ class Handler(BaseHTTPRequestHandler): elif data['action'] == 'kill-torrent': torrent_downloader.abort_session() torrent_downloader = None + elif data['action'] == 'start-seeding': + torrent_downloader.start_seeding(data['game_id'], data['magnet'], data['save_path']) self.send_response(200) self.end_headers() diff --git a/torrent-client/torrent_downloader.py b/torrent-client/torrent_downloader.py index b5280260..bcb13ffe 100644 --- a/torrent-client/torrent_downloader.py +++ b/torrent-client/torrent_downloader.py @@ -163,3 +163,12 @@ class TorrentDownloader: self.downloading_game_id = -1 return response + + def start_seeding(self, game_id: int, magnet: str, save_path: str): + print("seed log 1") + params = {'url': magnet, 'save_path': save_path, 'trackers': self.trackers} + torrent_handle = self.session.add_torrent(params) + self.torrent_handles[game_id] = torrent_handle + torrent_handle.set_flags(lt.torrent_flags.seed_mode) + torrent_handle.resume() + print("seed log 2")