forked from noxious/server
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
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;
|
|
} |