Code improvements

This commit is contained in:
Dennis Postma 2024-06-23 01:53:06 +02:00
parent f5191aa81f
commit 08b6c5c598
4 changed files with 26 additions and 25 deletions

View File

@ -1,6 +1,7 @@
import { Server } from "socket.io"; import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types"; import {TSocket} from "../../utilities/Types";
import fs from 'fs'; import fs from 'fs';
import path from "path";
interface IPayload { 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) => { socket.on('gm:tile:list', async (data: any, callback: (response: string[]) => void) => {
// get root path // get root path
const root_folder = process.cwd(); const folder = path.join(process.cwd(), 'public', 'tiles');
const folder = `${root_folder}/public/tiles`;
// list the files in the folder // list the files in the folder
let tiles: string[] = []; let tiles: string[] = [];

View File

@ -2,6 +2,8 @@ import { Server } from "socket.io";
import {TSocket} from "../../utilities/Types"; import {TSocket} from "../../utilities/Types";
import {writeFile} from "node:fs"; import {writeFile} from "node:fs";
import {randomUUID} from "node:crypto"; import {randomUUID} from "node:crypto";
import path from "path";
import fs from "fs";
interface IPayload { interface IPayload {
@ -13,21 +15,25 @@ interface IPayload {
* @param io * @param io
*/ */
export default function (socket: TSocket, io: Server) { 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 // get root path
const root_folder = process.cwd(); const public_folder = path.join(process.cwd(), 'public', 'tiles');
const public_folder = `${root_folder}/public`;
// check if folder exists or create it
if (!fs.existsSync(public_folder)) {
fs.mkdirSync(public_folder, { recursive: true });
}
for (const key in data) { for (const key in data) {
const filename = randomUUID(); const filename = randomUUID();
const path = `${public_folder}/tiles/${filename}.png`; const finalFilePath = path.join(public_folder, filename);
const tile = data[key]; const tile = data[key];
// save the tile to the disk, for example // save the tile to the disk, for example
writeFile(path, tile, (err) => { writeFile(finalFilePath, tile, (err) => {});
// pajeet INC
}); callback(true);
} }
}); });
} }

View File

@ -10,21 +10,16 @@ import config from "./Config";
import {loginAccountSchema, registerAccountSchema} from "./ZodTypes"; import {loginAccountSchema, registerAccountSchema} from "./ZodTypes";
import path from "path"; import path from "path";
function isValidAsset(assetName: string) { async function addHttpRoutes(app: Application) {
const assetPath = path.join(__dirname, 'public', 'assets', assetName); app.get('/assets/:type/:file', (req: Request, res: Response) => {
return assetPath.startsWith(path.join(__dirname, 'public', 'assets')); const assetName = req.params.file;
}
async function addAuthRoutes(app: Application) { // if (!isValidAsset(assetName)) {
app.get('/assets/:asset', (req: Request, res: Response) => { // return res.status(400).send('Invalid asset name');
const assetName = req.params.asset; // }
if (!isValidAsset(assetName)) {
return res.status(400).send('Invalid asset name');
}
const options = { const options = {
root: path.join(__dirname, 'public', 'assets'), root: path.join(process.cwd(), 'public', req.params.type),
}; };
res.sendFile(assetName, options, (err) => { res.sendFile(assetName, options, (err) => {
@ -75,7 +70,7 @@ async function addAuthRoutes(app: Application) {
return res.status(400).json({ message: 'Failed to register user' }); return res.status(400).json({ message: 'Failed to register user' });
}); });
console.log('[✅] Auth routes added'); console.log('[✅] Web routes added');
} }
export { addAuthRoutes }; export { addHttpRoutes };

View File

@ -2,7 +2,7 @@ import fs from "fs";
import path from "path"; import path from "path";
import express, {Application} from 'express'; import express, {Application} from 'express';
import {createServer as httpServer} from 'http'; import {createServer as httpServer} from 'http';
import {addAuthRoutes} from './app/utilities/Http'; import {addHttpRoutes} from './app/utilities/Http';
import cors from 'cors'; import cors from 'cors';
import {Server as SocketServer} from 'socket.io'; import {Server as SocketServer} from 'socket.io';
import {TSocket} from "./app/utilities/Types"; import {TSocket} from "./app/utilities/Types";
@ -54,7 +54,7 @@ export class Server
} }
// Add http API routes // Add http API routes
await addAuthRoutes(this.app); await addHttpRoutes(this.app);
// Load user manager // Load user manager
await UserManager.boot(); await UserManager.boot();