-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
183 lines (157 loc) · 4.88 KB
/
server.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
172
173
174
175
176
177
178
179
180
181
182
183
module.exports.start = function() {
const debugMode = false;
const showUnsupported = false;
// Socket.IO
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
// directory
const fileReader = require("fs");
let foldersToCheck = ["./Media/"];
let directory = [];
const validFileTypes = [
// videos
"mp4", "webm", "ogg",
// photos
"png", "jpg"
];
let alreadySetupDirectory = false;
function parseFolder() {
let folder = foldersToCheck[0];
foldersToCheck.shift();
fileReader.readdirSync(folder).forEach(file => {
let fullFilePath = folder + file;
if(file != ".gitignore") {
// first check to see if the current file is a folder
let fileIsFolder = fileReader.lstatSync(fullFilePath).isDirectory();
if(fileIsFolder) {
let parsePath = fullFilePath + "/";
foldersToCheck.push(parsePath);
}
else {
if(!(file.includes("."))) {
if(showUnsupported) {
console.log("\nUNSUPORTED FILE FOUND - NO FILE TYPE (will be excluded from server directory)");
console.log("....NAME: " + file);
console.log("....IN FOLDER: " + folder);
console.log("....FULL DIRECTORY: " + fullFilePath);
console.log("....SUPPORTED FILE TYPES: " + validFileTypes);
console.log("--------------------");
}
}
else {
let splitted = file.split(".");
let fileType = splitted[splitted.length - 1];
if(!(validFileTypes.includes(fileType))) {
if(showUnsupported) {
console.log("\nUNSUPORTED FILE FOUND (will be excluded from server directory)");
console.log("....NAME: " + file);
console.log("....IN FOLDER: " + folder);
console.log("....FULL DIRECTORY: " + fullFilePath);
console.log("....SUPPORTED FILE TYPES: " + validFileTypes);
console.log("--------------------");
}
}
else {
directory.push(fullFilePath);
}
}
}
}
});
// if more folders need to be checked, do the function again
if(foldersToCheck.length > 0) {
parseFolder();
}
else {
if(debugMode) {
console.log("\nDirectory set up!");
}
if(alreadySetupDirectory) {
io.emit("sendDirectory", directory);
}
}
}
function setupDirectory() {
if(debugMode) {
console.log("\nSetting up directory...");
}
foldersToCheck = ["./Media/"];
directory = [];
parseFolder();
}
setupDirectory();
// use static files
app.use(express.static(__dirname + "/Client"));
app.use(express.static(__dirname + "/Assets"));
app.use('/Media', express.static(__dirname + '/Media'));
// app.use("/Client", express.static(__dirname + "/Client"));
// send index.html to client
app.get("/", (req, res) => {
res.sendFile(__dirname + "/Client/index.html");
});
/**
* User Socket.IO to handle communication between client
* and server, which is needed for the client to get the
* media directory.
*/
io.on("connection", (socket) => {
// when client connects to server send directory
io.emit("sendDirectory", directory);
alreadySetupDirectory = true;
});
// start server
const port = 1234;
// start server
server.listen(port, () => {
console.log("\nServer started on port " + port);
});
// watch for changes in Media directory
let mediaModified = false;
hound = require("hound");
watcher = hound.watch("Media");
watcher.on("create", function(file, stats) {
try{
mediaModified = true;
}
catch(e) {
let unused = "unused";
}
});
watcher.on("change", function(file, stats) {
try{
mediaModified = true;
}
catch(e) {
let unused = "unused";
}
});
watcher.on("delete", function(file) {
try{
mediaModified = true;
}
catch(e) {
let unused = "unused";
}
});
// incase a bunch of changes made, don't spam user
setInterval(function() {
if(mediaModified) {
if(debugMode) {
console.log("resending directory");
}
mediaModified = false;
setupDirectory();
}
}, (5 * 1000));
// handle thrown error crashing server when empty folder deleted in Media
process.on("uncaughtException", function(e) {
if(debugMode) {
console.log("uncaught exception:");
console.log(e);
}
});
}