diff --git a/package.json b/package.json index 8e978db..b18cd67 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "bulma": "^0.7.2", "express": "^4.16.4", "express-fileupload": "^1.0.0", + "fecha": "^3.0.2", "filenamify": "^2.1.0", "helmet": "^3.15.0", "jquery": "^3.3.1", diff --git a/server.js b/server.js index 34b5c07..7c80b64 100644 --- a/server.js +++ b/server.js @@ -9,6 +9,7 @@ const fileUpload = require('express-fileupload'); const filenamify = require('filenamify'); const unusedFilename = require('unused-filename'); const snarkdown = require('snarkdown'); +const fecha = require('fecha'); const settings = require('./settings.json'); @@ -37,12 +38,11 @@ function Server () { })); this.server.use('/files', express.static(path.join(__dirname, './public/files/'))); - this.server.use('/history', express.static(path.join(__dirname, './public/history/'))); + this.server.use('/css', express.static(path.resolve('./node_modules/bulma/css/'))); + this.server.use('/css', express.static(path.join(__dirname, './public/css/'))); this.server.use('/js', express.static(path.join(__dirname, './public/js/'))); this.server.use('/js', express.static(path.resolve('./node_modules/jquery/dist/'))); this.server.use('/js', express.static(path.resolve('./node_modules/socket.io-client/dist/'))); - this.server.use('/css', express.static(path.resolve('./node_modules/bulma/css/'))); - this.server.use('/css', express.static(path.join(__dirname, './public/css/'))); this.server.get('/', (req, res) => { const html = this.generateHomePage(); @@ -114,6 +114,15 @@ function Server () { } }); + this.server.get('/history', (req, res) => { + const html = this.generateHistoryPage(); + if (html) { + res.send(html); + } else { + res.send('Something went wrong!'); + } + }); + this.io.on('connection', socket => { this.broadcastVisitors(); @@ -171,27 +180,25 @@ Server.prototype.generateHomePage = function () { if (bookData.hasOwnProperty('fileName')) return ''; const id = fileName.replace('.json', ''); - const header = '

' + bookData.title + '

' + bookData.author + '

'; - const content = '

Contributed by ' + bookData.contributor + '

' + snarkdown(bookData.summary) + '
' - + this.fillTemplate('./templates/elements/modal.html', { - id: 'confirm_' + id, - content: this.fillTemplate('./templates/elements/messageBox.html', { - header: 'Download Your Book', - message: '
' - + '

Please ensure that you\'re using a device that can download and save the file correctly!

' - + '
' - + '
After you leave or refresh this page, it will no longer be accessible to anyone!
' - + '
' - + 'Cancel I understand, give me the link!' - + '
', - }), - }); - const footer = 'Close Take Book'; + const confirmId = 'confirm_' + id; + const added = fecha.format(new Date(bookData.added), 'dddd MMMM Do, YYYY'); const modal = this.fillTemplate('./templates/elements/modalCard.html', { id, - header, - content, - footer, + header: '

' + bookData.title + '

' + bookData.author + '

', + content: this.fillTemplate('./templates/elements/bookInfo.html', { + contributor: bookData.contributor, + fileFormat: bookData.fileFormat, + added, + summary: snarkdown(bookData.summary), + }) + + this.fillTemplate('./templates/elements/modal.html', { + id: confirmId, + content: this.fillTemplate('./templates/elements/messageBox.html', { + header: 'Download Your Book', + message: this.fillTemplate('./templates/elements/takeConfirm.html', { id }), + }), + }), + footer: 'Close Take Book', }); return this.fillTemplate('./templates/elements/book.html', { id, @@ -201,10 +208,42 @@ Server.prototype.generateHomePage = function () { modal, }); }).join(''); - const body = this.fillTemplate('./templates/pages/booksList.html', { books }); + const body = '
' + books + '
'; return this.fillTemplate('./templates/htmlContainer.html', { title: 'View', body }); } +Server.prototype.generateHistoryPage = function () { + const files = fs.readdirSync(this.historyLocation).filter(fileName => fileName.includes('.json')); + const history = files.map(fileName => { + const bookData = JSON.parse(fs.readFileSync(path.resolve(this.historyLocation, fileName), 'utf8')); + const id = fileName.replace('.json', ''); + const added = fecha.format(new Date(bookData.added), 'dddd MMMM Do, YYYY'); + const removed = fecha.format(new Date(parseInt(id)), 'dddd MMMM Do, YYYY'); + const removedTag = '
Taken' + removed + '
'; + const modal = this.fillTemplate('./templates/elements/modalCard.html', { + id, + header: '

' + bookData.title + '

' + bookData.author + '

', + content: this.fillTemplate('./templates/elements/bookInfo.html', { + contributor: bookData.contributor, + fileFormat: bookData.fileType, + added, + removedTag, + summary: snarkdown(bookData.summary), + }), + footer: 'Close', + }); + return this.fillTemplate('./templates/elements/book.html', { + id, + title: bookData.title, + author: bookData.author, + fileType: bookData.fileType, + modal, + }); + }).join(''); + const body = '
' + history + '
'; + return this.fillTemplate('./templates/htmlContainer.html', { title: 'History', resourcePath: '../', body }); +} + Server.prototype.broadcastVisitors = function () { const numberConnected = this.io.of('/').clients().connected.length; this.io.emit('connected', numberConnected); @@ -226,6 +265,7 @@ Server.prototype.addBook = function (uploadData = {}, success = () => {}, error author: uploadData.author.trim(), summary: uploadData.summary.trim(), contributor: uploadData.contributor.trim(), + added: Date.now(), fileType: book.name.substr(book.name.lastIndexOf('.')), } @@ -276,7 +316,7 @@ Server.prototype.deleteBooks = function (socketId) { if (data.socketId === socketId) { const check = this.checkId(data.bookId, (bookPath, bookDataPath) => { fs.unlinkSync(bookPath); - fs.renameSync(bookDataPath, path.resolve(this.historyLocation, data.bookId + '.json')); + fs.renameSync(bookDataPath, unusedFilename.sync(path.resolve(this.historyLocation, Date.now() + '.json'))); }); if (check === false) { console.log('couldn\'t find data.bookId'); diff --git a/settings.json b/settings.json index 5bb9e41..27e9095 100644 --- a/settings.json +++ b/settings.json @@ -6,5 +6,6 @@ "historyLocation": "./public/history/", "maxLibrarySize": 0, "maxFileSize": 0, + "maxHistory": 0, "allowedFormats": [".epub", ".mobi", ".pdf"] } \ No newline at end of file diff --git a/templates/elements/bookInfo.html b/templates/elements/bookInfo.html new file mode 100644 index 0000000..6f1c8c3 --- /dev/null +++ b/templates/elements/bookInfo.html @@ -0,0 +1,24 @@ +
+

Contributed by {{contributor}}

+
+
+
+ File Format + {{fileFormat}} +
+
+
+
+ Added + {{added}} +
+
+ {{removedTag}} +
+ +
+

{{contributor}} said:

+ {{summary}} +
+ +
\ No newline at end of file diff --git a/templates/elements/takeConfirm.html b/templates/elements/takeConfirm.html new file mode 100644 index 0000000..f6a8555 --- /dev/null +++ b/templates/elements/takeConfirm.html @@ -0,0 +1,7 @@ +
+

Please ensure that you're using a device that can download and save the file correctly!

+
+
After you leave or refresh this page, it will no longer be accessible to anyone!
+
+ Cancel I understand, give me the link! +
\ No newline at end of file diff --git a/templates/htmlContainer.html b/templates/htmlContainer.html index 7b80ea9..e795798 100644 --- a/templates/htmlContainer.html +++ b/templates/htmlContainer.html @@ -9,11 +9,11 @@ {{title}}{{titleSeparator}}{{siteTitle}} - - + + - - + + @@ -39,6 +39,10 @@ Give + + + History +