-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
123 lines (104 loc) · 4.25 KB
/
index.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const https = require('https');
const fs = require('fs');
const decompress = require("decompress");
function downloadRepo(repo, author, branch) {
const file = fs.createWriteStream("files/" + author + "-" + repo + "-" + branch + ".zip");
// Making an empty folder so the repository won't be donwloaded more than one time
fs.mkdirSync("files/" + repo + "-" + branch + "-loading");
// Downloading the zip file
const request = https.get("https://codeload.github.com/" + author + "/" + repo + "/zip/" + branch, function(response) {
response.pipe(file);
file.on("finish", () => {
// When the download finishes
file.close();
console.log("Downloaded repository " + repo + " created by " + author + " at branch " + branch + ". ");
// Unpacking the zip file
decompress("files/" + author + "-" + repo + "-" + branch + ".zip", "files")
.then((files) => {
// Removing the empty folder and adding the new one
fs.unlink("files/" + author + "-" + repo + "-" + branch + ".zip", () => {});
fs.rmSync("files/" + repo + "-" + branch + "-loading", { recursive: true, force: true });
console.log("Decompressed repository " + repo + " created by " + author + " at branch " + branch + ". ");
// When the folder has no content, it means that the repository doesn't exist. In that case the script creates an error file in the folder
if (!fs.existsSync("files/" + repo + "-" + branch)) {
fs.rmSync("files/" + repo + "-" + branch + "-loading", { recursive: true, force: true });
fs.mkdirSync("files/" + repo + "-" + branch);
fs.writeFileSync("files/" + repo + "-" + branch + "/error.txt", "Error: Repository not found. ");
}
});
});
});
}
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
// Getting the index of the page
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// When running a repository
app.get('/run/*', (req, res) => {
var repo = req.originalUrl.split("/")[2];
var author = req.originalUrl.split("/")[3];
var branch = req.originalUrl.split("/")[4];
var fileName = "";
if (req.originalUrl.split("/").length >= 6) {
fileName = req.originalUrl.split("/").slice(5, req.originalUrl.split("/").length).join("/");
}
if (fileName === "") {
fileName = "index.html";
}
console.log("Requested loading file ''" + "files/" + repo + "-" + branch + "/" + fileName + "'");
if (fs.existsSync("files/" + repo + "-" + branch)) {
if (fs.existsSync("files/" + repo + "-" + branch + "/error.txt")) {
if (String(fs.readFileSync("files/" + repo + "-" + branch + "/error.txt")) === "Error: Repository not found. ") {
res.sendFile(__dirname + "/repo-not-found.html");
}
else {
if (fs.existsSync("files/" + repo + "-" + branch + "/" + fileName)) {
res.sendFile(__dirname + "/files/" + repo + "-" + branch + "/" + fileName);
}
else {
res.sendFile(__dirname + "/file-not-found.html");
}
}
}
else {
if (fs.existsSync("files/" + repo + "-" + branch + "/" + fileName)) {
res.sendFile(__dirname + "/files/" + repo + "-" + branch + "/" + fileName);
}
else {
res.sendFile(__dirname + "/file-not-found.html");
}
}
}
else {
var toSend = String(fs.readFileSync("loading.html"));
toSend = toSend.replaceAll("#repo#", repo);
toSend = toSend.replaceAll("#branch#", branch);
toSend = toSend.replaceAll("#author#", author);
res.send(toSend);
if (!fs.existsSync("files/" + repo + "-" + branch + "-loading")) {
downloadRepo(repo, author, branch);
}
}
});
// Sending style
app.get("/style.css", (req, res) => {
res.sendFile(__dirname + "/style.css");
});
// Sending background image
app.get("/background.jpg", (req, res) => {
res.sendFile(__dirname + "/background.jpg");
});
// Wrong url
app.get("*", (req, res) => {
res.sendFile(__dirname + "/site-not-found.html");
});
// Purging cache every day
setInterval(function() {
fs.rmSync("files", { recursive: true, force: true });
fs.mkdirSync("files");
}, 24 * 60 * 60 * 1000);
server.listen(3000, () => {});