2019-01-10 23:20:48 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const snarkdown = require('snarkdown');
|
|
|
|
const fecha = require('fecha');
|
|
|
|
|
2019-01-12 01:20:05 +01:00
|
|
|
const settings = require('../settings.json');
|
|
|
|
|
2019-01-10 23:20:48 +01:00
|
|
|
module.exports = function (app) {
|
|
|
|
app.server.get('/', (req, res) => {
|
|
|
|
const files = fs.readdirSync(app.fileLocation).filter(fileName => fileName.includes('.json'))
|
|
|
|
.map(fileName => { // Cache the file data so sorting doesn't need to re-check each file
|
2019-01-12 01:20:05 +01:00
|
|
|
const stats = fs.statSync(path.resolve(app.fileLocation, fileName));
|
|
|
|
return {
|
|
|
|
name: fileName,
|
|
|
|
size: stats.size / (1000 * 1000),
|
|
|
|
time: stats.mtime.getTime(),
|
|
|
|
};
|
|
|
|
}).sort((a, b) => a.time - b.time); // Sort from oldest to newest.
|
2019-01-10 23:20:48 +01:00
|
|
|
|
2019-01-12 01:20:05 +01:00
|
|
|
let books = files.map(fileDetails => {
|
|
|
|
const bookData = JSON.parse(fs.readFileSync(path.resolve(app.fileLocation, fileDetails.name), 'utf8'));
|
2019-01-10 23:20:48 +01:00
|
|
|
if (bookData.hasOwnProperty('fileName')) return '';
|
|
|
|
bookData.author = bookData.author ? bookData.author : '<em>author not provided</em>';
|
|
|
|
bookData.contributor = bookData.contributor ? bookData.contributor : 'Anonymous';
|
|
|
|
|
2019-01-12 01:20:05 +01:00
|
|
|
const id = fileDetails.name.replace('.json', '');
|
2019-01-10 23:20:48 +01:00
|
|
|
const confirmId = 'confirm_' + id;
|
|
|
|
const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY');
|
2019-01-10 23:31:08 +01:00
|
|
|
const modal = app.templater.fill('./templates/elements/modalCard.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
id,
|
|
|
|
header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>',
|
2019-01-10 23:31:08 +01:00
|
|
|
content: app.templater.fill('./templates/elements/bookInfo.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
contributor: bookData.contributor,
|
|
|
|
fileFormat: bookData.fileType,
|
|
|
|
added,
|
|
|
|
summary: snarkdown(bookData.summary),
|
|
|
|
})
|
2019-01-10 23:31:08 +01:00
|
|
|
+ app.templater.fill('./templates/elements/modal.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
id: confirmId,
|
2019-01-10 23:31:08 +01:00
|
|
|
content: app.templater.fill('./templates/elements/messageBox.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
header: 'Download Your Book',
|
2019-01-10 23:31:08 +01:00
|
|
|
message: app.templater.fill('./templates/elements/takeConfirm.html', { id }),
|
2019-01-10 23:20:48 +01:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
footer: '<a class="button close">Close</a> <a class="button is-success modal-button" data-modal="' + confirmId + '">Take Book</a>',
|
|
|
|
});
|
2019-01-12 01:20:05 +01:00
|
|
|
const maxSize = settings.maxFileSize > 0 ? settings.maxFileSize : 10;
|
2019-01-10 23:31:08 +01:00
|
|
|
return app.templater.fill('./templates/elements/book.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
id,
|
|
|
|
title: bookData.title,
|
|
|
|
author: bookData.author,
|
2019-01-12 01:20:05 +01:00
|
|
|
thickness: fileDetails.size > (maxSize * 0.3) ? 'is-thick' : (fileDetails.size < (maxSize * 0.6) ? 'is-thin' : ''),
|
|
|
|
tallness: bookData.title.length > 15 ? 'is-tall' : (bookData.title.length < 8 ? 'is-short' : ''),
|
2019-01-10 23:20:48 +01:00
|
|
|
fileType: bookData.fileType,
|
|
|
|
modal,
|
|
|
|
});
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
if (books == '') {
|
|
|
|
books = '<div class="column"><div class="content">The shelf is empty. Would you like to <a href="/give">add a book</a>?</div></div>';
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:20:05 +01:00
|
|
|
const body = '<h2 class="title">Available Books</h2><div class="bookshelf columns is-gapless is-multiline">' + books + '</div>';
|
2019-01-10 23:31:08 +01:00
|
|
|
const html = app.templater.fill('./templates/htmlContainer.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
title: 'View',
|
|
|
|
resourcePath: (req.url.substr(-1) === '/' ? '../' : './'),
|
|
|
|
body
|
|
|
|
});
|
|
|
|
|
|
|
|
if (html) {
|
|
|
|
res.send(html);
|
|
|
|
} else {
|
|
|
|
res.send('Something went wrong!');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|