-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
171 lines (150 loc) · 4.16 KB
/
start.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
const express = require("express"),
path = require("path"),
fs = require("fs"),
crypto = require("crypto"),
imageThumbnail = require("image-thumbnail");
/* Loading pages */
const layout_head = require("./views/includes/layout_head"),
layout_footer = require("./views/includes/layout_footer");
const index = require("./views/index"),
screenshots = require("./views/screenshots");
const app = express();
app.set("views", "views");
app.use(express.static("public"));
const galleryImagesPath = process.env.GALLERYPATH || "public/testimages";
const galleryThumbnailPath = process.env.THUMBNAILPATH || "public/thumbnails";
app.use("/images/screenshots", express.static(galleryImagesPath));
app.get("/", function(req, res) {
res.send(
index({
layout_head: layout_head({
title: "Home"
}),
layout_footer: layout_footer()
})
);
});
function walkSync(dir, filelist = []) {
fs.readdirSync(dir).forEach(file => {
const dirFile = path.join(dir, file);
try {
filelist = walkSync(dirFile, filelist);
} catch (err) {
if (err.code === "ENOTDIR" || err.code === "EBUSY")
filelist = [...filelist, dirFile];
else throw err;
}
});
return filelist;
}
app.get("/upload", function(req, res) {
res.redirect("https://fuelrats.cloud/s/eXqRPLAGfdZCMr4");
});
app.get("/tools", function(req, res) {
res.send("Not yet, but soon™.");
});
app.get("/screenshots/:page?", function(req, res) {
let currentPage = req.params.page ? req.params.page : 1;
const fileList = walkSync(galleryImagesPath);
res.send(
screenshots({
layout_head: layout_head({
title: "Screenshot gallery",
head: /*html*/ `
<link href="/lightbox2/css/lightbox.min.css" rel="stylesheet" />`
}),
currentPage,
fileList,
galleryImagesPath,
layout_footer: layout_footer()
})
);
});
app.get("/images/screenshots/thumbnails/:originalFileName", function(req, res) {
const origFileName = req.params.originalFileName;
let hash = crypto
.createHash("md5")
.update(origFileName)
.digest("hex");
const thumbFile = path.join(
galleryThumbnailPath,
hash + path.extname(origFileName)
);
if (fs.existsSync(thumbFile)) {
res.send(fs.readFileSync(thumbFile));
} else {
const origPath = path.join(galleryImagesPath, origFileName);
imageThumbnail(origPath, {
height: 138,
width: 400
}).then(thumb => {
fs.writeFileSync(thumbFile, thumb);
res.send(thumb);
});
}
});
app.get("/videos/background.webm", function(req, res) {
const path = "./public/videos/wollheim.webm";
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = end - start + 1;
const file = fs.createReadStream(path, {
start,
end
});
const head = {
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/webm"
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
"Content-Length": fileSize,
"Content-Type": "video/webm"
};
res.writeHead(200, head);
fs.createReadStream(path).pipe(res);
}
});
app.get("/videos/background.mp4", function(req, res) {
const path = "./public/videos/wollheim_background_video_no_sound.mp4";
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = end - start + 1;
const file = fs.createReadStream(path, {
start,
end
});
const head = {
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/mp4"
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
"Content-Length": fileSize,
"Content-Type": "video/mp4"
};
res.writeHead(200, head);
fs.createReadStream(path).pipe(res);
}
});
app.set("trust proxy", true);
app.listen(process.env.PORT || 3003);