forked from noxious/server
╭∩╮( •̀_•́ )╭∩╮
This commit is contained in:
parent
ad9b7f8892
commit
39e8f52595
@ -1,44 +0,0 @@
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket, TAsset} from "../utilities/Types";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default function (socket: TSocket, io: Server) {
|
||||
socket.on('assets:download', async (data: any, callback: (response: TAsset[]) => void) => {
|
||||
console.log('assets:download requested');
|
||||
|
||||
let assets: TAsset[] = [];
|
||||
const tiles = listTiles();
|
||||
|
||||
tiles.forEach(tile => {
|
||||
assets.push({key: 'tile_' + tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
|
||||
});
|
||||
|
||||
console.log('assets:download response', assets)
|
||||
|
||||
// return the list of assets to the socket
|
||||
callback(assets);
|
||||
});
|
||||
}
|
||||
|
||||
function listTiles(): string[] {
|
||||
// get root path
|
||||
const folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
|
||||
// list the files in the folder
|
||||
let tiles: string[] = [];
|
||||
|
||||
try {
|
||||
const files = fs.readdirSync(folder);
|
||||
|
||||
files.forEach(file => {
|
||||
tiles.push(file);
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
console.log(tiles);
|
||||
|
||||
return tiles;
|
||||
}
|
@ -32,7 +32,7 @@ export default function (socket: TSocket, io: Server) {
|
||||
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);
|
||||
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);
|
||||
|
@ -1,7 +1,6 @@
|
||||
/**
|
||||
* Resources:
|
||||
* https://stackoverflow.com/questions/76131891/what-is-the-best-method-for-socket-io-authentication
|
||||
*
|
||||
*/
|
||||
import {Application, Request, Response} from 'express';
|
||||
import UserService from '../services/UserService';
|
||||
@ -9,8 +8,18 @@ import jwt from "jsonwebtoken";
|
||||
import config from "./Config";
|
||||
import {loginAccountSchema, registerAccountSchema} from "./ZodTypes";
|
||||
import path from "path";
|
||||
import { TAsset } from './Types'
|
||||
import fs from 'fs'
|
||||
|
||||
async function addHttpRoutes(app: Application) {
|
||||
app.get('/assets', (req: Request, res: Response) => {
|
||||
let assets: TAsset[] = [];
|
||||
const tiles = listTiles();
|
||||
tiles.forEach(tile => {
|
||||
assets.push({key: 'tile_' + tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
|
||||
});
|
||||
res.json(assets);
|
||||
});
|
||||
app.get('/assets/:type/:file', (req: Request, res: Response) => {
|
||||
const assetName = req.params.file;
|
||||
|
||||
@ -74,3 +83,25 @@ async function addHttpRoutes(app: Application) {
|
||||
}
|
||||
|
||||
export { addHttpRoutes };
|
||||
|
||||
function listTiles(): string[] {
|
||||
// get root path
|
||||
const folder = path.join(process.cwd(), 'public', 'tiles');
|
||||
|
||||
// list the files in the folder
|
||||
let tiles: string[] = [];
|
||||
|
||||
try {
|
||||
const files = fs.readdirSync(folder);
|
||||
|
||||
files.forEach(file => {
|
||||
tiles.push(file);
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
console.log(tiles);
|
||||
|
||||
return tiles;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user