filesafe/routes/album.js
Bobby Wibowo 8b4b0e79c5
Improved albums public page cache and more
Removed its dependency towards albums' editedAt property.
Editing album's metas (name, description, etc) will no longer update
its editedAt property.
Instead it will now ONLY be updated when adding/removing files to/from
it. Just like how it was meant to be, which was to be used to check
whether it's necessary to re-generate their downloadable ZIPs.

Albums public page cache will still be properly invalidated when
adding/removing files to/from it, as well as after editing their metas.

Added views/album-notice.njk to be used to render okay-ish notice when
an album's public page is still being generated.
I was originally thinking of using it for disabled albums as well, but
I refrained from it to reduce the possibility of disabled album IDs from
being easily scanned (as it just returns 404 now).

Removed invalidatedAt property from stats cache. Instead their caches
will immediately be nullified as they should (thus frees up memory
slightly as well).

Stats cache for albums will now only be cleared when truly necessary.
As in, adding/removing files to/from albums will no longer clear them.

Updated Nunjucks files to properly use h1, h2, h3 tags in actual
hierarchical orders.
Elements that don't need to use hX tags will now use P instead.
Nothing changes visually, only structurally.

Fixed some elements in Nunjucks using single quotes instead of
double quotes. They'd have worked the same, but consistency.

Added h1 title in FAQ page.

Make text for no JS warning a bit bigger, and improved the phrasing
a little bit.
2020-06-03 10:44:24 +07:00

96 lines
2.8 KiB
JavaScript

const routes = require('express').Router()
const path = require('path')
const paths = require('./../controllers/pathsController')
const utils = require('./../controllers/utilsController')
const config = require('./../config')
const db = require('knex')(config.database)
routes.get('/a/:identifier', async (req, res, next) => {
const identifier = req.params.identifier
if (identifier === undefined)
res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404]))
const album = await db.table('albums')
.where({
identifier,
enabled: 1
})
.select('id', 'name', 'identifier', 'editedAt', 'download', 'public', 'description')
.first()
if (!album || album.public === 0)
return res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404]))
const nojs = req.query.nojs !== undefined
let cacheid
if (process.env.NODE_ENV !== 'development') {
// Cache ID - we initialize a separate cache for No-JS version
cacheid = nojs ? `${album.id}-nojs` : album.id
if (!utils.albumsCache[cacheid])
utils.albumsCache[cacheid] = {
cache: null,
generating: false
}
if (!utils.albumsCache[cacheid].cache && utils.albumsCache[cacheid].generating)
return res.render('album-notice', {
config,
versions: utils.versionStrings,
album,
notice: 'This album\'s public page is still being generated. Please try again later.'
})
else if (utils.albumsCache[cacheid].cache)
return res.send(utils.albumsCache[cacheid].cache)
utils.albumsCache[cacheid].generating = true
}
const files = await db.table('files')
.select('name', 'size')
.where('albumid', album.id)
.orderBy('id', 'desc')
album.thumb = ''
album.totalSize = 0
for (const file of files) {
album.totalSize += parseInt(file.size)
file.extname = path.extname(file.name)
if (utils.mayGenerateThumb(file.extname)) {
file.thumb = `thumbs/${file.name.slice(0, -file.extname.length)}.png`
// If thumbnail for album is still not set, set it to current file's full URL.
// A potential improvement would be to let the user set a specific image as an album cover.
if (!album.thumb) album.thumb = file.name
}
}
album.downloadLink = album.download === 0
? null
: `api/album/zip/${album.identifier}?v=${album.editedAt}`
album.url = `a/${album.identifier}`
return res.render('album', {
config,
versions: utils.versionStrings,
album,
files,
nojs
}, (error, html) => {
const data = error ? null : html
if (cacheid) {
utils.albumsCache[cacheid].cache = data
utils.albumsCache[cacheid].generating = false
}
// Express should already send error to the next handler
if (error) return
return res.send(data)
})
})
module.exports = routes