generated from GDGVIT/template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
170 lines (139 loc) · 3.83 KB
/
app.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
const express = require("express");
const app = express();
const bp = require("body-parser");
const cors = require("cors");
const dotenv = require("dotenv");
dotenv.config();
const jwt = require("jsonwebtoken");
const mongoose = require("mongoose");
const morgan = require("morgan");
const socket = require("socket.io");
// const ss = require("socket.io-stream");
const stream = require("stream");
const fs = require("fs");
const btoa = require("btoa");
const bash = require("bash");
// const WebSocket = require("ws");
const { exec, execFile, spawn } = require("child_process");
const { check, validationResult } = require("express-validator");
// var data = "";
const loginRoute = require("./Login/routes/login");
const oAuthRoute = require("./Login/routes/oAuth");
const previewRoute = require("./Login/routes/preview");
const authTestRoute = require('./Login/routes/authTest');
const { User } = require("./Login/models/model");
const port = process.env.BACKEND_PORT || 3000;
// middle-wares
app.use(morgan("dev"));
app.use(express.static("public"));
app.use(
bp.urlencoded({
extended: false,
})
);
app.use(bp.json());
app.use(cors());
app.get("/ping", (_req, res) => {
res.json({ Health: "Ok" });
});
const axios = require('axios');
const requestIp = require('request-ip');
app.use(requestIp.mw())
// mongo db
mongoose.set("useCreateIndex", true);
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
// listen port
const server = app.listen(port, function () {
console.log(`Server started at ${port}`);
});
// routes
app.use("/api/auth", oAuthRoute);
app.use("/api/manauth", loginRoute);
app.use("/api/preview", previewRoute);
app.use("/",authTestRoute);
let child;
var io = socket(server, {path: '/api/socket.io'});
io.origins("*:*");
io.on("connection", (person) => {
console.log(`made socket connection : ${person.id}`);
person.on("cmd", async function (val){
let val_json = JSON.parse(val);
let token = val_json.token;
let fileName = val_json.fileId;
let data = val_json.data;
// await trigger(val_json,'socket')
console.log(val_json);
let email = '';
try {
jwt.verify(token, process.env.JWT_KEY, (err, user) => {
if (err) {
console.log(err)
}
console.log(user);
email = user.email;
console.log(email);
});
} catch (err) {
console.log(err);
}
User.find({
email: email
}).then((doc) => {
if(doc.length>=1){
let timestamps = {
updatedAt: new Date(),
};
console.log(fileName);
User.updateOne(
{
_id: doc._id,
"files.fileId": fileName,
},
{
$set: {
"files.$.fileData": data,
"files.$.timestamps": timestamps,
},
}
)
.exec()
.then((result) => {
console.log("File updated");
})
.catch((err) => {
console.log(err);
});
let command = 'printf "' + data + '"';
child = execFile(
"pdfroff",
["-i", "-ms", `--pdf-output=media/${doc._id}.pdf`],
(err) => {
if (err) {
console.log(err);
} else {
fs.readFile(`media/${doc._id}.pdf`, "binary", (err, data) => {
if (err) {
return console.log("Error:" + err);
}
let buff = btoa(data);
person.emit("cmd", buff);
});
}
}
);
var stdinStream = new stream.Readable();
stdinStream.push(data);
stdinStream.push(null);
stdinStream.pipe(child.stdin);
} else {
console.log("");
}
}).catch(err => {
console.log(err)
});
})
});