import { existsSync, readFileSync } from 'node:fs'; interface Mapping { ending: string; encoding: string; } export class File { type: string; content: Buffer; constructor(type: string, content: Buffer) { this.content = content; this.type = type; } } export class FileHandler { private www_folder: string = '/home/carsten/Dokumente/SmartHomeDashboard/html/'; private index_alias: string = 'index.html'; private mappings: Mapping[] = [ { ending: 'html', encoding: 'text/html' }, { ending: 'js', encoding: 'text/javascript' }, { ending: 'css', encoding: 'text/css' }, { ending: 'json', encoding: 'application/json' }, { ending: 'ico', encoding: 'image/x-icon' }, { ending: 'svg', encoding: 'image/svg+xml' }, { ending: 'txt', encoding: 'text/plain' }, ] file_allowed(url: string): boolean { let allowed: boolean = false; let m = url.match('\\.\\.'); if (m == null) { allowed = true; } return allowed; } file_exists(url: string): boolean { if(url == '/') { url = this.index_alias; } return existsSync(this.www_folder + url); } file_get(url: string): File { if(url == '/') { url = this.index_alias; } return new File(this.get_type(url), readFileSync(this.www_folder + url)); } private get_type(url: string): string { if(url == '/') { url = this.index_alias; } let type: string = 'application/octet-stream'; let m: Mapping = this.mappings.find((e) => e.ending == url.split('.').pop()); if(m != undefined) { type = m.encoding; } return type; } }