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,

View File

@ -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()

View File

@ -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")