we can now download maps from bsaver

This commit is contained in:
MathieuG-P
2022-12-06 00:52:37 +01:00
parent 9dce72250e
commit 8930009398
16 changed files with 309 additions and 24 deletions
+12 -1
View File
@@ -56,7 +56,18 @@ ipcMain.on("delete-maps", async (event, request: IpcRequest<{version: BSVersion,
maps.deleteMaps(request.args.maps, request.args.version).then(() => {
utils.ipcSend<void>(request.responceChannel, {success: true});
}).catch(err => {
utils.ipcSend<void>(request.responceChannel, {success: true, error: err});
utils.ipcSend<void>(request.responceChannel, {success: false, error: err});
});
});
ipcMain.on("download-map", async (event, request: IpcRequest<{zipUrl: string, version: BSVersion}>) => {
const utils = UtilsService.getInstance();
const maps = LocalMapsManagerService.getInstance();
maps.downloadMap(request.args.zipUrl, request.args.version).then(() => {
utils.ipcSend(request.responceChannel, {success: true});
}).catch(err => {
console.log(err);
utils.ipcSend(request.responceChannel, {success: false, error: err});
});
});
@@ -8,6 +8,8 @@ import { UtilsService } from "../utils.service";
import crypto from "crypto";
import { lstatSync, symlinkSync, unlinkSync, readdirSync } from "fs";
import { copySync } from "fs-extra";
import StreamZip from "node-stream-zip";
import { RequestService } from "../request.service";
export class LocalMapsManagerService {
@@ -24,11 +26,13 @@ export class LocalMapsManagerService {
private readonly localVersion: BSLocalVersionService;
private readonly installLocation: InstallationLocationService;
private readonly utils: UtilsService;
private readonly reqService: RequestService
private constructor(){
this.localVersion = BSLocalVersionService.getInstance();
this.installLocation = InstallationLocationService.getInstance();
this.utils = UtilsService.getInstance();
this.reqService = RequestService.getInstance();
}
private async getMapsFolderPath(version?: BSVersion): Promise<string>{
@@ -73,6 +77,18 @@ export class LocalMapsManagerService {
return {rawInfo, coverUrl, songUrl, hash};
}
private async downloadMapZip(zipUrl: string): Promise<{zip: StreamZip.StreamZipAsync, zipPath: string}>{
const fileName = path.basename(zipUrl);
const tempPath = this.utils.getTempPath();
this.utils.createFolderIfNotExist(this.utils.getTempPath());
const dest = path.join(tempPath, fileName);
const zipPath = await this.reqService.downloadFile(zipUrl, dest);
const zip = new StreamZip.async({file : zipPath});
return {zip, zipPath};
}
public async getMaps(version?: BSVersion): Promise<BsmLocalMap[]>{
const levelsFolder = await this.getMapsFolderPath(version);
@@ -146,6 +162,27 @@ export class LocalMapsManagerService {
}
public async downloadMap(zipUrl: string, version?: BSVersion){
console.log(zipUrl, version);
const mapsFolder = await this.getMapsFolderPath(version);
const {zip, zipPath} = await this.downloadMapZip(zipUrl);
const zipName = path.parse(zipPath).name;
const mapPath = path.join(mapsFolder, zipName);
if(!zip){ throw `Cannot download ${zipUrl}`; }
this.utils.createFolderIfNotExist(mapPath);
await zip.extract(null, mapPath);
await zip.close();
unlinkSync(zipPath);
}
}
+1
View File
@@ -27,6 +27,7 @@ export class RequestService {
public downloadFile(url: string, dest: string): Promise<string>{
return new Promise((resolve, reject) => {
const file = createWriteStream(dest);
get(url, res => {
res.pipe(file);