2022-04-19 07:22:53 -05:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2022-05-26 11:15:07 -05:00
|
|
|
const { MongoClient } = require('mongodb');
|
2022-05-10 11:40:08 -05:00
|
|
|
const cors = require('cors');
|
2022-06-02 08:31:53 -05:00
|
|
|
const config = require('../src/config.js');
|
2022-06-02 13:30:51 -05:00
|
|
|
const getData = require('./modules/getData.js');
|
|
|
|
const fs = require('fs');
|
2022-04-22 07:13:22 -05:00
|
|
|
|
2022-06-02 08:31:53 -05:00
|
|
|
const port = config.API.PORT;
|
2022-04-19 07:22:53 -05:00
|
|
|
|
2022-06-02 08:31:53 -05:00
|
|
|
const URI = config.API.MONGODB_URI;
|
|
|
|
const database = config.API.DB_NAME;
|
|
|
|
const collection = config.API.COLLECTION_NAME;
|
2022-04-19 07:22:53 -05:00
|
|
|
|
2022-05-26 11:15:07 -05:00
|
|
|
const client = new MongoClient(URI);
|
2022-04-19 07:22:53 -05:00
|
|
|
|
2022-05-10 11:40:08 -05:00
|
|
|
app.use(cors());
|
|
|
|
|
2022-04-19 07:22:53 -05:00
|
|
|
app.use(express.json());
|
|
|
|
|
2022-05-26 11:15:07 -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-02 13:30:51 -05:00
|
|
|
app.get('/screenshots/:id', async (req, res) => {
|
|
|
|
res.sendFile(config.API.STORAGE_LOCATION + '/LinkWarden/screenshot\'s/' + req.params.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/pdfs/:id', async (req, res) => {
|
|
|
|
res.sendFile(config.API.STORAGE_LOCATION + '/LinkWarden/pdf\'s/' + req.params.id);
|
|
|
|
});
|
|
|
|
|
2022-05-26 11:15:07 -05:00
|
|
|
app.post('/api', async (req, res) => {
|
2022-04-26 05:50:07 -05:00
|
|
|
const pageToVisit = req.body.link;
|
2022-05-26 11:15:07 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
const dataResult = await getData(pageToVisit, req.body._id);
|
|
|
|
req.body.title = dataResult;
|
|
|
|
insertDoc(req.body);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
insertDoc(req.body);
|
|
|
|
} finally {
|
|
|
|
res.send('DONE!');
|
|
|
|
}
|
2022-04-19 07:22:53 -05:00
|
|
|
});
|
|
|
|
|
2022-05-26 11:15:07 -05:00
|
|
|
app.delete('/api', async (req, res) => {
|
2022-04-26 01:20:42 -05:00
|
|
|
const id = req.body.id.toString();
|
|
|
|
|
|
|
|
await deleteDoc(id);
|
|
|
|
|
2022-05-26 11:15:07 -05:00
|
|
|
res.send(`Bookmark with _id:${id} deleted.`);
|
2022-04-26 01:20:42 -05:00
|
|
|
});
|
|
|
|
|
2022-04-19 07:22:53 -05:00
|
|
|
async function insertDoc(doc) {
|
|
|
|
try {
|
2022-05-11 04:23:24 -05:00
|
|
|
const db = client.db(database);
|
|
|
|
const list = db.collection(collection);
|
2022-04-19 07:22:53 -05:00
|
|
|
const result = await list.insertOne(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDoc() {
|
|
|
|
try {
|
2022-05-11 04:23:24 -05:00
|
|
|
const db = client.db(database);
|
|
|
|
const list = db.collection(collection);
|
2022-04-19 07:22:53 -05:00
|
|
|
const result = await list.find({}).toArray();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 01:20:42 -05:00
|
|
|
async function deleteDoc(doc) {
|
|
|
|
try {
|
2022-05-11 04:23:24 -05:00
|
|
|
const db = client.db(database);
|
|
|
|
const list = db.collection(collection);
|
2022-05-26 11:15:07 -05:00
|
|
|
const result = await list.deleteOne({"_id": doc});
|
2022-05-11 04:23:24 -05:00
|
|
|
|
2022-06-02 13:30:51 -05:00
|
|
|
fs.unlink(config.API.STORAGE_LOCATION + '/LinkWarden/screenshot\'s/' + doc + '.png', (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.unlink(config.API.STORAGE_LOCATION + '/LinkWarden/pdf\'s/' + doc + '.pdf', (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-05-11 04:23:24 -05:00
|
|
|
return result;
|
2022-04-26 01:20:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
catch(err) {
|
|
|
|
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
|
|
|
});
|