|
| 1 | +/* |
| 2 | + * Copyright 2016 John Coker for ThrustCurve.org |
| 3 | + * Licensed under the ISC License, https://opensource.org/licenses/ISC |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const path = require('path'), |
| 8 | + fs = require('fs'), |
| 9 | + express = require('express'), |
| 10 | + favicon = require('serve-favicon'), |
| 11 | + logger = require('morgan'), |
| 12 | + cookieParser = require('cookie-parser'), |
| 13 | + config = require('./config/server.js'), |
| 14 | + Pictures = require('./pictures.js'); |
| 15 | + |
| 16 | +const app = express(), |
| 17 | + router = express.Router(); |
| 18 | + |
| 19 | +app.use(logger('dev')); |
| 20 | +app.use(favicon(__dirname + '/public/favicon.ico')); |
| 21 | +app.use(express.static(path.join(__dirname, '/public'))); |
| 22 | +app.use(cookieParser()); |
| 23 | + |
| 24 | +// set up picture state |
| 25 | +const pictures = new Pictures(config); |
| 26 | +pictures.reload(); |
| 27 | + |
| 28 | +// main page is dynamic |
| 29 | +const manageRoot = path.join(__dirname + '/public/manage/'), |
| 30 | + manageHeader = fs.readFileSync(path.join(manageRoot, 'header.incl'), 'utf8'), |
| 31 | + manageFooter = fs.readFileSync(path.join(manageRoot, 'footer.incl'), 'utf8'); |
| 32 | + |
| 33 | +function formatCaption(picture) { |
| 34 | + let s = encodeHTML(picture.file) + ', '; |
| 35 | + |
| 36 | + if (picture == pictures.current) |
| 37 | + s += 'showing now'; |
| 38 | + else if (!picture.lastShown) |
| 39 | + s += 'never shown'; |
| 40 | + else { |
| 41 | + let midnight = new Date(); |
| 42 | + midnight.setHours(0, 0, 0, 0); |
| 43 | + let ago = 0; |
| 44 | + while (picture.lastShown < midnight) { |
| 45 | + midnight.setDate(midnight.getDate(-1)); |
| 46 | + ago++; |
| 47 | + } |
| 48 | + |
| 49 | + if (ago < 1) |
| 50 | + s += 'shown today'; |
| 51 | + else if (ago == 1) |
| 52 | + s += 'shown yesterday'; |
| 53 | + else |
| 54 | + s += `shown ${ago}d ago`; |
| 55 | + } |
| 56 | + |
| 57 | + return s; |
| 58 | +} |
| 59 | + |
| 60 | +router.get(['/', '/index.html', '/manage', '/manage.html'], function(req, res, next) { |
| 61 | + |
| 62 | + var feedback = []; |
| 63 | + |
| 64 | + // change the current picture |
| 65 | + if (req.query.hasOwnProperty('switch') && pictures.switch(req.query.switch)) { |
| 66 | + let encoded = encodeHTML(pictures.current.file); |
| 67 | + feedback.push(`Now showing ${encoded}.`); |
| 68 | + } |
| 69 | + |
| 70 | + // reset the sequence |
| 71 | + if (req.query.hasOwnProperty('reset')) { |
| 72 | + feedback.push('Picture sequence reset.'); |
| 73 | + } |
| 74 | + |
| 75 | + // resume normal sequence |
| 76 | + if (req.query.hasOwnProperty('resume')) { |
| 77 | + |
| 78 | + if (pictures.current) { |
| 79 | + let encoded = encodeHTML(pictures.current.file); |
| 80 | + feedback.push('Resumed sequence with ${encoded}.'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // collect the sort order |
| 85 | + let sort, setSort = false; |
| 86 | + if (req.query.hasOwnProperty('sort') && |
| 87 | + /^(name|shown|updated)$/.test(req.query.sort.toLowerCase())) { |
| 88 | + sort = req.query.sort; |
| 89 | + res.append('Set-Cookie', 'sort=' + sort); |
| 90 | + } else { |
| 91 | + sort = req.cookies.sort; |
| 92 | + } |
| 93 | + |
| 94 | + // generate the page |
| 95 | + var parts = []; |
| 96 | + parts.push(manageHeader); |
| 97 | + |
| 98 | + if (feedback.length > 0) { |
| 99 | + parts.push(' <div class="container">\n'); |
| 100 | + for (let i = 0; i < feedback.length; i++) { |
| 101 | + parts.push(` <div class="alert alert-info" role="alert"> |
| 102 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> |
| 103 | + ${feedback[i]} |
| 104 | + </div> |
| 105 | +`); |
| 106 | + } |
| 107 | + parts.push('</div>\n'); |
| 108 | + } |
| 109 | + |
| 110 | + parts.push(' <div class="container">\n'); |
| 111 | + |
| 112 | + if (pictures.current) { |
| 113 | + let caption = formatCaption(pictures.current); |
| 114 | + parts.push(` <div class="row"> |
| 115 | + <div class="col-sm-12 picture current"> |
| 116 | + <img src="/picture/current" /> |
| 117 | + <p class="caption">${caption}</p> |
| 118 | + </div> |
| 119 | + </div>\n`); |
| 120 | + } |
| 121 | + |
| 122 | + if (pictures.length > 1) { |
| 123 | + parts.push(' <div class="row">\n'); |
| 124 | + let col = 0; |
| 125 | + let sorted = pictures.sorted(sort); |
| 126 | + for (let i = 0; i < sorted.length; i++) { |
| 127 | + if (sorted[i] == pictures.current) |
| 128 | + continue; |
| 129 | + |
| 130 | + if (col >= 12) { |
| 131 | + parts.push(' </div>\n'); |
| 132 | + parts.push(' <div class="row">\n'); |
| 133 | + col = 0; |
| 134 | + } |
| 135 | + parts.push(' <div class="col-sm-3 picture">\n'); |
| 136 | + let encoded = encodeURIComponent(sorted[i].file); |
| 137 | + parts.push(` <a href="?switch=${encoded}"><img src="/thumbnail/${encoded}" /></a>\n`); |
| 138 | + let caption = formatCaption(sorted[i]); |
| 139 | + parts.push(` <p class="caption">${caption}</p>\n`); |
| 140 | + parts.push(' </div>\n'); |
| 141 | + col += 3; |
| 142 | + } |
| 143 | + if (col < 12) |
| 144 | + parts.push(` <div class="col-sm-${12 - col}"> </div>\n`); |
| 145 | + parts.push(' </div>\n'); |
| 146 | + } |
| 147 | + |
| 148 | + parts.push(' </div>\n'); |
| 149 | + parts.push(manageFooter); |
| 150 | + |
| 151 | + res.status(200) |
| 152 | + .type('html'); |
| 153 | + for (let i = 0; i < parts.length; i++) |
| 154 | + res.write(parts[i]); |
| 155 | + res.end(); |
| 156 | +}); |
| 157 | + |
| 158 | +router.get(['/picture/current', '/thumbnail/current'], function(req, res, next) { |
| 159 | + if (pictures.current) { |
| 160 | + res.sendFile(pictures.current.path, { |
| 161 | + lastModified: false, |
| 162 | + cacheControl: false, |
| 163 | + headers: { |
| 164 | + 'Last-Modified': pictures.current.lastShown.toUTCString(), |
| 165 | + 'Cache-Control': 'must-revalidate', |
| 166 | + 'X-Current-Picture': pictures.current.file |
| 167 | + } |
| 168 | + }); |
| 169 | + } else |
| 170 | + res.status(404).send(); |
| 171 | +}); |
| 172 | + |
| 173 | +router.get(['/picture/:file', '/thumbnail/:file'], function(req, res, next) { |
| 174 | + let found; |
| 175 | + if (req.params.file != null && req.params.file !== '') |
| 176 | + found = pictures.byFile(req.params.file); |
| 177 | + if (found) |
| 178 | + res.sendFile(found.path); |
| 179 | + else |
| 180 | + res.status(404).send(); |
| 181 | +}); |
| 182 | + |
| 183 | +app.use('/', router); |
| 184 | + |
| 185 | +// handle other routes as 404 |
| 186 | +app.use(function(req, res, next) { |
| 187 | + res.status(404).send(); |
| 188 | +}); |
| 189 | + |
| 190 | +// handle internal errors |
| 191 | +app.use(function(err, req, res, next) { |
| 192 | + let status = err.status || 500; |
| 193 | + let message = encodeHTML(err.message) || 'Unknown error'; |
| 194 | + let body = `<!DOCTYPE html> |
| 195 | +<html> |
| 196 | +<head> |
| 197 | + <title>Server Error</title> |
| 198 | +</head> |
| 199 | +<body> |
| 200 | + <h1>Server Error</h1> |
| 201 | + <ul> |
| 202 | + <li>URL: ${req.url}</li> |
| 203 | + <li>status: ${status}</li> |
| 204 | + <li>message: ${message}</li> |
| 205 | + </ul> |
| 206 | +`; |
| 207 | + |
| 208 | + if (err && err.stack) |
| 209 | + body += ' <pre>\n' + encodeHTML(err.stack) + '\n</pre>\n'; |
| 210 | + |
| 211 | + body += ` |
| 212 | +</body> |
| 213 | +</html>`; |
| 214 | + |
| 215 | + res.status(status).send(body); |
| 216 | +}); |
| 217 | + |
| 218 | +function encodeHTML(s) { |
| 219 | + if (s == null || s === '') |
| 220 | + return ''; |
| 221 | + return s.replace(/&/g, '&') |
| 222 | + .replace(/</g, '<') |
| 223 | + .replace(/>/g, '>'); |
| 224 | +} |
| 225 | + |
| 226 | +module.exports = app; |
0 commit comments