1
0
forked from noxious/server
noxious_server/src/app/events/gm/GmTileUpload.ts
2024-06-23 01:53:06 +02:00

39 lines
1.1 KiB
TypeScript

import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types";
import {writeFile} from "node:fs";
import {randomUUID} from "node:crypto";
import path from "path";
import fs from "fs";
interface IPayload {
}
/**
* Handle game master upload tile event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:tile:upload', async (data: any, callback: (response: boolean) => void) => {
// 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 });
}
for (const key in data) {
const filename = randomUUID();
const finalFilePath = path.join(public_folder, filename);
const tile = data[key];
// save the tile to the disk, for example
writeFile(finalFilePath, tile, (err) => {});
callback(true);
}
});
}