mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-31 07:11:33 +00:00
Updates
Updated thumbs.js script to allow purging CF's cache of succeeded files Updated utils.purgeCloudflareCache() function accordingly
This commit is contained in:
parent
8482049584
commit
e38037e660
@ -308,7 +308,7 @@ utilsController.bulkDeleteFiles = async (field, values, user, set) => {
|
|||||||
|
|
||||||
// Purge Cloudflare's cache if necessary
|
// Purge Cloudflare's cache if necessary
|
||||||
if (config.cloudflare.purgeCache)
|
if (config.cloudflare.purgeCache)
|
||||||
utilsController.purgeCloudflareCache(filtered.map(file => file.name), true)
|
utilsController.purgeCloudflareCache(filtered.map(file => file.name), true, true)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (!result.errors.length) return
|
if (!result.errors.length) return
|
||||||
result.errors.forEach(error => console.error(`CF: ${error}`))
|
result.errors.forEach(error => console.error(`CF: ${error}`))
|
||||||
@ -317,7 +317,7 @@ utilsController.bulkDeleteFiles = async (field, values, user, set) => {
|
|||||||
return failed
|
return failed
|
||||||
}
|
}
|
||||||
|
|
||||||
utilsController.purgeCloudflareCache = async (names, uploads) => {
|
utilsController.purgeCloudflareCache = async (names, uploads, thumbs) => {
|
||||||
if (!cloudflareAuth)
|
if (!cloudflareAuth)
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@ -328,20 +328,20 @@ utilsController.purgeCloudflareCache = async (names, uploads) => {
|
|||||||
let domain = config.domain
|
let domain = config.domain
|
||||||
if (!uploads) domain = config.homeDomain
|
if (!uploads) domain = config.homeDomain
|
||||||
|
|
||||||
const thumbs = []
|
const thumbNames = []
|
||||||
names = names.map(name => {
|
names = names.map(name => {
|
||||||
if (uploads) {
|
if (uploads) {
|
||||||
const url = `${domain}/${name}`
|
const url = `${domain}/${name}`
|
||||||
const extname = utilsController.extname(name)
|
const extname = utilsController.extname(name)
|
||||||
if (utilsController.mayGenerateThumb(extname))
|
if (thumbs && utilsController.mayGenerateThumb(extname))
|
||||||
thumbs.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`)
|
thumbNames.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`)
|
||||||
return url
|
return url
|
||||||
} else {
|
} else {
|
||||||
return name === 'home' ? domain : `${domain}/${name}`
|
return name === 'home' ? domain : `${domain}/${name}`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const files = names.concat(thumbs)
|
const files = names.concat(thumbNames)
|
||||||
let success = false
|
let success = false
|
||||||
let errors = []
|
let errors = []
|
||||||
try {
|
try {
|
||||||
|
@ -5,7 +5,9 @@ const utils = require('./../controllers/utilsController')
|
|||||||
|
|
||||||
const thumbs = {
|
const thumbs = {
|
||||||
mode: null,
|
mode: null,
|
||||||
force: null
|
force: null,
|
||||||
|
verbose: null,
|
||||||
|
cfcache: null
|
||||||
}
|
}
|
||||||
|
|
||||||
thumbs.mayGenerateThumb = extname => {
|
thumbs.mayGenerateThumb = extname => {
|
||||||
@ -39,6 +41,7 @@ thumbs.do = async () => {
|
|||||||
thumbs.mode = parseInt(args[0])
|
thumbs.mode = parseInt(args[0])
|
||||||
thumbs.force = parseInt(args[1] || 0)
|
thumbs.force = parseInt(args[1] || 0)
|
||||||
thumbs.verbose = parseInt(args[2] || 0)
|
thumbs.verbose = parseInt(args[2] || 0)
|
||||||
|
thumbs.cfcache = parseInt(args[3] || 0)
|
||||||
if (![1, 2, 3].includes(thumbs.mode) ||
|
if (![1, 2, 3].includes(thumbs.mode) ||
|
||||||
![0, 1].includes(thumbs.force) ||
|
![0, 1].includes(thumbs.force) ||
|
||||||
![0, 1].includes(thumbs.verbose) ||
|
![0, 1].includes(thumbs.verbose) ||
|
||||||
@ -47,11 +50,12 @@ thumbs.do = async () => {
|
|||||||
return console.log(stripIndents(`
|
return console.log(stripIndents(`
|
||||||
Generate thumbnails.
|
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
|
mode : 1 = images only, 2 = videos only, 3 = both images and videos
|
||||||
force : 0 = no force (default), 1 = overwrite existing thumbnails
|
force : 0 = no force (default), 1 = overwrite existing thumbnails
|
||||||
verbose: 0 = only print missing thumbs (default), 1 = print all
|
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')
|
const uploadsDir = path.join(__dirname, '..', 'uploads')
|
||||||
@ -64,7 +68,7 @@ thumbs.do = async () => {
|
|||||||
return _thumb.slice(0, -extname.length)
|
return _thumb.slice(0, -extname.length)
|
||||||
})
|
})
|
||||||
|
|
||||||
let success = 0
|
const succeeded = []
|
||||||
let error = 0
|
let error = 0
|
||||||
let skipped = 0
|
let skipped = 0
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
@ -84,13 +88,22 @@ thumbs.do = async () => {
|
|||||||
} else {
|
} else {
|
||||||
const generated = await utils.generateThumbs(_upload, thumbs.force)
|
const generated = await utils.generateThumbs(_upload, thumbs.force)
|
||||||
console.log(`${_upload}: ${generated ? 'OK' : 'ERROR'}`)
|
console.log(`${_upload}: ${generated ? 'OK' : 'ERROR'}`)
|
||||||
generated ? success++ : error++
|
generated ? succeeded.push(_upload) : error++
|
||||||
}
|
}
|
||||||
return generate(i + 1)
|
return generate(i + 1)
|
||||||
}
|
}
|
||||||
return generate(0)
|
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()
|
thumbs.do()
|
||||||
|
Loading…
Reference in New Issue
Block a user