-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
84 lines (72 loc) · 2.87 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
const express = require("express");
const app = express();
const path = require("path");
const public = path.join(__dirname, "public");
const favicon = require("serve-favicon");
const bodyParser = require("body-parser");
const fs = require("fs-extra");
const serveIndex = require("serve-index");
const multer = require("multer");
// Handles getting the output files ready for the client
const books = require("./libs/books");
app.use(bodyParser.json());
app.set("view-engine", "ejs");
app.use(express.static(public));
app.use("/uploads/", serveIndex(__dirname + "/public/uploads/"));
app.use(favicon("./public/images/favicon.ico"));
app.post('/upload/:folder', async function (req, res) {
// Designate storage location dynamically by the folder parameter set in upload.js by looking for the book html file.
var storage = multer.diskStorage({
destination: function (req, file, cb) {
// Get filepath from preserved Path
var filepath = file['originalname'].split('/').slice(0, -1).join('/');
var dir = `./public/uploads/${filepath}/`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, dir);
},
filename: function (req, file, cb) {
// Get the original name (removing the preserved path in front)
var originalname = file.originalname.split('/').slice(-1).toString();
cb(null, originalname);
}
});
// Assign multer with input file fields to upload
var upload = multer({ storage: storage, preservePath: true }).fields([{ name: 'uploads' }]);
script_done = upload(req, res, async function (err) {
if (err) {
return res.end("Error uploading file.");
}
// If upload finishes then the client will emit a socket io message.
// Nothing more needs to be done here.
});
});
app.post('/delete', function (req, res) {
// Designate storage location dynamically by the folder parameter set in upload.js by looking for the book html file.
var files = req.body;
for (var file in files) {
try {
var filepath = path.join(__dirname, "public", "output", files[file]);
if (fs.existsSync(filepath)) {
// Delete File if it Exists
fs.remove(filepath);
}
} catch(err) {
console.error(err)
}
}
});
app.get("/", async function (req, res) {
res.render("index.ejs", { files: await books.getBooks("zip", "batch"), active: "smil" });
});
app.get("/convert", async function (req, res) {
res.render("convert.ejs", { files: await books.getBooks("epub"), active: "convert" });
});
app.get("/batchconvert", async function (req, res) {
res.render("batch_convert.ejs", { files: await books.getBooks("-batch.zip"), active: "batchconvert" });
});
app.get("/about", function (req, res) {
res.render("about.ejs", { active: "about" });
});
module.exports = app;