-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (83 loc) · 3.1 KB
/
index.js
File metadata and controls
97 lines (83 loc) · 3.1 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
fs = require("fs");
path = require("path");
express = require("express");
const mime = require('mime-types');
function setCorsHeaders(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'https://worker-server.pages.dev*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
}
function walk(dir) {
let fsjson = [];
// get the contents of dir
let items = fs.readdirSync(dir);
// for each item in the contents
items.forEach((item) => {
// get the item path
let itemPath = path.join(dir, item);
// get the stats of the item
stats = fs.statSync(itemPath);
// Just log the item path for now
// console.log(itemPath);
// for now just use stats to find out
// if the current item is a dir
if (stats.isDirectory()) {
// if so walk that too, by calling this
// method recursively
fsjson.push({"name":item,"contents":walk(itemPath)});
// console.log(fsjson)
}else{
fsjson.push({"name":item,"path":itemPath});
// console.log(fsjson)
}
});
return(fsjson);
};
console.log(JSON.stringify(walk("./files/")));
function readFile(filePath) {
try {
const data = fs.readFileSync(filePath);
return data.toString();
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
const app = express()
const bodyParser = require('body-parser');
const { dirname } = require("path");
app.use(setCorsHeaders);
app.use(bodyParser.urlencoded({ extended: true }));
const port = 4000
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.send('<script>window.location.href = window.location.href + "public/index.html";</script>')
});
app.get('/files/*', function(req, res){
var filepath = req.path.replace("/files/","");
const file = `${__dirname}/files/${filepath}`;
let mimeType = mime.lookup(file);
if(mimeType == "text/html"){
mimeType = "text/plain";
}
res.setHeader("content-type",mimeType);
res.send(readFile(file)); // Set disposition and send it.
// res.send(req.path);
});
app.get('/download/*', function(req, res){
var filepath = req.path.replace("/download","");
const file = `${__dirname}${filepath}`;
res.download(file); // Set disposition and send it.
// res.send(req.path);
});
app.get("/files", (req,res) =>{
fs.writeFileSync("./public/files.json",JSON.stringify(walk("./files/")));
// console.log(`${req.socket.remoateAddress} accessed the files page`)
res.send('<script>window.location.href = window.location.href.replace(window.location.pathname,"").replace(window.location.search,"") + "/files.html";</script>')
});
app.all('*', (req, res) => {
res.status(404).send('<h1>This page doesn'+"'"+'t exist, dumbass!</h1>');
});
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})