From 535d3df73f6dd79f79f59ea83b116dca2dbfc0e1 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 27 Apr 2018 10:03:13 +0700 Subject: [PATCH] Updated some things involving album zipping * When version string is not specified in the URL query of /api/zip/:identifier (the API route to generate and download ZIP archive of the album), it will redirect the request with the album's editedAt timestamp as the value in the version string (only if editedAt timestamp exists). This will ensure that any bare calls to the API (without version string) will always be redirected to its latest state. This is mainly useful when being used with CDN that respects query string. * Download button and CDN notice will no longer be visible in empty album pages. * Matched empty album phrase as the one being used with the zipping API. --- controllers/albumsController.js | 8 ++++++-- views/album.njk | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 1c5bc7b..a74a323 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -191,10 +191,10 @@ albumsController.get = async (req, res, next) => { } albumsController.generateZip = async (req, res, next) => { + const versionString = parseInt(req.query.v) const download = (filePath, fileName) => { const headers = { 'Access-Control-Allow-Origin': '*' } - // Album page will append editedAt timestamp to the download link by default - if (parseInt(req.query.v) > 0) { + if (versionString > 0) { // Cache-Control header is useful when using CDN (max-age: 30 days) headers['Cache-Control'] = 'public, max-age=2592000, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800' } @@ -227,6 +227,10 @@ albumsController.generateZip = async (req, res, next) => { }) } + if ((!versionString || versionString <= 0) && album.editedAt) { + return res.redirect(`${album.identifier}?v=${album.editedAt}`) + } + if (album.zipGeneratedAt > album.editedAt) { const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`) const fileName = `${album.name}.zip` diff --git a/views/album.njk b/views/album.njk index 00ba3ca..4b79a58 100644 --- a/views/album.njk +++ b/views/album.njk @@ -41,7 +41,7 @@ - {% if enableDownload -%} + {% if enableDownload and files.length -%}

Download Album @@ -49,7 +49,7 @@

{%- endif %} - {% if enableDownload -%} + {% if enableDownload and files.length -%}
Album archives may be cached by CDN, if the one you downloaded seems outdated, you should try refreshing the page to get the latest version of the download link. @@ -72,7 +72,7 @@ {%- else -%}
- There is nothing here yet! + There are no files in the album.
{%- endif %}