Updated thumbs.js script to allow purging CF's cache of succeeded files

Updated utils.purgeCloudflareCache() function accordingly
This commit is contained in:
Bobby Wibowo 2019-01-18 10:40:15 +07:00
parent 8482049584
commit e38037e660
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 24 additions and 11 deletions

View File

@ -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 {

View File

@ -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} <mode=1|2|3> [force=0|1] [verbose=0|1]
Usage :\nnode ${location} <mode=1|2|3> [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()