-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·160 lines (143 loc) · 4.42 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
const http = require("http");
const fs = require("fs");
const sudo = require("sudo-js");
const formidable = require("formidable");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const password = "pwd";
var NumberOfFiles = 0;
var FilesReceived = 0;
sudo.setPassword(password);
// Reading the file that has to be displayed
fs.readFile("./index.html", function(err, data) {
if (err) {
throw err;
}
htmlFile = data;
});
fs.readFile("./favicon.ico", function(err, data) {
if (err) {
throw err;
}
favicon = data;
});
fs.readFile("./styles.css", function(err, data) {
if (err) {
throw err;
}
cssFile = data;
});
fs.readFile("./upload.js", function(err, data) {
if (err) {
throw err;
}
javascriptFile = data;
});
// Server creation
var server = http.createServer(function(req, res) {
// GET methode -> User wants something (html, css, etc..)
if (req.method === "GET") {
// Serves different pages depending on what whants the client
switch (req.url) {
case "/upload.js":
res.writeHead(200, { "Content-Type": "application/js" });
res.write(javascriptFile);
res.end();
break;
case "/styles.css":
res.writeHead(200, { "Content-Type": "text/css" });
res.write(cssFile);
res.end();
break;
case "/favicon.ico":
res.writeHead(200, { "Content-Type": "image/x-icon" });
res.write(favicon);
res.end();
break;
case "/":
res.writeHead(200, { "Content-Type": "text/html" });
res.write(htmlFile);
res.end();
break;
default:
break;
}
// POST METHOD when user want to send something
} else if (req.method === "POST") {
if (req.url === "/replugStorage") {
console.log("Unmounting USB to upload new files..");
UnmoutUSB();
console.log("Mounting USB back..");
MountUSB();
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ message: "success" }));
}
// CHECK IF USER WANTS TO UPLOAD
else if (req.url === "/upload") {
if (req.headers["numberoffiles"]) {
NumberOfFiles = req.headers["numberoffiles"];
console.log("Unmounting USB to upload new files..");
UnmoutUSB();
} else {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if (err) throw err;
try {
console.log("Uploading " + files["files[]"].name);
fs.readFile(files["files[]"].path, function(err, data) {
if (err) throw err;
var fileName = files["files[]"].name;
var directory = "/mnt/usb_share/";
//var directory = '';
fs.writeFile(directory + fileName, data, function(err) {
if (err) throw err;
console.log("File " + fileName + " was successfully saved.");
console.log("Compressing image..");
compressImage(directory + fileName)
});
});
FilesReceived++;
if (FilesReceived == NumberOfFiles) {
console.log("Mounting USB back..");
MountUSB();
FilesReceived = 0;
NumberOfFiles = 0;
}
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ message: "success" }));
} catch (error) {
console.log(error);
}
});
}
}
}
});
server.listen(8081);
console.log("Server running at http://localhost:8081/");
function MountUSB() {
// Bash command to remount the USB
var command = [
"modprobe",
"g_mass_storage",
"file=/pisusb.bin",
"stall=0",
"ro=1"
];
sudo.exec(command, function(err, pid, result) {
console.log(result);
});
}
function UnmoutUSB() {
// Bash command to unmount the USB
var command = ["modprobe", "-r", "g_mass_storage"];
sudo.exec(command, function(err, pid, result) {
console.log(result);
});
}
async function compressImage(filePath){
var command = "convert " + filePath + " -quality 30% " + filePath;
const { stdout, stderr } = await exec(command);
console.log('stdout:', stdout);
console.log('stderr:', stderr);
}