2019-01-06 06:27:17 +00:00
|
|
|
const { stripIndents } = require('./_utils')
|
2018-05-12 14:01:14 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const utils = require('./../controllers/utilsController')
|
|
|
|
|
|
|
|
const thumbs = {
|
|
|
|
mode: null,
|
2019-01-18 03:40:15 +00:00
|
|
|
force: null,
|
|
|
|
verbose: null,
|
|
|
|
cfcache: null
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
thumbs.mayGenerateThumb = extname => {
|
|
|
|
return ([1, 3].includes(thumbs.mode) && utils.imageExtensions.includes(extname)) ||
|
|
|
|
([2, 3].includes(thumbs.mode) && utils.videoExtensions.includes(extname))
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbs.getFiles = directory => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readdir(directory, async (error, names) => {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (error) return reject(error)
|
2018-05-12 14:01:14 +00:00
|
|
|
const files = []
|
|
|
|
await Promise.all(names.map(name => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-09-04 17:29:53 +00:00
|
|
|
fs.lstat(path.join(directory, name), (error, stats) => {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (error) return reject(error)
|
|
|
|
if (stats.isFile() && !name.startsWith('.')) files.push(name)
|
2018-05-12 14:01:14 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
resolve(files)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbs.do = async () => {
|
2019-01-06 06:27:17 +00:00
|
|
|
const location = process.argv[1].replace(process.cwd() + '/', '')
|
2018-05-12 14:01:14 +00:00
|
|
|
const args = process.argv.slice(2)
|
|
|
|
|
|
|
|
thumbs.mode = parseInt(args[0])
|
2019-01-06 06:27:17 +00:00
|
|
|
thumbs.force = parseInt(args[1] || 0)
|
|
|
|
thumbs.verbose = parseInt(args[2] || 0)
|
2019-01-18 03:40:15 +00:00
|
|
|
thumbs.cfcache = parseInt(args[3] || 0)
|
2019-01-06 06:27:17 +00:00
|
|
|
if (![1, 2, 3].includes(thumbs.mode) ||
|
|
|
|
![0, 1].includes(thumbs.force) ||
|
|
|
|
![0, 1].includes(thumbs.verbose) ||
|
|
|
|
args.includes('--help') ||
|
|
|
|
args.includes('-h'))
|
|
|
|
return console.log(stripIndents(`
|
|
|
|
Generate thumbnails.
|
|
|
|
|
2019-01-18 03:40:15 +00:00
|
|
|
Usage :\nnode ${location} <mode=1|2|3> [force=0|1] [verbose=0|1] [cfcache=0|1]
|
2019-01-06 06:27:17 +00:00
|
|
|
|
|
|
|
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
|
2019-01-18 03:40:15 +00:00
|
|
|
cfcache: 0 = do not clear cloudflare cache (default), 1 = clear cloudflare cache
|
2019-01-06 06:27:17 +00:00
|
|
|
`))
|
2018-05-12 14:01:14 +00:00
|
|
|
|
|
|
|
const uploadsDir = path.join(__dirname, '..', 'uploads')
|
|
|
|
const thumbsDir = path.join(uploadsDir, 'thumbs')
|
|
|
|
const _uploads = await thumbs.getFiles(uploadsDir)
|
|
|
|
|
|
|
|
let _thumbs = await thumbs.getFiles(thumbsDir)
|
|
|
|
_thumbs = _thumbs.map(_thumb => {
|
|
|
|
const extname = path.extname(_thumb)
|
|
|
|
return _thumb.slice(0, -extname.length)
|
|
|
|
})
|
|
|
|
|
2019-01-18 03:40:15 +00:00
|
|
|
const succeeded = []
|
2018-09-04 17:29:53 +00:00
|
|
|
let error = 0
|
|
|
|
let skipped = 0
|
2018-05-12 14:01:14 +00:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const generate = async i => {
|
|
|
|
const _upload = _uploads[i]
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!_upload) return resolve()
|
2018-05-12 14:01:14 +00:00
|
|
|
|
|
|
|
const extname = path.extname(_upload)
|
|
|
|
const basename = _upload.slice(0, -extname.length)
|
|
|
|
|
|
|
|
if (_thumbs.includes(basename) && !thumbs.force) {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (thumbs.verbose) console.log(`${_upload}: thumb exists.`)
|
2018-09-04 17:29:53 +00:00
|
|
|
skipped++
|
2018-05-12 14:01:14 +00:00
|
|
|
} else if (!thumbs.mayGenerateThumb(extname)) {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (thumbs.verbose) console.log(`${_upload}: extension skipped.`)
|
2018-09-04 17:29:53 +00:00
|
|
|
skipped++
|
2018-05-12 14:01:14 +00:00
|
|
|
} else {
|
2019-01-29 19:50:45 +00:00
|
|
|
const start = Date.now()
|
2018-05-12 14:01:14 +00:00
|
|
|
const generated = await utils.generateThumbs(_upload, thumbs.force)
|
2019-01-29 19:50:45 +00:00
|
|
|
console.log(`${_upload}: ${(Date.now() - start) / 1000}s: ${generated ? 'OK' : 'ERROR'}`)
|
2019-01-18 03:40:15 +00:00
|
|
|
generated ? succeeded.push(_upload) : error++
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
2018-09-04 17:29:53 +00:00
|
|
|
return generate(i + 1)
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
2018-09-04 17:29:53 +00:00
|
|
|
return generate(0)
|
2018-05-12 14:01:14 +00:00
|
|
|
})
|
2019-01-18 03:40:15 +00:00
|
|
|
console.log(`Success: ${succeeded.length}\nError: ${error}\nSkipped: ${skipped}`)
|
|
|
|
|
|
|
|
if (thumbs.cfcache && succeeded.length) {
|
|
|
|
console.log('Purging Cloudflare\'s cache...')
|
2019-01-31 09:49:14 +00:00
|
|
|
const results = await utils.purgeCloudflareCache(succeeded.map(name => {
|
|
|
|
const extname = utils.extname(name)
|
|
|
|
return `thumbs/${name.slice(0, -extname.length)}.png`
|
|
|
|
}), true, false)
|
2019-01-31 09:29:34 +00:00
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
if (results[i].errors.length)
|
|
|
|
results[i].errors.forEach(error => console.error(`CF: ${error}`))
|
|
|
|
console.log(`Status [${i}]: ${results[i].success ? 'OK' : 'ERROR'}`)
|
|
|
|
}
|
2019-01-18 03:40:15 +00:00
|
|
|
}
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
thumbs.do()
|