Added fully archive support!

This commit is contained in:
Daniel 2022-06-02 23:00:51 +04:30
parent b18ef2b905
commit 1d23855eac
10 changed files with 135 additions and 80 deletions

View File

@ -4,8 +4,8 @@ const fetch = require('cross-fetch');
const config = require('../../src/config.js'); const config = require('../../src/config.js');
const fs = require('fs'); const fs = require('fs');
const screenshotDirectory = config.API.STORAGE_LOCATION + '/Webmarker/screenshot\'s/'; const screenshotDirectory = config.API.STORAGE_LOCATION + '/LinkWarden/screenshot\'s/';
const pdfDirectory = config.API.STORAGE_LOCATION + '/Webmarker/pdf\'s/'; const pdfDirectory = config.API.STORAGE_LOCATION + '/LinkWarden/pdf\'s/';
if (!fs.existsSync(screenshotDirectory)){ if (!fs.existsSync(screenshotDirectory)){
fs.mkdirSync(screenshotDirectory, { recursive: true }); fs.mkdirSync(screenshotDirectory, { recursive: true });

3
api/package-lock.json generated
View File

@ -4623,7 +4623,8 @@
"ws": { "ws": {
"version": "8.6.0", "version": "8.6.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.6.0.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-8.6.0.tgz",
"integrity": "sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw==" "integrity": "sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw==",
"requires": {}
}, },
"xdg-basedir": { "xdg-basedir": {
"version": "4.0.0", "version": "4.0.0",

View File

@ -3,7 +3,8 @@ const app = express();
const { MongoClient } = require('mongodb'); const { MongoClient } = require('mongodb');
const cors = require('cors'); const cors = require('cors');
const config = require('../src/config.js'); const config = require('../src/config.js');
const getData = require('./modules/getData.js') const getData = require('./modules/getData.js');
const fs = require('fs');
const port = config.API.PORT; const port = config.API.PORT;
@ -22,6 +23,14 @@ app.get('/api', async (req, res) => {
res.send(data); res.send(data);
}); });
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);
});
app.post('/api', async (req, res) => { app.post('/api', async (req, res) => {
const pageToVisit = req.body.link; const pageToVisit = req.body.link;
@ -77,6 +86,19 @@ async function deleteDoc(doc) {
const list = db.collection(collection); const list = db.collection(collection);
const result = await list.deleteOne({"_id": doc}); const result = await list.deleteOne({"_id": doc});
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);
}
});
return result; return result;
} }

View File

@ -1,8 +1,7 @@
import { useState } from 'react'; import { useState } from 'react';
import { nanoid } from 'nanoid'
import '../styles/AddItem.css'; import '../styles/AddItem.css';
import config from '../config';
import TagSelection from './TagSelection'; import TagSelection from './TagSelection';
import addItem from '../modules/addItem';
const AddItem = ({onExit, reFetch, tags}) => { const AddItem = ({onExit, reFetch, tags}) => {
const [name, setName] = useState(''); const [name, setName] = useState('');
@ -22,54 +21,6 @@ const AddItem = ({onExit, reFetch, tags}) => {
setTag(value.map(e => e.value.toLowerCase())); setTag(value.map(e => e.value.toLowerCase()));
} }
async function submitBookmark() {
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
if(name !== '' && isValidHttpUrl(link) && tag !== '') {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
// Adding method type
method: "POST",
// Adding body or contents to send
body: JSON.stringify({
_id: nanoid(),
name: name,
title: '',
link: link,
tag: tag
}),
// Adding headers to the request
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.text())
.then(message => {console.log(message)})
.then(() => reFetch());
onExit();
} else if(name !== '' && link !== '' && tag !== '') {
alert('Please make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
}
else {
alert('Please fill all fields and make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
}
}
function abort(e) { function abort(e) {
if (e.target.className === "add-overlay") { if (e.target.className === "add-overlay") {
onExit(); onExit();
@ -88,11 +39,11 @@ const AddItem = ({onExit, reFetch, tags}) => {
<input onChange={SetLink} className="AddItem-input" type="search" placeholder="e.g. https://example.com/"/> <input onChange={SetLink} className="AddItem-input" type="search" placeholder="e.g. https://example.com/"/>
<h3>Tags:</h3> <h3>Tags:</h3>
<TagSelection setTags={SetTags} tags={tags} /> <TagSelection setTags={SetTags} tags={tags} />
<button onClick={submitBookmark} className="upload-btn">Upload &#xf093;</button> <button onClick={() => addItem(name, link, tag, reFetch, onExit)} className="upload-btn">Upload &#xf093;</button>
</div> </div>
</fieldset> </fieldset>
</> </>
) )
} }
export default AddItem export default AddItem;

View File

@ -1,28 +1,9 @@
import '../styles/List.css'; import '../styles/List.css';
import config from '../config';
import LazyLoad from 'react-lazyload'; import LazyLoad from 'react-lazyload';
import ViewArchived from './ViewArchived';
import deleteEntity from '../modules/deleteEntity';
const List = ({data, reFetch}) => { const List = ({data, reFetch}) => {
function deleteEntity(id) {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
// Adding method type
method: "DELETE",
// Adding body or contents to send
body: JSON.stringify({id}),
// Adding headers to the request
headers: {
"Content-type": "application/json; charset=UTF-8",
}
})
.then(res => res.text())
.then(message => {console.log(message)})
.then(() => reFetch())
}
return ( return (
<div className="list"> <div className="list">
{data.map((e, i) => { {data.map((e, i) => {
@ -34,7 +15,9 @@ const List = ({data, reFetch}) => {
<div className="img-content-grp"> <div className="img-content-grp">
<img src={favicon} /> <img src={favicon} />
<div className="list-entity-content"> <div className="list-entity-content">
<div className='row-name'><span className="num">{i + 1}.</span> {e.name} <a target="_blank" href={e.link}>({url.hostname})</a></div> <div className='row-name'>
<span className="num">{i + 1}.</span> {e.name} <a target="_blank" href={e.link}>({url.hostname})</a>
</div>
<div>{e.title}</div> <div>{e.title}</div>
<div className="tags"> <div className="tags">
{e.tag.map((e, i) => { {e.tag.map((e, i) => {
@ -43,7 +26,10 @@ const List = ({data, reFetch}) => {
</div> </div>
</div> </div>
</div> </div>
<div className="delete" onClick={() => deleteEntity(e._id)}>&#xf2ed;</div> <div className='options'>
<ViewArchived className='view-archived' id={e._id} />
<div className="delete" onClick={() => deleteEntity(e._id, reFetch)}>&#xf2ed;</div>
</div>
</div> </div>
</LazyLoad> </LazyLoad>
} catch (e) { } catch (e) {

View File

@ -0,0 +1,17 @@
import '../styles/ViewArchived.css';
import config from '../config';
const ViewArchived = ({ id }) => {
const screenshotPath = config.API.ADDRESS + ":" + config.API.PORT + '/screenshots/' + id + '.png';
const pdfPath = config.API.ADDRESS + ":" + config.API.PORT + '/pdfs/' + id + '.pdf';
return (
<div className='view-archived'>
<a href={screenshotPath} target='_blank'>Screenshot</a>
<hr className='seperator' />
<a href={pdfPath} target='_blank'>PDF</a>
</div>
)
}
export default ViewArchived;

46
src/modules/addItem.js Normal file
View File

@ -0,0 +1,46 @@
import config from '../config';
import { nanoid } from 'nanoid';
const addItem = async (name, link, tag, reFetch, onExit) => {
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
if(name !== '' && isValidHttpUrl(link) && tag !== '') {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
method: "POST",
body: JSON.stringify({
_id: nanoid(),
name: name,
title: '',
link: link,
tag: tag
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.text())
.then(message => {console.log(message)})
.then(() => reFetch());
onExit();
} else if(name !== '' && link !== '' && tag !== '') {
alert('Please make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
}
else {
alert('Please fill all fields and make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
}
}
export default addItem;

View File

@ -0,0 +1,17 @@
import config from '../config';
const deleteEntity = (id, reFetch) => {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
method: "DELETE",
body: JSON.stringify({id}),
headers: {
"Content-type": "application/json; charset=UTF-8",
}
})
.then(res => res.text())
.then(message => {console.log(message)})
.then(() => reFetch())
}
export default deleteEntity;

View File

@ -110,3 +110,8 @@
.num { .num {
font-size: 1rem; font-size: 1rem;
} }
.options {
display: flex;
align-items: center;
}

View File

@ -0,0 +1,10 @@
.view-archived {
display: flex;
flex-direction: column;
text-align: left;
}
.seperator {
width: 100%;
color: white;
}