diff --git a/src/app/events/gm/GmTileList.ts b/src/app/events/gm/GmTileList.ts index c068e0f..2c91d7a 100644 --- a/src/app/events/gm/GmTileList.ts +++ b/src/app/events/gm/GmTileList.ts @@ -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[] = []; diff --git a/src/app/events/gm/GmTileUpload.ts b/src/app/events/gm/GmTileUpload.ts index eb1dc49..0be96b1 100644 --- a/src/app/events/gm/GmTileUpload.ts +++ b/src/app/events/gm/GmTileUpload.ts @@ -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); } }); } \ No newline at end of file diff --git a/src/app/utilities/Http.ts b/src/app/utilities/Http.ts index 3ef41af..2a3a118 100644 --- a/src/app/utilities/Http.ts +++ b/src/app/utilities/Http.ts @@ -10,21 +10,16 @@ import config from "./Config"; import {loginAccountSchema, registerAccountSchema} from "./ZodTypes"; import path from "path"; -function isValidAsset(assetName: string) { - const assetPath = path.join(__dirname, 'public', 'assets', assetName); - return assetPath.startsWith(path.join(__dirname, 'public', 'assets')); -} +async function addHttpRoutes(app: Application) { + app.get('/assets/:type/:file', (req: Request, res: Response) => { + const assetName = req.params.file; -async function addAuthRoutes(app: Application) { - app.get('/assets/:asset', (req: Request, res: Response) => { - const assetName = req.params.asset; - - if (!isValidAsset(assetName)) { - return res.status(400).send('Invalid asset name'); - } + // if (!isValidAsset(assetName)) { + // return res.status(400).send('Invalid asset name'); + // } const options = { - root: path.join(__dirname, 'public', 'assets'), + root: path.join(process.cwd(), 'public', req.params.type), }; res.sendFile(assetName, options, (err) => { @@ -75,7 +70,7 @@ async function addAuthRoutes(app: Application) { return res.status(400).json({ message: 'Failed to register user' }); }); - console.log('[✅] Auth routes added'); + console.log('[✅] Web routes added'); } -export { addAuthRoutes }; \ No newline at end of file +export { addHttpRoutes }; \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 20672ee..caee1ea 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,7 +2,7 @@ import fs from "fs"; import path from "path"; import express, {Application} from 'express'; import {createServer as httpServer} from 'http'; -import {addAuthRoutes} from './app/utilities/Http'; +import {addHttpRoutes} from './app/utilities/Http'; import cors from 'cors'; import {Server as SocketServer} from 'socket.io'; import {TSocket} from "./app/utilities/Types"; @@ -54,7 +54,7 @@ export class Server } // Add http API routes - await addAuthRoutes(this.app); + await addHttpRoutes(this.app); // Load user manager await UserManager.boot();