mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-15 04:32:13 +00:00
feat: implement directory size check and seed abort
This commit is contained in:
parent
bd184fceda
commit
abb3db4e30
2 changed files with 53 additions and 11 deletions
|
@ -15,9 +15,10 @@ import {
|
||||||
LibtorrentStatus,
|
LibtorrentStatus,
|
||||||
PauseDownloadPayload,
|
PauseDownloadPayload,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import { calculateETA } from "./helpers";
|
import { calculateETA, getDirSize } from "./helpers";
|
||||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
||||||
import { RealDebridClient } from "./real-debrid";
|
import { RealDebridClient } from "./real-debrid";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export class DownloadManager {
|
export class DownloadManager {
|
||||||
private static downloadingGameId: number | null = null;
|
private static downloadingGameId: number | null = null;
|
||||||
|
@ -123,7 +124,7 @@ export class DownloadManager {
|
||||||
{ status: "complete", shouldSeed: false }
|
{ status: "complete", shouldSeed: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
this.pauseSeeding(gameId);
|
this.cancelDownload(gameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
await downloadQueueRepository.delete({ game });
|
await downloadQueueRepository.delete({ game });
|
||||||
|
@ -151,6 +152,30 @@ export class DownloadManager {
|
||||||
|
|
||||||
if (!seedStatus.data.length) return;
|
if (!seedStatus.data.length) return;
|
||||||
|
|
||||||
|
seedStatus.data.forEach(async (status) => {
|
||||||
|
const game = await gameRepository.findOne({
|
||||||
|
where: { id: status.gameId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!game) return;
|
||||||
|
|
||||||
|
const totalSize = await getDirSize(
|
||||||
|
path.join(game.downloadPath!, status.folderName!)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (totalSize < status.fileSize) {
|
||||||
|
await this.cancelDownload(game.id);
|
||||||
|
|
||||||
|
await gameRepository.update(game.id, {
|
||||||
|
status: "paused",
|
||||||
|
shouldSeed: false,
|
||||||
|
progress: totalSize / status.fileSize,
|
||||||
|
});
|
||||||
|
|
||||||
|
WindowManager.mainWindow?.webContents.send("on-hard-delete");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
WindowManager.mainWindow?.webContents.send(
|
WindowManager.mainWindow?.webContents.send(
|
||||||
"on-seeding-status",
|
"on-seeding-status",
|
||||||
JSON.parse(JSON.stringify(seedStatus.data))
|
JSON.parse(JSON.stringify(seedStatus.data))
|
||||||
|
@ -212,15 +237,6 @@ export class DownloadManager {
|
||||||
// );
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
static async pauseSeeding(gameId: number) {
|
|
||||||
await PythonRPC.rpc
|
|
||||||
.post("/action", {
|
|
||||||
action: "pause",
|
|
||||||
game_id: gameId,
|
|
||||||
} as PauseDownloadPayload)
|
|
||||||
.catch(() => {});
|
|
||||||
}
|
|
||||||
|
|
||||||
static async pauseDownload() {
|
static async pauseDownload() {
|
||||||
await PythonRPC.rpc
|
await PythonRPC.rpc
|
||||||
.post("/action", {
|
.post("/action", {
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
import path from "node:path";
|
||||||
|
import fs from "node:fs";
|
||||||
|
|
||||||
export const calculateETA = (
|
export const calculateETA = (
|
||||||
totalLength: number,
|
totalLength: number,
|
||||||
completedLength: number,
|
completedLength: number,
|
||||||
|
@ -11,3 +14,26 @@ export const calculateETA = (
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getDirSize = async (dir: string): Promise<number> => {
|
||||||
|
const getItemSize = async (filePath: string): Promise<number> => {
|
||||||
|
const stat = await fs.promises.stat(filePath);
|
||||||
|
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
return getDirSize(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stat.size;
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const files = await fs.promises.readdir(dir);
|
||||||
|
const filePaths = files.map((file) => path.join(dir, file));
|
||||||
|
const sizes = await Promise.all(filePaths.map(getItemSize));
|
||||||
|
|
||||||
|
return sizes.reduce((total, size) => total + size, 0);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue