From fc19f00b1bea051421c32445e4b44203811fbecb Mon Sep 17 00:00:00 2001 From: ADARSH <64017655+0x0806@users.noreply.github.com> Date: Sat, 5 Jul 2025 16:46:25 +0530 Subject: [PATCH] Update file-server.js --- file-server.js | 79 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/file-server.js b/file-server.js index 450154b..3a083e2 100644 --- a/file-server.js +++ b/file-server.js @@ -17,11 +17,15 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) { if (stat.isDirectory()) { getAllMarkdownFiles(fullPath, arrayOfFiles); } else if (file.endsWith('.md')) { + const stats = fs.statSync(fullPath); arrayOfFiles.push({ name: file, path: fullPath, relativePath: path.relative('.', fullPath), - folder: path.dirname(path.relative('.', fullPath)) + folder: path.dirname(path.relative('.', fullPath)), + size: stats.size, + type: 'md', + lastModified: stats.mtime }); } } catch (error) { @@ -35,16 +39,6 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) { return arrayOfFiles; } -// Function to read file content -function readFileContent(filePath) { - try { - return fs.readFileSync(filePath, 'utf8'); - } catch (error) { - console.error('Error reading file:', error); - return null; - } -} - // Get all markdown files const markdownFiles = getAllMarkdownFiles('./'); @@ -62,7 +56,7 @@ const server = http.createServer((req, res) => { if (pathname.startsWith('/api/file')) { const filePath = parsedUrl.query.path; if (!filePath) { - res.writeHead(400); + res.writeHead(400, { 'Content-Type': 'text/plain' }); res.end('Missing file path'); return; } @@ -72,7 +66,7 @@ const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end(content); } catch (error) { - res.writeHead(404); + res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('File not found'); } return; @@ -87,22 +81,63 @@ const server = http.createServer((req, res) => { // Handle static files let filePath = path.join(__dirname, pathname === '/' ? 'index.html' : pathname); - const extname = path.extname(filePath); + + // Security check to prevent directory traversal + if (!filePath.startsWith(__dirname)) { + res.writeHead(403, { 'Content-Type': 'text/plain' }); + res.end('Forbidden'); + return; + } + + const extname = path.extname(filePath).toLowerCase(); let contentType = 'text/html'; switch (extname) { - case '.css': contentType = 'text/css'; break; - case '.js': contentType = 'text/javascript'; break; - case '.json': contentType = 'application/json'; break; - case '.png': contentType = 'image/png'; break; - case '.jpg': contentType = 'image/jpg'; break; - case '.ico': contentType = 'image/x-icon'; break; + case '.css': + contentType = 'text/css'; + break; + case '.js': + contentType = 'text/javascript'; + break; + case '.json': + contentType = 'application/json'; + break; + case '.png': + contentType = 'image/png'; + break; + case '.jpg': + case '.jpeg': + contentType = 'image/jpeg'; + break; + case '.ico': + contentType = 'image/x-icon'; + break; + case '.svg': + contentType = 'image/svg+xml'; + break; + case '.woff': + contentType = 'font/woff'; + break; + case '.woff2': + contentType = 'font/woff2'; + break; + case '.ttf': + contentType = 'font/ttf'; + break; + case '.eot': + contentType = 'application/vnd.ms-fontobject'; + break; } fs.readFile(filePath, (err, content) => { if (err) { - res.writeHead(404); - res.end('Not found'); + if (err.code === 'ENOENT') { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('File not found'); + } else { + res.writeHead(500, { 'Content-Type': 'text/plain' }); + res.end('Server error'); + } } else { res.writeHead(200, { 'Content-Type': contentType }); res.end(content);