2022-06-16 04:13:44 -05:00
|
|
|
const express = require("express");
|
2022-04-19 07:22:53 -05:00
|
|
|
const app = express();
|
2022-06-16 04:13:44 -05:00
|
|
|
const { MongoClient } = require("mongodb");
|
|
|
|
const cors = require("cors");
|
|
|
|
const getData = require("./modules/getData.js");
|
|
|
|
const fs = require("fs");
|
2022-06-21 10:53:17 -05:00
|
|
|
const {
|
|
|
|
port,
|
|
|
|
URI,
|
|
|
|
database,
|
|
|
|
collection,
|
|
|
|
screenshotDirectory,
|
|
|
|
pdfDirectory,
|
|
|
|
} = require("./config.js");
|
2022-06-16 04:13:44 -05:00
|
|
|
const fetch = require("cross-fetch");
|
2022-06-17 20:18:48 -05:00
|
|
|
const sanitize = require("sanitize-filename");
|
2022-04-19 07:22:53 -05:00
|
|
|
|
2022-05-26 11:15:07 -05:00
|
|
|
const client = new MongoClient(URI);
|
2022-06-04 05:07:35 -05:00
|
|
|
const db = client.db(database);
|
|
|
|
const list = db.collection(collection);
|
2022-04-19 07:22:53 -05:00
|
|
|
|
2022-06-20 14:29:22 -05:00
|
|
|
// Create the storage directories if they do not exist
|
2022-06-17 20:18:48 -05:00
|
|
|
if (!fs.existsSync(screenshotDirectory)) {
|
|
|
|
fs.mkdirSync(screenshotDirectory, { recursive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(pdfDirectory)) {
|
|
|
|
fs.mkdirSync(pdfDirectory, { recursive: true });
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:40:08 -05:00
|
|
|
app.use(cors());
|
|
|
|
|
2022-04-19 07:22:53 -05:00
|
|
|
app.use(express.json());
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.get("/api", async (req, res) => {
|
2022-04-19 07:22:53 -05:00
|
|
|
const data = await getDoc();
|
|
|
|
res.send(data);
|
|
|
|
});
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.get("/screenshots/:id", async (req, res) => {
|
|
|
|
res.sendFile(
|
2022-06-21 10:53:17 -05:00
|
|
|
__dirname + "/" + screenshotDirectory + "/" + sanitize(req.params.id),
|
2022-06-16 04:13:44 -05:00
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
res.sendFile(__dirname + "/pages/404.html");
|
|
|
|
}
|
2022-06-05 22:58:03 -05:00
|
|
|
}
|
2022-06-16 04:13:44 -05:00
|
|
|
);
|
2022-06-02 13:30:51 -05:00
|
|
|
});
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.get("/pdfs/:id", async (req, res) => {
|
2022-06-21 11:02:59 -05:00
|
|
|
res.sendFile(
|
|
|
|
__dirname + "/" + pdfDirectory + "/" + sanitize(req.params.id),
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
res.sendFile(__dirname + "/pages/404.html");
|
|
|
|
}
|
2022-06-05 22:58:03 -05:00
|
|
|
}
|
2022-06-21 11:02:59 -05:00
|
|
|
);
|
2022-06-02 13:30:51 -05:00
|
|
|
});
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.post("/api", async (req, res) => {
|
2022-04-26 05:50:07 -05:00
|
|
|
const pageToVisit = req.body.link;
|
2022-06-05 12:12:49 -05:00
|
|
|
const id = req.body._id;
|
2022-06-16 04:13:44 -05:00
|
|
|
const getTitle = async (url) => {
|
2022-06-05 12:12:49 -05:00
|
|
|
let body;
|
|
|
|
await fetch(url)
|
2022-06-16 04:13:44 -05:00
|
|
|
.then((res) => res.text())
|
|
|
|
.then((text) => (body = text));
|
2022-06-05 12:12:49 -05:00
|
|
|
// regular expression to parse contents of the <title> tag
|
2022-06-17 20:18:48 -05:00
|
|
|
let match = body.match(/<title.*>([^<]*)<\/title>/);
|
2022-06-05 12:12:49 -05:00
|
|
|
return match[1];
|
2022-06-16 04:13:44 -05:00
|
|
|
};
|
2022-05-26 11:15:07 -05:00
|
|
|
|
|
|
|
try {
|
2022-06-05 12:12:49 -05:00
|
|
|
req.body.title = await getTitle(req.body.link);
|
|
|
|
await insertDoc(req.body);
|
|
|
|
|
|
|
|
res.send("DONE!");
|
2022-06-05 12:18:22 -05:00
|
|
|
getData(pageToVisit, req.body._id);
|
2022-05-26 11:15:07 -05:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
insertDoc(req.body);
|
|
|
|
}
|
2022-04-19 07:22:53 -05:00
|
|
|
});
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.put("/api", async (req, res) => {
|
2022-06-04 05:07:35 -05:00
|
|
|
const id = req.body._id;
|
|
|
|
|
|
|
|
await updateDoc(id, req.body);
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
res.send("Updated!");
|
2022-06-04 05:07:35 -05:00
|
|
|
});
|
|
|
|
|
2022-06-16 04:13:44 -05:00
|
|
|
app.delete("/api", async (req, res) => {
|
2022-06-04 05:07:35 -05:00
|
|
|
const id = req.body.id;
|
2022-04-26 01:20:42 -05:00
|
|
|
|
|
|
|
await deleteDoc(id);
|
|
|
|
|
2022-06-25 14:38:37 -05:00
|
|
|
res.send(`Link with _id:${id} deleted.`);
|
2022-04-26 01:20:42 -05:00
|
|
|
});
|
|
|
|
|
2022-06-04 05:07:35 -05:00
|
|
|
async function updateDoc(id, updatedListing) {
|
|
|
|
try {
|
2022-06-16 04:13:44 -05:00
|
|
|
await list.updateOne({ _id: id }, { $set: updatedListing });
|
|
|
|
} catch (err) {
|
2022-06-04 05:07:35 -05:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:22:53 -05:00
|
|
|
async function insertDoc(doc) {
|
|
|
|
try {
|
2022-06-04 05:07:35 -05:00
|
|
|
await list.insertOne(doc);
|
2022-06-16 04:13:44 -05:00
|
|
|
} catch (err) {
|
2022-04-19 07:22:53 -05:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDoc() {
|
|
|
|
try {
|
|
|
|
const result = await list.find({}).toArray();
|
|
|
|
|
|
|
|
return result;
|
2022-06-16 04:13:44 -05:00
|
|
|
} catch (err) {
|
2022-04-19 07:22:53 -05:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 01:20:42 -05:00
|
|
|
async function deleteDoc(doc) {
|
2022-06-17 20:18:48 -05:00
|
|
|
doc = sanitize(doc);
|
2022-04-26 01:20:42 -05:00
|
|
|
try {
|
2022-06-16 04:13:44 -05:00
|
|
|
const result = await list.deleteOne({ _id: doc });
|
2022-05-11 04:23:24 -05:00
|
|
|
|
2022-06-21 10:53:17 -05:00
|
|
|
fs.unlink(screenshotDirectory + "/" + doc + ".png", (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
2022-06-02 13:30:51 -05:00
|
|
|
}
|
2022-06-21 10:53:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.unlink(pdfDirectory + "/" + doc + ".pdf", (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
2022-06-02 13:30:51 -05:00
|
|
|
}
|
2022-06-21 10:53:17 -05:00
|
|
|
});
|
2022-06-02 13:30:51 -05:00
|
|
|
|
2022-05-11 04:23:24 -05:00
|
|
|
return result;
|
2022-06-16 04:13:44 -05:00
|
|
|
} catch (err) {
|
2022-04-26 01:20:42 -05:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:22:53 -05:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Success! running on port ${port}.`);
|
2022-04-26 15:30:36 -05:00
|
|
|
client.connect();
|
2022-06-02 08:31:53 -05:00
|
|
|
});
|