From c2fab4e99911f4b9040cde211df183213fe71b22 Mon Sep 17 00:00:00 2001
From: Dennis Postma <dennis@directonline.io>
Date: Fri, 12 Jul 2024 00:07:39 +0200
Subject: [PATCH] Improved asset loading

---
 src/app/utilities/Http.ts | 74 ++++++++++-----------------------------
 1 file changed, 18 insertions(+), 56 deletions(-)

diff --git a/src/app/utilities/Http.ts b/src/app/utilities/Http.ts
index 5c52b97..7b3fd45 100644
--- a/src/app/utilities/Http.ts
+++ b/src/app/utilities/Http.ts
@@ -10,18 +10,30 @@ import {loginAccountSchema, registerAccountSchema} from "./ZodTypes";
 import path from "path";
 import { TAsset } from './Types'
 import fs from 'fs'
+import tileRepository from '../repositories/TileRepository'
+import objectRepository from '../repositories/ObjectRepository'
 
 async function addHttpRoutes(app: Application) {
-    app.get('/assets', (req: Request, res: Response) => {
+    app.get('/assets', async (req: Request, res: Response) => {
         let assets: TAsset[] = [];
-        const tiles = listTiles();
+        const tiles = await tileRepository.getAll();
         tiles.forEach(tile => {
-            assets.push({key: tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
+            assets.push({
+                key: tile.name,
+                value: '/assets/tiles/' + tile.name + '.png',
+                group: 'tiles',
+                type: 'link'
+            });
         });
 
-        const objects = listObjects();
+        const objects = await objectRepository.getAll();
         objects.forEach(object => {
-            assets.push({key: object, value: '/objects/' + object, group: 'objects', type: 'link'});
+            assets.push({
+                key: object.name,
+                value: '/assets/objects/' + object.name + '.png',
+                group: 'objects',
+                type: 'link'
+            });
         });
 
         res.json(assets);
@@ -88,54 +100,4 @@ async function addHttpRoutes(app: Application) {
     console.log('[✅] Web routes added');
 }
 
-export { addHttpRoutes };
-
-function listTiles(): string[] {
-    // get root path
-    const folder = path.join(process.cwd(), 'public', 'tiles');
-
-    // if folder does not exist, create it
-    if (!fs.existsSync(folder)) {
-        fs.mkdirSync(folder);
-    }
-
-    // list the files in the folder
-    let tiles: string[] = [];
-
-    try {
-        const files = fs.readdirSync(folder);
-
-        files.forEach(file => {
-            tiles.push(file.replace('.png', ''));
-        });
-    } catch (err) {
-        console.log(err);
-    }
-
-    return tiles;
-}
-
-function listObjects(): string[] {
-    // get root path
-    const folder = path.join(process.cwd(), 'public', 'objects');
-
-    // if folder does not exist, create it
-    if (!fs.existsSync(folder)) {
-        fs.mkdirSync(folder);
-    }
-
-    // list the files in the folder
-    let objects: string[] = [];
-
-    try {
-        const files = fs.readdirSync(folder);
-
-        files.forEach(file => {
-            objects.push(file.replace('.png', ''));
-        });
-    } catch (err) {
-        console.log(err);
-    }
-
-    return objects;
-}
\ No newline at end of file
+export { addHttpRoutes };
\ No newline at end of file