diff --git a/src/main/helpers/fs.helpers.ts b/src/main/helpers/fs.helpers.ts index 1a46beea..98792642 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,18 @@ 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 dirEntries = await readdir(folderPath, {withFileTypes: true}) + + return dirEntries + .filter(entry => entry.isFile()) + .map(file => path.join(folderPath, file.name)); +} + +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 +71,7 @@ export function moveFolderContent(src: string, dest: string): Observable{ - const infoFilePath = path.join(mapPath, "Info.dat"); + const files = await getFilesInFolder(mapPath); + const infoFile = files + .find(file => (path.basename(file).toLowerCase() === "info.dat")) - if(!(await pathExist(infoFilePath))){ return null; } + if(infoFile === null){ return null; } - const rawInfoString = await readFile(infoFilePath, {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; 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 +}