-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
216 lines (195 loc) · 6.03 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const express = require("express");
const fileUpload = require("express-fileupload");
const { exec } = require("child_process"); //to execute python files
const app = express();
app.use(fileUpload());
app.use(express.static('public'));
app.use('/images', express.static('images'));
var bigFileType = "";
function setFileType(fileType) {
bigFileType = fileType;
return bigFileType;
}
var returnMessage = "";
function setReturnMessage(message) {
returnMessage = message;
return message;
}
function cleanDir() {
console.log("Directory cleaned!");
exec(
`rm -r ./public/images/Objects/*`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
}
);
}
// Upload Endpoint
app.post("/upload", (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: "No file uploaded" });
}
exec(`mkdir .\\public\\images\\Decoded`);
exec(`mkdir .\\public\\images\\Encoded`);
exec(`mkdir .\\public\\images\\Objects`);
const file = req.files.file;
file.mv(`${__dirname}/public/images/Objects/${file.name}`, (err) => {
if (err) {
console.error(err);
return res.status(500).send(err);
}
res.json({ fileName: file.name, filePath: `http://localhost:5000/images/Objects/${file.name}` });
});
});
app.post("/encode", (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: "No file uploaded" });
}
const coverFile = req.files.file[0];
const payloadFile = req.files.file[1];
const value = req.body.value;
// console.log(coverFile);
// console.log(coverFile.name.split('.'));
if (coverFile.name.split('.')[1] === 'png' || coverFile.name.split('.')[1] === 'bmp') {
exec(
`python IMAGE_STEGA.py encode ./public/images/Objects/${coverFile.name} ./public/images/Objects/${payloadFile.name} ${value}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
setReturnMessage(stdout);
console.log(`stdout: ${stdout}`);
}
);
}
else if (coverFile.name.split('.')[1] === 'wav') {
exec(
`python AUDIO_STEGA.py encode ./public/images/Objects/${coverFile.name} ./public/images/Objects/${payloadFile.name} ${value}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
setReturnMessage(stdout);
console.log(`stdout: ${stdout}`);
}
);
}
else if (coverFile.name.split('.')[1] === 'xlsx' || coverFile.name.split('.')[1] === 'docx' || coverFile.name.split('.')[1] === 'txt' || coverFile.name.split('.')[1] === 'pdf') {
exec(
`python DOCUMENT_STEGA.py encode ./public/images/Objects/${coverFile.name} ./public/images/Objects/${payloadFile.name} ${value}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
setReturnMessage(stdout);
console.log(`stdout: ${stdout}`);
}
);
}
setTimeout(function(){
res.json({ consoleMessage:returnMessage,fileName: `encoded.${coverFile.name.split('.')[1]}`, filePath: `http://localhost:5000/images/Encoded/encoded.${coverFile.name.split('.')[1]}` });
// res.json({ fileName: 'encoded.png', filePath: `${__dirname}/public/images/Encoded/encoded.png` });
},5000 );
});
app.post("/decode", (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: "No file uploaded" });
}
var fileType = "";
const encodedFile = req.files.file;
if (
encodedFile.name.split(".")[1] === "png" ||
encodedFile.name.split(".")[1] === "bmp"
) {
exec(
`python IMAGE_STEGA.py decode ./public/images/Objects/${encodedFile.name}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log("FUCKING STDOUT" + stdout)
fileType = stdout.split(" ")[1];
setFileType(fileType);
setReturnMessage(stdout);
}
);
} else if (encodedFile.name.split(".")[1] === "wav") {
exec(
`python AUDIO_STEGA.py decode ./public/images/Objects/${encodedFile.name}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
fileType = stdout.split(" ")[1];
setFileType(fileType);
setReturnMessage(stdout);
}
);
} else if (
encodedFile.name.split(".")[1] === "xlsx" ||
encodedFile.name.split(".")[1] === "docx" ||
encodedFile.name.split(".")[1] === "pdf" ||
encodedFile.name.split(".")[1] === "txt"
) {
exec(
`python DOCUMENT_STEGA.py decode ./public/images/Objects/${encodedFile.name}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
fileType = stdout.split(" ")[1];
setFileType(fileType);
setReturnMessage(stdout);
}
);
}
setTimeout(function () {
res.json({
consoleMessage:returnMessage,
fileName: `decoded.${bigFileType}`,
filePath: `http://localhost:5000/images/Decoded/decoded.${bigFileType}`,
});
}, 10000);
});
app.get("/");
app.listen(5000, () => console.log("Server Started..."));