feat: adding recursive automatic extraction

This commit is contained in:
Chubby Granny Chaser
2025-04-02 11:25:56 +01:00
parent d15ef33a86
commit 5fa4d128c3
14 changed files with 120 additions and 59 deletions

View File

@@ -1,6 +1,7 @@
import { app } from "electron";
import cp from "node:child_process";
import path from "node:path";
import { logger } from "./logger";
export const binaryName = {
linux: "7zzs",
@@ -20,19 +21,55 @@ export class _7Zip {
);
public static extractFile(
filePath: string,
outputPath: string,
cb: () => void
) {
const child = cp.spawn(this.binaryPath, [
"x",
{
filePath,
`-o"${outputPath}"`,
"-y",
]);
outputPath,
cwd,
passwords = [],
}: {
filePath: string;
outputPath?: string;
cwd?: string;
passwords?: string[];
},
cb: (success: boolean) => void
) {
const tryPassword = (index = -1) => {
const password = passwords[index] ?? "";
logger.info(`Trying password ${password} on ${filePath}`);
child.on("exit", () => {
cb();
});
const args = ["x", filePath, "-y", "-p" + password];
if (outputPath) {
args.push("-o" + outputPath);
}
const child = cp.execFile(this.binaryPath, args, {
cwd,
});
child.once("exit", (code) => {
console.log("EXIT CALLED", code, filePath);
if (code === 0) {
cb(true);
return;
}
if (index < passwords.length - 1) {
logger.info(
`Failed to extract file: ${filePath} with password: ${password}. Trying next password...`
);
tryPassword(index + 1);
} else {
logger.info(`Failed to extract file: ${filePath}`);
cb(false);
}
});
};
tryPassword();
}
}

View File

@@ -16,6 +16,12 @@ export class Aria2 {
"--rpc-listen-all",
"--file-allocation=none",
"--allow-overwrite=true",
"-s",
"16",
"-x",
"16",
"-k",
"1M",
],
{ stdio: "inherit", windowsHide: true }
);

View File

@@ -1,9 +1,6 @@
import { Downloader, DownloadError } from "@shared";
import { WindowManager } from "../window-manager";
import {
publishDownloadCompleteNotification,
publishExtractionCompleteNotification,
} from "../notifications";
import { publishDownloadCompleteNotification } from "../notifications";
import type { Download, DownloadProgress, UserPreferences } from "@types";
import {
GofileApi,
@@ -25,11 +22,11 @@ import { logger } from "../logger";
import { db, downloadsSublevel, gamesSublevel, levelKeys } from "@main/level";
import { sortBy } from "lodash-es";
import { TorBoxClient } from "./torbox";
import { _7Zip } from "../7zip";
import { FILE_EXTENSIONS_TO_EXTRACT } from "@shared";
import { GameFilesManager } from "../game-files-manager";
export class DownloadManager {
private static downloadingGameId: string | null = null;
private static readonly extensionsToExtract = [".rar", ".zip", ".7z"];
public static async startRPC(
download?: Download,
@@ -155,12 +152,7 @@ export class DownloadManager {
queued: false,
});
} else {
const shouldExtract =
download.downloader !== Downloader.Torrent &&
this.extensionsToExtract.some((ext) =>
download.folderName?.endsWith(ext)
) &&
download.automaticallyExtract;
const shouldExtract = download.automaticallyExtract;
downloadsSublevel.put(gameId, {
...download,
@@ -171,29 +163,26 @@ export class DownloadManager {
});
if (shouldExtract) {
_7Zip.extractFile(
path.join(download.downloadPath, download.folderName!),
path.join(
download.downloadPath,
path.parse(download.folderName!).name
),
async () => {
const download = await downloadsSublevel.get(gameId);
downloadsSublevel.put(gameId, {
...download!,
extracting: false,
});
WindowManager.mainWindow?.webContents.send(
"on-extraction-complete",
game.shop,
game.objectId
);
publishExtractionCompleteNotification(game);
}
const gameFilesManager = new GameFilesManager(
game.shop,
game.objectId
);
if (
FILE_EXTENSIONS_TO_EXTRACT.some((ext) =>
download.folderName?.endsWith(ext)
)
) {
gameFilesManager.extractDownloadedFile();
} else {
gameFilesManager
.extractFilesInDirectory(
path.join(download.downloadPath, download.folderName!)
)
.then(() => {
gameFilesManager.setExtractionComplete();
});
}
}
this.cancelDownload(gameId);

View File

@@ -9,3 +9,4 @@ export * from "./hydra-api";
export * from "./ludusavi";
export * from "./cloud-sync";
export * from "./7zip";
export * from "./game-files-manager";