From d59608174d38cfc2c05a2aec75dd89c854c9b628 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Mon, 1 Jul 2024 10:46:02 +0200 Subject: [PATCH] refractor file to unfuck the logic --- src/app/events/gm/GmTileUpload.ts | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/app/events/gm/GmTileUpload.ts b/src/app/events/gm/GmTileUpload.ts index cebf972..d207df3 100644 --- a/src/app/events/gm/GmTileUpload.ts +++ b/src/app/events/gm/GmTileUpload.ts @@ -1,12 +1,11 @@ import { Server } from "socket.io"; -import {TSocket} from "../../utilities/Types"; -import {writeFile} from "node:fs"; -import {randomUUID} from "node:crypto"; +import { TSocket } from "../../utilities/Types"; +import { writeFile } from "node:fs/promises"; import path from "path"; -import fs from "fs"; +import fs from "fs/promises"; - -interface IPayload { +interface ITileData { + [key: string]: Buffer; } /** @@ -15,34 +14,44 @@ interface IPayload { * @param io */ export default function (socket: TSocket, io: Server) { - socket.on('gm:tile:upload', async (data: any, callback: (response: boolean) => void) => { + socket.on('gm:tile:upload', async (data: ITileData, callback: (response: boolean) => void) => { + try { + if (socket.character?.role !== 'gm') { + callback(false); + return; + } - if (socket.character?.role !== 'gm') { - return; - } + const public_folder = path.join(process.cwd(), 'public', 'tiles'); - // get root path - const public_folder = path.join(process.cwd(), 'public', 'tiles'); - - // check if folder exists or create it - if (!fs.existsSync(public_folder)) { - fs.mkdirSync(public_folder, { recursive: true }); - } + // Ensure the folder exists + await fs.mkdir(public_folder, { recursive: true }); - for (const key in data) { - // the files in the folder are named 0.png, 1.png, 2.png etc... check the last file name and add 1 - const files = fs.readdirSync(public_folder); - const lastFile = files.reduce((a, b) => a > b ? a : b, '0.png'); - const lastFileName = lastFile?.split('.')[0]; - const filename = `${parseInt(lastFileName ?? '0') + 1}.png`; - const finalFilePath = path.join(public_folder, filename); - const tile = data[key]; + const files = await fs.readdir(public_folder); + let highestNumber = -1; - // save the tile to the disk, for example - writeFile(finalFilePath, tile, (err) => {}); + // Find the highest number in existing filenames + for (const file of files) { + const match = file.match(/^(\d+)\.png$/); + if (match) { + const number = parseInt(match[1], 10); + if (number > highestNumber) { + highestNumber = number; + } + } + } + + const uploadPromises = Object.entries(data).map(async ([key, tileData], index) => { + const filename = `${highestNumber + index + 1}.png`; + const finalFilePath = path.join(public_folder, filename); + await writeFile(finalFilePath, tileData); + }); + + await Promise.all(uploadPromises); - // return true to the client callback(true); + } catch (error) { + console.error('Error uploading tiles:', error); + callback(false); } }); } \ No newline at end of file