forked from noxious/server
Code improvements
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../../utilities/Types";
|
||||
import fs from 'fs';
|
||||
import path from "path";
|
||||
|
||||
interface IPayload {
|
||||
}
|
||||
@ -14,8 +15,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:list', async (data: any, callback: (response: string[]) => void) => {
|
||||
|
||||
// get root path
|
||||
const root_folder = process.cwd();
|
||||
const folder = `${root_folder}/public/tiles`;
|
||||
const folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
|
||||
// list the files in the folder
|
||||
let tiles: string[] = [];
|
||||
|
@ -2,6 +2,8 @@ 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 {
|
||||
@ -13,21 +15,25 @@ interface IPayload {
|
||||
* @param io
|
||||
*/
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('gm:tile:upload', async (data: any) => {
|
||||
socket.on('gm:tile:upload', async (data: any, callback: (response: boolean) => void) => {
|
||||
|
||||
// get root path
|
||||
const root_folder = process.cwd();
|
||||
const public_folder = `${root_folder}/public`;
|
||||
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 path = `${public_folder}/tiles/${filename}.png`;
|
||||
const finalFilePath = path.join(public_folder, filename);
|
||||
const tile = data[key];
|
||||
|
||||
// save the tile to the disk, for example
|
||||
writeFile(path, tile, (err) => {
|
||||
// pajeet INC
|
||||
});
|
||||
writeFile(finalFilePath, tile, (err) => {});
|
||||
|
||||
callback(true);
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user