From e38037e6603445221acbcb9ce087b97aec342e96 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 18 Jan 2019 10:40:15 +0700 Subject: [PATCH] Updates Updated thumbs.js script to allow purging CF's cache of succeeded files Updated utils.purgeCloudflareCache() function accordingly --- controllers/utilsController.js | 12 ++++++------ scripts/thumbs.js | 23 ++++++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 842ebf0..de013eb 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -308,7 +308,7 @@ utilsController.bulkDeleteFiles = async (field, values, user, set) => { // Purge Cloudflare's cache if necessary if (config.cloudflare.purgeCache) - utilsController.purgeCloudflareCache(filtered.map(file => file.name), true) + utilsController.purgeCloudflareCache(filtered.map(file => file.name), true, true) .then(result => { if (!result.errors.length) return result.errors.forEach(error => console.error(`CF: ${error}`)) @@ -317,7 +317,7 @@ utilsController.bulkDeleteFiles = async (field, values, user, set) => { return failed } -utilsController.purgeCloudflareCache = async (names, uploads) => { +utilsController.purgeCloudflareCache = async (names, uploads, thumbs) => { if (!cloudflareAuth) return { success: false, @@ -328,20 +328,20 @@ utilsController.purgeCloudflareCache = async (names, uploads) => { let domain = config.domain if (!uploads) domain = config.homeDomain - const thumbs = [] + const thumbNames = [] names = names.map(name => { if (uploads) { const url = `${domain}/${name}` const extname = utilsController.extname(name) - if (utilsController.mayGenerateThumb(extname)) - thumbs.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`) + if (thumbs && utilsController.mayGenerateThumb(extname)) + thumbNames.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`) return url } else { return name === 'home' ? domain : `${domain}/${name}` } }) - const files = names.concat(thumbs) + const files = names.concat(thumbNames) let success = false let errors = [] try { diff --git a/scripts/thumbs.js b/scripts/thumbs.js index 1e82583..a37fca2 100644 --- a/scripts/thumbs.js +++ b/scripts/thumbs.js @@ -5,7 +5,9 @@ const utils = require('./../controllers/utilsController') const thumbs = { mode: null, - force: null + force: null, + verbose: null, + cfcache: null } thumbs.mayGenerateThumb = extname => { @@ -39,6 +41,7 @@ thumbs.do = async () => { thumbs.mode = parseInt(args[0]) thumbs.force = parseInt(args[1] || 0) thumbs.verbose = parseInt(args[2] || 0) + thumbs.cfcache = parseInt(args[3] || 0) if (![1, 2, 3].includes(thumbs.mode) || ![0, 1].includes(thumbs.force) || ![0, 1].includes(thumbs.verbose) || @@ -47,11 +50,12 @@ thumbs.do = async () => { return console.log(stripIndents(` Generate thumbnails. - Usage :\nnode ${location} [force=0|1] [verbose=0|1] + Usage :\nnode ${location} [force=0|1] [verbose=0|1] [cfcache=0|1] mode : 1 = images only, 2 = videos only, 3 = both images and videos force : 0 = no force (default), 1 = overwrite existing thumbnails verbose: 0 = only print missing thumbs (default), 1 = print all + cfcache: 0 = do not clear cloudflare cache (default), 1 = clear cloudflare cache `)) const uploadsDir = path.join(__dirname, '..', 'uploads') @@ -64,7 +68,7 @@ thumbs.do = async () => { return _thumb.slice(0, -extname.length) }) - let success = 0 + const succeeded = [] let error = 0 let skipped = 0 await new Promise((resolve, reject) => { @@ -84,13 +88,22 @@ thumbs.do = async () => { } else { const generated = await utils.generateThumbs(_upload, thumbs.force) console.log(`${_upload}: ${generated ? 'OK' : 'ERROR'}`) - generated ? success++ : error++ + generated ? succeeded.push(_upload) : error++ } return generate(i + 1) } return generate(0) }) - console.log(`Success: ${success}\nError: ${error}\nSkipped: ${skipped}`) + console.log(`Success: ${succeeded.length}\nError: ${error}\nSkipped: ${skipped}`) + + if (thumbs.cfcache && succeeded.length) { + console.log('Purging Cloudflare\'s cache...') + const result = await utils.purgeCloudflareCache(succeeded.map(n => `thumbs/${n}`), true, false) + if (result.errors.length) + return result.errors.forEach(error => console.error(`CF: ${error}`)) + else + console.log(`Success: ${result.success}`) + } } thumbs.do()