-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (99 loc) · 3.2 KB
/
index.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
const express = require("express");
const exphbs = require("express-handlebars");
const path = require("path");
const fs = require("fs-extra");
const { format } = require("timeago.js");
const Archiver = require("archiver");
const config = require("./config");
const ArchiveManager = require("./modules/ArchiveManager");
const archivesDirectory = config.archiveDirectoryName;
const mainDirectory = path.join(__dirname, archivesDirectory);
const archiveManager = new ArchiveManager({ mainDirectory });
const app = express();
const hbs = exphbs.create({
defaultLayout: "main.hbs",
helpers: {
timeago: function (time) {
return format(time, "en_US");
},
},
});
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
fs.ensureDirSync(archivesDirectory);
app.use(express.static("public"));
app.use("/archives", express.static(archivesDirectory));
app.get("/", (req, res) => {
return res.render("index.hbs", {
archives: archiveManager.getArchives().map((archive) => {
let dir = path.join(archivesDirectory, archive.folder);
return {
full: fs.pathExistsSync(path.join(dir, "full", "index.html"))
? path.join(dir, "full", "index.html")
: "",
archiveorg: fs.pathExistsSync(path.join(dir, "archive.org.txt"))
? fs.readFileSync(path.join(dir, "archive.org.txt"))
: "",
favicon: fs.pathExistsSync(path.join(dir, "favicon.ico"))
? path.join(dir, "favicon.ico")
: "",
dom: fs.pathExistsSync(path.join(dir, "output.html"))
? path.join(dir, "output.html")
: "",
pdf: fs.pathExistsSync(path.join(dir, "output.pdf"))
? path.join(dir, "output.pdf")
: "",
screenshot: fs.pathExistsSync(path.join(dir, "output.png"))
? path.join(dir, "output.png")
: "",
...archive,
};
}),
});
});
app.get("/s", async (req, res) => {
if (typeof req.query.url === "undefined") {
return res.status(404).json({
status: "failed",
message: "no parameter",
});
}
let url = req.query.url;
let expression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/g;
let regex = new RegExp(expression);
if (url.length < 1 || !url.match(regex)) {
return res.status(404).json({
status: "failed",
message: "failed check",
});
}
try {
let result = await archiveManager.addUrl(url);
return res.status(200).send(result);
} catch (error) {
return res.status(404).send(error);
}
});
app.get("/d/:id", function (req, res) {
const archive = archiveManager.getArchivePath(req.params.id);
if (typeof archive === "undefined") {
console.error(`[!] download error: id not found ${req.params.id}`);
return res.status(404).send("not found");
}
console.error(`[@] sending archive zip for ${req.params.id}`);
res.writeHead(200, {
"Content-Type": "application/zip",
"Content-disposition": `attachment; filename=${archive.folder}.zip`,
});
const zip = Archiver("zip");
zip.pipe(res);
return zip
.directory(
path.join(__dirname, archivesDirectory, archive.folder, "full"),
false
)
.finalize();
});
app.listen(config.serverPort, () =>
console.log(`[@] ark running on ${config.serverPort}!`)
);