2022-07-10 12:46:25 +00:00
|
|
|
const { Router } = require('hyper-express')
|
|
|
|
const routes = new Router()
|
2018-01-23 20:06:30 +00:00
|
|
|
const path = require('path')
|
2022-07-12 01:51:22 +00:00
|
|
|
const errors = require('./../controllers/errorsController')
|
2018-04-13 16:20:57 +00:00
|
|
|
const utils = require('./../controllers/utilsController')
|
2022-10-05 19:39:51 +00:00
|
|
|
const config = require('./../controllers/utils/ConfigManager')
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
routes.get('/a/:identifier', async (req, res) => {
|
2022-07-21 19:03:59 +00:00
|
|
|
const identifier = req.path_parameters && req.path_parameters.identifier
|
2020-11-02 11:39:28 +00:00
|
|
|
if (identifier === undefined) {
|
2022-07-11 23:21:21 +00:00
|
|
|
return errors.handleNotFound(req, res)
|
2020-11-02 11:39:28 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2022-06-03 21:21:56 +00:00
|
|
|
const album = await utils.db.table('albums')
|
2018-04-28 17:26:39 +00:00
|
|
|
.where({
|
|
|
|
identifier,
|
|
|
|
enabled: 1
|
|
|
|
})
|
2019-09-17 04:13:41 +00:00
|
|
|
.select('id', 'name', 'identifier', 'editedAt', 'download', 'public', 'description')
|
2018-04-26 18:33:11 +00:00
|
|
|
.first()
|
|
|
|
|
2020-11-02 11:39:28 +00:00
|
|
|
if (!album || album.public === 0) {
|
2022-07-11 23:21:21 +00:00
|
|
|
return errors.handleNotFound(req, res)
|
2020-11-02 11:39:28 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2022-07-21 19:02:59 +00:00
|
|
|
const nojs = req.query_parameters.nojs !== undefined
|
2019-09-17 04:13:41 +00:00
|
|
|
|
2020-04-18 19:52:11 +00:00
|
|
|
let cacheid
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
2022-07-06 10:51:34 +00:00
|
|
|
// Cache ID - we use a separate cache key for No-JS version
|
|
|
|
cacheid = `${album.id}${nojs ? '-nojs' : ''}`
|
2020-04-18 19:52:11 +00:00
|
|
|
|
2022-07-06 10:51:34 +00:00
|
|
|
const cache = utils.albumRenderStore.get(cacheid)
|
|
|
|
if (cache) {
|
2022-07-12 03:42:14 +00:00
|
|
|
return res.type('html').send(cache)
|
2022-07-06 10:51:34 +00:00
|
|
|
} else if (cache === null) {
|
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 03:44:24 +00:00
|
|
|
return res.render('album-notice', {
|
|
|
|
config,
|
2022-06-29 06:58:09 +00:00
|
|
|
utils,
|
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 03:44:24 +00:00
|
|
|
versions: utils.versionStrings,
|
|
|
|
album,
|
|
|
|
notice: 'This album\'s public page is still being generated. Please try again later.'
|
2020-04-18 19:52:11 +00:00
|
|
|
})
|
2020-11-02 11:39:28 +00:00
|
|
|
}
|
2020-04-18 19:52:11 +00:00
|
|
|
|
2022-07-06 10:51:34 +00:00
|
|
|
utils.albumRenderStore.hold(cacheid)
|
2020-04-18 19:52:11 +00:00
|
|
|
}
|
2019-09-17 04:13:41 +00:00
|
|
|
|
2022-06-03 21:21:56 +00:00
|
|
|
const files = await utils.db.table('files')
|
2022-09-19 00:13:09 +00:00
|
|
|
.select('name', 'size', 'timestamp')
|
2018-04-26 18:33:11 +00:00
|
|
|
.where('albumid', album.id)
|
2020-04-04 16:36:43 +00:00
|
|
|
.orderBy('id', 'desc')
|
2018-04-26 18:33:11 +00:00
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
album.thumb = ''
|
|
|
|
album.totalSize = 0
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const file of files) {
|
2019-09-17 04:13:41 +00:00
|
|
|
album.totalSize += parseInt(file.size)
|
|
|
|
|
|
|
|
file.extname = path.extname(file.name)
|
2018-05-12 14:01:14 +00:00
|
|
|
if (utils.mayGenerateThumb(file.extname)) {
|
2019-09-17 04:13:41 +00:00
|
|
|
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
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
album.downloadLink = album.download === 0
|
|
|
|
? null
|
|
|
|
: `api/album/zip/${album.identifier}?v=${album.editedAt}`
|
|
|
|
|
|
|
|
album.url = `a/${album.identifier}`
|
2022-05-05 06:23:18 +00:00
|
|
|
album.description = album.description
|
|
|
|
? utils.md.instance.render(album.description)
|
|
|
|
: null
|
2019-09-17 04:13:41 +00:00
|
|
|
|
2022-07-12 03:42:14 +00:00
|
|
|
// This will already end the Response,
|
|
|
|
// thus may only continue with tasks that will not interface with Response any further
|
|
|
|
const html = await res.render('album', {
|
2019-09-19 12:10:37 +00:00
|
|
|
config,
|
2022-06-29 06:58:09 +00:00
|
|
|
utils,
|
2019-09-19 12:10:37 +00:00
|
|
|
versions: utils.versionStrings,
|
|
|
|
album,
|
|
|
|
files,
|
|
|
|
nojs
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2022-07-12 03:42:14 +00:00
|
|
|
|
|
|
|
if (cacheid) {
|
|
|
|
// Only store rendered page if it did not error out and album actually have files
|
|
|
|
if (html && files.length) {
|
|
|
|
utils.albumRenderStore.set(cacheid, html)
|
|
|
|
} else {
|
|
|
|
utils.albumRenderStore.delete(cacheid)
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = routes
|