From 17a2bc552e56ba7a4872efac83d0b5e9e55d673b Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Wed, 28 Jun 2023 22:53:26 +0200 Subject: [PATCH 1/3] fix(linux): search for info file case insensitively On linux only exactly Info.dat was matched, and many maps use different casing. --- src/main/helpers/fs.helpers.ts | 29 ++++++++++++++----- .../local-maps-manager.service.ts | 24 ++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/helpers/fs.helpers.ts b/src/main/helpers/fs.helpers.ts index 1a46beea..0cb4002a 100644 --- a/src/main/helpers/fs.helpers.ts +++ b/src/main/helpers/fs.helpers.ts @@ -12,7 +12,7 @@ export async function pathExist(path: string): Promise { }catch(e){ return false; } - + } export async function ensureFolderExist(path: string): Promise { @@ -52,8 +52,21 @@ export async function getFoldersInFolder(folderPath: string, opts?: {ignoreSymli return (await Promise.all(promises)).filter(folder => folder); } -export function moveFolderContent(src: string, dest: string): Observable{ - const progress: Progression = { current: 0, total: 0 }; +export async function getFilesInFolder(folderPath: string): Promise { + if(!(await pathExist(folderPath))){ return []; } + + const files = await readdir(folderPath, {withFileTypes: true}); + + const promises = files.map(async file => { + if(file.isFile()){ return path.join(folderPath, file.name); } + return undefined; + }); + + return (await Promise.all(promises)).filter(file => file); +} + +export function moveFolderContent(src: string, dest: string): Observable{ + const progress: Progression = { current: 0, total: 0 }; return new Observable(subscriber => { subscriber.next(progress); (async () => { @@ -61,7 +74,7 @@ export function moveFolderContent(src: string, dest: string): Observable{ - const infoFilePath = path.join(mapPath, "Info.dat"); + const files = await getFilesInFolder(mapPath); + const infoFilePaths = files + .filter(file => (path.basename(file).toLowerCase() === "info.dat")) - if(!(await pathExist(infoFilePath))){ return null; } + if(infoFilePaths.length === 0){ return null; } - const rawInfoString = await readFile(infoFilePath, {encoding: "utf-8"}); + const rawInfoString = await readFile(infoFilePaths[0], {encoding: "utf-8"}); const rawInfo: RawMapInfoData = JSON.parse(rawInfoString); const coverUrl = new URL(`file:///${path.join(mapPath, rawInfo._coverImageFilename)}`).href; const songUrl = new URL(`file:///${path.join(mapPath, rawInfo._songFilename)}`).href; - + const hash = await this.computeMapHash(mapPath, rawInfoString); - + return {rawInfo, coverUrl, songUrl, hash, path: mapPath}; } @@ -127,7 +129,7 @@ export class LocalMapsManagerService { ipcMain.once("one-click-map-info", async (event, req: IpcRequest) => { this.utils.ipcSend(req.responceChannel, {success: true, data: {id: mapId, isHash}}); }); - + this.windows.openWindow("oneclick-download-map.html"); } @@ -189,7 +191,7 @@ export class LocalMapsManagerService { } public deleteMaps(maps: BsmLocalMap[]): Observable{ - + const mapsFolders = maps.map(map => map.path); const mapsHashsToDelete = maps.map(map => map.hash); @@ -244,7 +246,7 @@ export class LocalMapsManagerService { public async exportMaps(version: BSVersion, maps: BsmLocalMap[], outPath: string){ const archive = new Archive(outPath); - + if(!maps || maps.length === 0){ const mapsFolder = await this.getMapsFolderPath(version); archive.addDirectory(mapsFolder, false); @@ -290,4 +292,4 @@ export class LocalMapsManagerService { return Array.from(Object.values(this.DEEP_LINKS)).every(link => this.deepLink.isDeepLinkRegistred(link)); } -} \ No newline at end of file +} From 3b8a202ba4f3cd4fe62e896326b39ddbc0744249 Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Thu, 29 Jun 2023 21:47:47 +0200 Subject: [PATCH 2/3] refactor: avoid async for iterating over dir entries Also uses nicer functional pipeline instead of loops. --- src/main/helpers/fs.helpers.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/helpers/fs.helpers.ts b/src/main/helpers/fs.helpers.ts index 0cb4002a..98792642 100644 --- a/src/main/helpers/fs.helpers.ts +++ b/src/main/helpers/fs.helpers.ts @@ -55,14 +55,11 @@ export async function getFoldersInFolder(folderPath: string, opts?: {ignoreSymli export async function getFilesInFolder(folderPath: string): Promise { if(!(await pathExist(folderPath))){ return []; } - const files = await readdir(folderPath, {withFileTypes: true}); + const dirEntries = await readdir(folderPath, {withFileTypes: true}) - const promises = files.map(async file => { - if(file.isFile()){ return path.join(folderPath, file.name); } - return undefined; - }); - - return (await Promise.all(promises)).filter(file => file); + return dirEntries + .filter(entry => entry.isFile()) + .map(file => path.join(folderPath, file.name)); } export function moveFolderContent(src: string, dest: string): Observable{ From a4c7ee04dbc3217a5be6cf1c81b0b7c3bc67a0a4 Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Thu, 29 Jun 2023 21:48:27 +0200 Subject: [PATCH 3/3] refactor: use find instead of filter for nicer code and perf --- .../additional-content/local-maps-manager.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/services/additional-content/local-maps-manager.service.ts b/src/main/services/additional-content/local-maps-manager.service.ts index e40c06a0..2f73915f 100644 --- a/src/main/services/additional-content/local-maps-manager.service.ts +++ b/src/main/services/additional-content/local-maps-manager.service.ts @@ -96,12 +96,12 @@ export class LocalMapsManagerService { private async loadMapInfoFromPath(mapPath: string): Promise{ const files = await getFilesInFolder(mapPath); - const infoFilePaths = files - .filter(file => (path.basename(file).toLowerCase() === "info.dat")) + const infoFile = files + .find(file => (path.basename(file).toLowerCase() === "info.dat")) - if(infoFilePaths.length === 0){ return null; } + if(infoFile === null){ return null; } - const rawInfoString = await readFile(infoFilePaths[0], {encoding: "utf-8"}); + const rawInfoString = await readFile(infoFile, {encoding: "utf-8"}); const rawInfo: RawMapInfoData = JSON.parse(rawInfoString); const coverUrl = new URL(`file:///${path.join(mapPath, rawInfo._coverImageFilename)}`).href;