Update file-server.js
This commit is contained in:
parent
16ba6b2231
commit
fc19f00b1b
1 changed files with 57 additions and 22 deletions
|
|
@ -17,11 +17,15 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) {
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
getAllMarkdownFiles(fullPath, arrayOfFiles);
|
getAllMarkdownFiles(fullPath, arrayOfFiles);
|
||||||
} else if (file.endsWith('.md')) {
|
} else if (file.endsWith('.md')) {
|
||||||
|
const stats = fs.statSync(fullPath);
|
||||||
arrayOfFiles.push({
|
arrayOfFiles.push({
|
||||||
name: file,
|
name: file,
|
||||||
path: fullPath,
|
path: fullPath,
|
||||||
relativePath: path.relative('.', 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) {
|
} catch (error) {
|
||||||
|
|
@ -35,16 +39,6 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) {
|
||||||
return 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
|
// Get all markdown files
|
||||||
const markdownFiles = getAllMarkdownFiles('./');
|
const markdownFiles = getAllMarkdownFiles('./');
|
||||||
|
|
||||||
|
|
@ -62,7 +56,7 @@ const server = http.createServer((req, res) => {
|
||||||
if (pathname.startsWith('/api/file')) {
|
if (pathname.startsWith('/api/file')) {
|
||||||
const filePath = parsedUrl.query.path;
|
const filePath = parsedUrl.query.path;
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
res.writeHead(400);
|
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||||
res.end('Missing file path');
|
res.end('Missing file path');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +66,7 @@ const server = http.createServer((req, res) => {
|
||||||
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
|
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||||
res.end(content);
|
res.end(content);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.writeHead(404);
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||||
res.end('File not found');
|
res.end('File not found');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
@ -87,22 +81,63 @@ const server = http.createServer((req, res) => {
|
||||||
|
|
||||||
// Handle static files
|
// Handle static files
|
||||||
let filePath = path.join(__dirname, pathname === '/' ? 'index.html' : pathname);
|
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';
|
let contentType = 'text/html';
|
||||||
|
|
||||||
switch (extname) {
|
switch (extname) {
|
||||||
case '.css': contentType = 'text/css'; break;
|
case '.css':
|
||||||
case '.js': contentType = 'text/javascript'; break;
|
contentType = 'text/css';
|
||||||
case '.json': contentType = 'application/json'; break;
|
break;
|
||||||
case '.png': contentType = 'image/png'; break;
|
case '.js':
|
||||||
case '.jpg': contentType = 'image/jpg'; break;
|
contentType = 'text/javascript';
|
||||||
case '.ico': contentType = 'image/x-icon'; break;
|
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) => {
|
fs.readFile(filePath, (err, content) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.writeHead(404);
|
if (err.code === 'ENOENT') {
|
||||||
res.end('Not found');
|
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 {
|
} else {
|
||||||
res.writeHead(200, { 'Content-Type': contentType });
|
res.writeHead(200, { 'Content-Type': contentType });
|
||||||
res.end(content);
|
res.end(content);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue