Add a tool to reset visitors count

This commit is contained in:
Robbie Antenesse 2019-01-08 16:15:06 -07:00
parent 3d3f9742f7
commit 5192d7e22d
3 changed files with 39 additions and 24 deletions

View File

@ -48,7 +48,7 @@ function Server () {
fileSize: (settings.maxFileSize > 0 ? settings.maxFileSize * 1024 * 1024 : Infinity), // filesize in bytes (settings accepts MB) fileSize: (settings.maxFileSize > 0 ? settings.maxFileSize * 1024 * 1024 : Infinity), // filesize in bytes (settings accepts MB)
}, },
})); }));
this.server.use('/backup', fileUpload()); // Allow file upload on backup with no limits. this.server.use('/tools', fileUpload()); // Allow file upload on backup with no limits.
this.server.use('/files', express.static(path.join(__dirname, './public/files/'))); this.server.use('/files', express.static(path.join(__dirname, './public/files/')));
this.server.use('/css', express.static(path.resolve('./node_modules/bulma/css/'))); this.server.use('/css', express.static(path.resolve('./node_modules/bulma/css/')));
@ -168,12 +168,17 @@ function Server () {
} }
}); });
this.server.get('/backup', (req, res) => { this.server.get('/tools', (req, res) => {
if (req.query.pass === settings.backupPassword) { if (req.query.pass === settings.toolsPassword) {
const templateValues = {}; const templateValues = {};
let html = this.fillTemplate('./templates/pages/backup.html', templateValues); let html = this.fillTemplate('./templates/pages/tools.html', templateValues);
if (req.query.dl && ['files', 'history'].includes(req.query.dl)) { if (req.query.do && ['resetVisitors'].includes(req.query.do)) {
this.connections = 0;
templateValues.resetVisitors = 'Done!';
html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html);
} else if (req.query.dl && ['files', 'history'].includes(req.query.dl)) {
const onezip = require('onezip'); const onezip = require('onezip');
const { dl } = req.query; const { dl } = req.query;
const saveLocation = path.resolve(this.fileLocation, dl + 'Backup.zip'); const saveLocation = path.resolve(this.fileLocation, dl + 'Backup.zip');
@ -186,7 +191,7 @@ function Server () {
.on('error', (error) => { .on('error', (error) => {
console.error(error); console.error(error);
templateValues[dl + 'Download'] = 'Something went wrong: ' + JSON.stringify(error); templateValues[dl + 'Download'] = 'Something went wrong: ' + JSON.stringify(error);
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
}) })
.on('end', () => { .on('end', () => {
@ -194,7 +199,7 @@ function Server () {
let backupLocation = saveLocation.replace(/\\/g, '/'); let backupLocation = saveLocation.replace(/\\/g, '/');
backupLocation = backupLocation.substr(backupLocation.lastIndexOf('/')); backupLocation = backupLocation.substr(backupLocation.lastIndexOf('/'));
templateValues[dl + 'Download'] = '<a download href="' + encodeURI('./files' + backupLocation) + '">Download</a> (This will be removed from the server in 1 hour)'; templateValues[dl + 'Download'] = '<a download href="' + encodeURI('./files' + backupLocation) + '">Download</a> (This will be removed from the server in 1 hour)';
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
console.log('Will delete ' + saveLocation + ' in 1 hour'); console.log('Will delete ' + saveLocation + ' in 1 hour');
setTimeout(() => { setTimeout(() => {
@ -214,10 +219,10 @@ function Server () {
res.status(400).send(); res.status(400).send();
} }
}); });
this.server.post('/backup', (req, res) => { this.server.post('/tools', (req, res) => {
if (req.query.pass === settings.backupPassword) { if (req.query.pass === settings.toolsPassword) {
const templateValues = {}; const templateValues = {};
let html = this.fillTemplate('./templates/pages/backup.html', templateValues); let html = this.fillTemplate('./templates/pages/tools.html', templateValues);
const { files } = req; const { files } = req;
if (Object.keys(files).length > 0) { if (Object.keys(files).length > 0) {
@ -229,7 +234,7 @@ function Server () {
if (err) { if (err) {
console.error(error); console.error(error);
templateValues[backupType + 'UploadSuccess'] = 'Could not upload the file.'; templateValues[backupType + 'UploadSuccess'] = 'Could not upload the file.';
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
} else { } else {
onezip.extract(uploadPath, path.resolve('./public', backupType)) onezip.extract(uploadPath, path.resolve('./public', backupType))
@ -239,12 +244,12 @@ function Server () {
.on('error', (error) => { .on('error', (error) => {
console.error(error); console.error(error);
templateValues[backupType + 'UploadSuccess'] = 'Something went wrong: ' + JSON.stringify(error); templateValues[backupType + 'UploadSuccess'] = 'Something went wrong: ' + JSON.stringify(error);
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
}) })
.on('end', () => { .on('end', () => {
templateValues[backupType + 'UploadSuccess'] = 'Uploaded Successfully!'; templateValues[backupType + 'UploadSuccess'] = 'Uploaded Successfully!';
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
fs.unlink(uploadPath, (err) => { fs.unlink(uploadPath, (err) => {
if (err) { if (err) {
@ -258,7 +263,7 @@ function Server () {
}); });
} else { } else {
templateValues['generalError'] = '<p>' + backupType + ' is not a valid backup type.</p>'; templateValues['generalError'] = '<p>' + backupType + ' is not a valid backup type.</p>';
html = this.fillTemplate('./templates/pages/backup.html', templateValues); html = this.fillTemplate('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
} }
} else { } else {
@ -288,6 +293,7 @@ function Server () {
socket.on('disconnect', () => { socket.on('disconnect', () => {
if (!settings.hideVisitors) { if (!settings.hideVisitors) {
this.connections--; this.connections--;
if (this.connections < 0) this.connections = 0;
this.io.emit('update visitors', this.connections); this.io.emit('update visitors', this.connections);
} }
this.deleteBooks(socket.id); this.deleteBooks(socket.id);

View File

@ -8,7 +8,7 @@
"maxFileSize": 0, "maxFileSize": 0,
"maxHistory": 0, "maxHistory": 0,
"allowedFormats": [".epub", ".mobi", ".pdf"], "allowedFormats": [".epub", ".mobi", ".pdf"],
"backupPassword": "password", "toolsPassword": "password",
"hideVisitors": false, "hideVisitors": false,
"sslPrivateKey": null, "sslPrivateKey": null,
"sslCertificate": null, "sslCertificate": null,

View File

@ -1,16 +1,23 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Backup</title> <title>Tools{{titleSeparator}}{{siteTitle}}</title>
</head> </head>
<body> <body>
<h1>Backup/Restore</h1> <h1>Tools</h1>
<h3>Management</h3>
<p> <p>
This page allows you to download a <code>.zip</code> file of your Files folder and your History <a href="#" id="resetVisitors">
<button>Reset Visitors Count</button>
</a> {{resetVisitors}}
</p>
<h3>Backup/Restore</h3>
<p>
These tools allows you to download a <code>.zip</code> file of your Files folder and your History
folder or re-import one of the zipped files you received from a backup. folder or re-import one of the zipped files you received from a backup.
</p> </p>
<h2>Export</h2> <h5>Export</h5>
<p> <p>
<a href="#" id="filesDownload"> <a href="#" id="filesDownload">
<button>Back Up the <code>files</code> Folder</button> <button>Back Up the <code>files</code> Folder</button>
@ -22,7 +29,7 @@
</a> {{historyDownload}} </a> {{historyDownload}}
</p> </p>
<h2>Import</h2> <h5>Import</h5>
{{generalError}} {{generalError}}
<form id="filesForm" action="." method="post" enctype="multipart/form-data"> <form id="filesForm" action="." method="post" enctype="multipart/form-data">
<p> <p>
@ -58,14 +65,16 @@ function getParameterByName(name, url) {
window.onload = function() { window.onload = function() {
var pass = getParameterByName('pass'); var pass = getParameterByName('pass');
var resetVisitorsLink = document.getElementById('resetVisitors');
var filesDownloadLink = document.getElementById('filesDownload'); var filesDownloadLink = document.getElementById('filesDownload');
var historyDownloadLink = document.getElementById('historyDownload'); var historyDownloadLink = document.getElementById('historyDownload');
var filesForm = document.getElementById('filesForm'); var filesForm = document.getElementById('filesForm');
var historyForm = document.getElementById('historyForm'); var historyForm = document.getElementById('historyForm');
filesDownloadLink.href = './backup?pass=' + pass + '&dl=files'; resetVisitorsLink.href = './tools?pass=' + pass + '&do=resetVisitors';
historyDownloadLink.href = './backup?pass=' + pass + '&dl=history'; filesDownloadLink.href = './tools?pass=' + pass + '&dl=files';
filesForm.action = './backup?pass=' + pass; historyDownloadLink.href = './tools?pass=' + pass + '&dl=history';
historyForm.action = './backup?pass=' + pass; filesForm.action = './tools?pass=' + pass;
historyForm.action = './tools?pass=' + pass;
} }
</script> </script>
</body> </body>