From 5192d7e22decf22253780f105bf04aa2967c194f Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 8 Jan 2019 16:15:06 -0700 Subject: [PATCH] Add a tool to reset visitors count --- server.js | 34 ++++++++++++--------- settings.example.json | 2 +- templates/pages/{backup.html => tools.html} | 27 ++++++++++------ 3 files changed, 39 insertions(+), 24 deletions(-) rename templates/pages/{backup.html => tools.html} (70%) diff --git a/server.js b/server.js index 40f46f7..07e3da4 100644 --- a/server.js +++ b/server.js @@ -48,7 +48,7 @@ function Server () { 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('/css', express.static(path.resolve('./node_modules/bulma/css/'))); @@ -168,12 +168,17 @@ function Server () { } }); - this.server.get('/backup', (req, res) => { - if (req.query.pass === settings.backupPassword) { + this.server.get('/tools', (req, res) => { + if (req.query.pass === settings.toolsPassword) { 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 { dl } = req.query; const saveLocation = path.resolve(this.fileLocation, dl + 'Backup.zip'); @@ -186,7 +191,7 @@ function Server () { .on('error', (error) => { console.error(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); }) .on('end', () => { @@ -194,7 +199,7 @@ function Server () { let backupLocation = saveLocation.replace(/\\/g, '/'); backupLocation = backupLocation.substr(backupLocation.lastIndexOf('/')); templateValues[dl + 'Download'] = 'Download (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); console.log('Will delete ' + saveLocation + ' in 1 hour'); setTimeout(() => { @@ -214,10 +219,10 @@ function Server () { res.status(400).send(); } }); - this.server.post('/backup', (req, res) => { - if (req.query.pass === settings.backupPassword) { + this.server.post('/tools', (req, res) => { + if (req.query.pass === settings.toolsPassword) { const templateValues = {}; - let html = this.fillTemplate('./templates/pages/backup.html', templateValues); + let html = this.fillTemplate('./templates/pages/tools.html', templateValues); const { files } = req; if (Object.keys(files).length > 0) { @@ -229,7 +234,7 @@ function Server () { if (err) { console.error(error); 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); } else { onezip.extract(uploadPath, path.resolve('./public', backupType)) @@ -239,12 +244,12 @@ function Server () { .on('error', (error) => { console.error(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); }) .on('end', () => { templateValues[backupType + 'UploadSuccess'] = 'Uploaded Successfully!'; - html = this.fillTemplate('./templates/pages/backup.html', templateValues); + html = this.fillTemplate('./templates/pages/tools.html', templateValues); res.send(html); fs.unlink(uploadPath, (err) => { if (err) { @@ -258,7 +263,7 @@ function Server () { }); } else { templateValues['generalError'] = '

' + backupType + ' is not a valid backup type.

'; - html = this.fillTemplate('./templates/pages/backup.html', templateValues); + html = this.fillTemplate('./templates/pages/tools.html', templateValues); res.send(html); } } else { @@ -288,6 +293,7 @@ function Server () { socket.on('disconnect', () => { if (!settings.hideVisitors) { this.connections--; + if (this.connections < 0) this.connections = 0; this.io.emit('update visitors', this.connections); } this.deleteBooks(socket.id); diff --git a/settings.example.json b/settings.example.json index a54dbee..11c442f 100644 --- a/settings.example.json +++ b/settings.example.json @@ -8,7 +8,7 @@ "maxFileSize": 0, "maxHistory": 0, "allowedFormats": [".epub", ".mobi", ".pdf"], - "backupPassword": "password", + "toolsPassword": "password", "hideVisitors": false, "sslPrivateKey": null, "sslCertificate": null, diff --git a/templates/pages/backup.html b/templates/pages/tools.html similarity index 70% rename from templates/pages/backup.html rename to templates/pages/tools.html index 3ba496d..de0da0c 100644 --- a/templates/pages/backup.html +++ b/templates/pages/tools.html @@ -1,16 +1,23 @@ - Backup + Tools{{titleSeparator}}{{siteTitle}} -

Backup/Restore

+

Tools

+

Management

- This page allows you to download a .zip file of your Files folder and your History + + + {{resetVisitors}} +

+

Backup/Restore

+

+ These tools allows you to download a .zip file of your Files folder and your History folder or re-import one of the zipped files you received from a backup.

-

Export

+
Export

@@ -22,7 +29,7 @@ {{historyDownload}}

-

Import

+
Import
{{generalError}}

@@ -58,14 +65,16 @@ function getParameterByName(name, url) { window.onload = function() { var pass = getParameterByName('pass'); + var resetVisitorsLink = document.getElementById('resetVisitors'); var filesDownloadLink = document.getElementById('filesDownload'); var historyDownloadLink = document.getElementById('historyDownload'); var filesForm = document.getElementById('filesForm'); var historyForm = document.getElementById('historyForm'); - filesDownloadLink.href = './backup?pass=' + pass + '&dl=files'; - historyDownloadLink.href = './backup?pass=' + pass + '&dl=history'; - filesForm.action = './backup?pass=' + pass; - historyForm.action = './backup?pass=' + pass; + resetVisitorsLink.href = './tools?pass=' + pass + '&do=resetVisitors'; + filesDownloadLink.href = './tools?pass=' + pass + '&dl=files'; + historyDownloadLink.href = './tools?pass=' + pass + '&dl=history'; + filesForm.action = './tools?pass=' + pass; + historyForm.action = './tools?pass=' + pass; }