From b50247027999f7a523b8c538e49255497bccda16 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 27 Dec 2018 12:40:03 -0700 Subject: [PATCH] Get "take book" working --- public/js/little-library.js | 19 ++++++++++++++++-- server.js | 39 +++++++++++++++++++++++++++--------- templates/elements/book.html | 5 +++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/public/js/little-library.js b/public/js/little-library.js index 66cc445..6281c32 100644 --- a/public/js/little-library.js +++ b/public/js/little-library.js @@ -1,8 +1,16 @@ $(document).ready(function() { var socket = io(); - - $('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .close').click(function() { + + var downloadButton; + + socket.on('get book', function(url) { + console.log(url); + $(downloadButton).replaceWith('Download'); + }); + + $('.modal-background, .modal-close, .modal-card-head .delete, .modal .close').click(function() { $(this).closest('.modal').removeClass('is-active'); + downloadButton = undefined; }); $('.modal-button').click(function() { @@ -10,6 +18,13 @@ $(document).ready(function() { $('#' + modal).addClass('is-active'); }); + $('.take-book').click(function() { + var id = $(this).data('book'); + socket.emit('take book', id); + downloadButton = this; + $(this).addClass('is-loading'); + }); + $('#book').change(function() { var fileName = $(this).val(); if (fileName) { diff --git a/server.js b/server.js index 7935097..34b5c07 100644 --- a/server.js +++ b/server.js @@ -36,8 +36,8 @@ function Server () { }, })); - this.server.use(express.static(path.join(__dirname, './public/files/'))); - this.server.use(express.static(path.join(__dirname, './public/history/'))); + 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('/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/'))); @@ -122,7 +122,7 @@ function Server () { if (fileLocation) { console.log(socket.id + ' removed ' + bookId); const downloadLocation = fileLocation.substr(fileLocation.lastIndexOf('/')); - socket.emit('get book', './files' + downloadLocation); + socket.emit('get book', encodeURI('./files' + downloadLocation)); } }); @@ -168,10 +168,25 @@ Server.prototype.generateHomePage = function () { const files = fs.readdirSync(this.fileLocation).filter(fileName => fileName.includes('.json')); const books = files.map(fileName => { const bookData = JSON.parse(fs.readFileSync(path.resolve(this.fileLocation, fileName), 'utf8')); + 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) + '
'; - const footer = 'Close Take Book'; + 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 modal = this.fillTemplate('./templates/elements/modalCard.html', { id, header, @@ -182,6 +197,7 @@ Server.prototype.generateHomePage = function () { id, title: bookData.title, author: bookData.author, + fileType: bookData.fileType, modal, }); }).join(''); @@ -222,7 +238,7 @@ Server.prototype.addBook = function (uploadData = {}, success = () => {}, error const bookDataPath = unusedFilename.sync(path.resolve(bookPath + '.json')); fs.writeFileSync(bookDataPath, JSON.stringify(bookData)); success(); - console.log('uploaded ' + bookData.title + ' to ' + bookFilePath + ', and saved metadata to ' + bookDataPath); + // console.log('uploaded ' + bookData.title + ' to ' + bookFilePath + ', and saved metadata to ' + bookDataPath); } }); } @@ -234,8 +250,8 @@ Server.prototype.takeBook = function (bookId, socketId) { bookData.fileName = newFileName; fs.renameSync(bookPath, newFileName); fs.writeFileSync(bookDataPath, JSON.stringify(bookData)); - takenBooks.push({ socketId, bookId }); - return newFileName; + this.takenBooks.push({ socketId, bookId }); + return newFileName.replace(/\\/g, '/'); }); } @@ -245,7 +261,7 @@ Server.prototype.checkId = function (bookId, callback = () => {}) { const bookDataRaw = fs.readFileSync(bookDataPath); if (bookDataRaw) { const bookData = JSON.parse(bookDataRaw); - const bookPath = path.resolve(this.fileLocation, bookId + bookData.fileType); + const bookPath = bookData.hasOwnProperty('fileName') ? bookData.fileName : path.resolve(this.fileLocation, bookId + bookData.fileType); if (fs.existsSync(bookPath)) { return callback(bookPath, bookDataPath, bookData); } @@ -258,10 +274,13 @@ Server.prototype.checkId = function (bookId, callback = () => {}) { Server.prototype.deleteBooks = function (socketId) { this.takenBooks.forEach(data => { if (data.socketId === socketId) { - this.checkId(data.bookId, (bookPath, bookDataPath) => { + const check = this.checkId(data.bookId, (bookPath, bookDataPath) => { fs.unlinkSync(bookPath); fs.renameSync(bookDataPath, path.resolve(this.historyLocation, data.bookId + '.json')); }); + if (check === false) { + console.log('couldn\'t find data.bookId'); + } } }); this.takenBooks = this.takenBooks.filter(data => data.socketId === socketId); diff --git a/templates/elements/book.html b/templates/elements/book.html index f830a05..517c56f 100644 --- a/templates/elements/book.html +++ b/templates/elements/book.html @@ -2,6 +2,11 @@ + {{modal}} \ No newline at end of file