-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_html_dir.js
More file actions
82 lines (68 loc) · 1.65 KB
/
render_html_dir.js
File metadata and controls
82 lines (68 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require('fs');
const path = require('path');
async function renderHtmlDir(req, res, rootPath, reqPath, fsPath) {
let filenames;
try {
filenames = await fs.promises.readdir(fsPath);
}
catch (e) {
res.statusCode = 500;
res.write("Error reading directory");
res.end();
return;
}
res.setHeader('Content-Type', 'text/html');
let pathParts = reqPath.split('/');
let listing;
if (pathParts.length > 1) {
pathParts.pop();
const parentUrl = rootPath + pathParts.join('/');
listing = `<a href=../>..[parent]</a> 📁`;
}
else {
listing = '';
}
for (const filename of filenames) {
//const url = rootPath + reqPath + '/' + filename;
const url = './' + filename;
const childFsPath = path.join(fsPath, filename);
let childStats;
try {
childStats = await fs.promises.stat(childFsPath);
}
catch (e) {
console.error("This one shouldn't happen");
console.error(e);
continue;
}
let link;
if (childStats.isDirectory()) {
link = `<a href=${url}/>${filename}</a> 📁`;
}
else {
link = `<a target='_blank' href=${url}>${filename}</a>`;
}
listing += `
<div>
${link}
</div>
`;
}
const html = `
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>remFS</title>
</head>
<body>
<div style='margin: 0 auto; max-width: 640px; font-size: 24px;'>
${listing}
</div>
</body>
</html>
`;
res.write(html);
res.end();
}
module.exports = { renderHtmlDir };