-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (90 loc) · 3.18 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
const express = require("express");
const app = express();
const path = require("path");
const https = require("https");
const fs = require("fs");
const downloadMD = require("./src/mangadex");
const downloadNH = require("./src/nhentai");
const dotenv = require("dotenv");
// Read the environment variable
dotenv.config();
const PORT = process.env.PORT || 3069;
// Middleware to log the requested URL
app.use((req, res, next) => {
console.log(`Request URL: ${req.url}`);
next();
});
// Middleware to disable caching
function noCache(req, res, next) {
res.set("Cache-Control", "no-store, no-cache, must-revalidate, private");
res.set("Pragma", "no-cache");
res.set("Expires", "0");
next();
}
// Route to process the chapter
app.post("/download/md/:chapterID", noCache, async (req, res) => {
const chapterID = req.params.chapterID;
try {
await downloadMD(chapterID);
res.json({ success: true });
} catch (error) {
console.error("Error processing the chapter:", error);
res.status(500).json({ success: false });
}
});
app.post("/download/nhen/:galleryID", noCache, async (req, res) => {
const galleryID = req.params.galleryID;
try {
await downloadNH(galleryID);
res.json({ success: true });
} catch (error) {
console.error("Error processing the chapter:", error);
res.status(500).json({ success: false });
}
});
// Route to download the zipped chapter
app.get("/download/md/:chapterID.zip", noCache, (req, res) => {
const chapterID = req.params.chapterID;
const zipFilePath = path.join(__dirname, "md", `${chapterID}.zip`);
res.download(zipFilePath, (err) => {
if (err && !res.headersSent) {
console.error("Error sending the zip file:", err);
res.status(500).send("Error sending the zip file.");
} else if (!err) {
console.log("Zip file sent successfully.");
}
});
});
app.get("/download/nhen/:galleryID.zip", noCache, (req, res) => {
const galleryID = req.params.galleryID;
const zipFilePath = path.join(__dirname, "nhen", `${galleryID}.zip`);
res.download(zipFilePath, (err) => {
if (err && !res.headersSent) {
console.error("Error sending the zip file:", err);
res.status(500).send("Error sending the zip file.");
} else if (!err) {
console.log("Zip file sent successfully.");
}
});
});
// Create the server
// Checking if the server does utilize https or not
if (process.env.HTTPS && process.env.HTTPS.toLowerCase() === "true") {
// Bind the credentials
const certsDir = process.env.CERTS_DIR;
const pkey = fs.readFileSync(`${certsDir}/${process.env.PRIVATEKEY}`);
const certificate = fs.readFileSync(`${certsDir}/${process.env.CERT}`);
const ca = fs.readFileSync(`${certsDir}/${process.env.CA}`);
const credentials = { key: pkey, cert: certificate, ca: ca };
// Launch it
console.log("Running on HTTPS.");
https.createServer(credentials, app).listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
} else {
// Normal http server
console.log("Running on HTTP.");
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
}