-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
153 lines (137 loc) · 4.07 KB
/
Copy pathroutes.js
File metadata and controls
153 lines (137 loc) · 4.07 KB
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
const crypto = require("crypto");
const express = require("express");
const fs = require("fs-extra");
const multer = require("multer");
const homeGenerator = require("./home_generator");
const editJsonFile = require("edit-json-file");
const data = require("./data.json");
const { zip } = require("zip-a-folder");
const mv = require("mv");
const TIMEOUT = 1000 * 60 * 15; // 15 minutes
var upload = multer({ dest: "/tmp/" });
const router = express.Router();
router.get("/", (request, response) => response.send("Hello World!"));
router.post("/getstarted", upload.single("profilePhoto"), (req, res) => {
// Creates images directory if not existed.
if (!fs.existsSync("images")) {
fs.mkdirSync("images");
}
// Creating hash using filename & timestamp.
var hash = crypto
.createHash("md5")
.update(req.file.originalname + Date.now().toString())
.digest("hex");
var file = __dirname + "/images/" + hash;
// Save the image by renaming & respond with the hash.
mv(req.file.path, file, function (err) {
if (err) {
console.log(err);
res.status(500).send("Unable to upload the file.");
} else {
res.json({
message: "File uploaded successfully",
hash: hash,
});
}
});
});
router.get("/data", (req, response) => {
if (req.query.code === "theadmin") {
response.send({
total: data["count"],
data: data["users"],
});
} else {
response.status(404).send("Not Found");
}
});
router.post("/downloaderesume", express.json(), (req, res) => {
const {
name,
bio,
education,
skills,
experience,
achievements,
contact,
token,
secureCode,
} = req.body;
if (secureCode !== "TheLastProject") {
res.json({
status: 401,
message: "User is not authorized.",
});
return;
}
if (!fs.existsSync(`images/${token}`)) {
res.json({
status: 400,
message: "Invalid token. User not found.",
});
return;
}
fs.copy(__dirname + "/template", __dirname + "/" + token, function (err) {
if (err) {
console.error(err);
} else {
console.log("success!");
try {
// Move the image from images directory to eresume directory.
mv(
`images/${token}`,
__dirname + "/" + token + "/images/logo.jpg",
function (err) {
if (err) {
console.error(err);
return res.status(500).send("Unable to set img");
}
try {
var html = homeGenerator(
name,
bio,
education,
skills,
experience,
achievements,
contact
);
let file = editJsonFile(`${__dirname}/data.json`);
fs.writeFileSync(__dirname + "/" + token + "/index.html", html);
file.set("count", file.get("count") + 1);
file.append("users", { name, education, contact });
zip(__dirname + "/" + token, __dirname + "/zip/" + token + ".zip")
.then(() => {
setTimeout(() => {
fs.rmdirSync(__dirname + "/" + token, { recursive: true });
fs.rmSync(__dirname + "/zip/" + token + ".zip");
}, TIMEOUT);
res.json({
status: 200,
message: "success!",
url:
"http://" +
req.headers["host"] +
"/zip/" +
token +
".zip",
});
})
.catch((e) => {
console.error(e);
return res.status(500).send("Unable to compress files.");
});
} catch (e) {
console.error(e);
return res.status(500).send("Unable to create html file.");
}
}
);
} catch (e) {
console.error(e);
return res.status(500).send("unable to rename image.");
}
}
});
});
module.exports = router;