From 7587bfa408b5d33d34b4a8b765cd4aed83638c81 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Wed, 5 Sep 2018 00:29:53 +0700 Subject: [PATCH] Updates * Updated utilsController.js: Unlink thumb of type symlink whenever generateThumbs() is called. * Updated thumbs.js: Add stats (success/error/skipped). --- controllers/utilsController.js | 15 +++++++++++++-- scripts/thumbs.js | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 3ad0ee3..aa22c61 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -68,14 +68,25 @@ utilsController.generateThumbs = (name, force) => { return new Promise(resolve => { const extname = pce(name).toLowerCase() const thumbname = path.join(thumbsDir, name.slice(0, -extname.length) + '.png') - fs.access(thumbname, error => { + fs.lstat(thumbname, async (error, stats) => { if (error && error.code !== 'ENOENT') { console.error(error) return resolve(false) } + if (!error && stats.isSymbolicLink()) { + // Unlink symlink + const unlink = await new Promise((resolve, reject) => { + fs.unlink(thumbname, error => { + if (error) { return reject(error) } + return resolve(true) + }) + }).catch(console.error) + if (!unlink) { return resolve(false) } + } + // Only make thumbnail if it does not exist (ENOENT) - if (!error && !force) { return resolve(true) } + if (!error && !stats.isSymbolicLink() && !force) { return resolve(true) } // If image extension if (utilsController.imageExtensions.includes(extname)) { diff --git a/scripts/thumbs.js b/scripts/thumbs.js index 9bb302d..343086b 100644 --- a/scripts/thumbs.js +++ b/scripts/thumbs.js @@ -19,9 +19,9 @@ thumbs.getFiles = directory => { const files = [] await Promise.all(names.map(name => { return new Promise((resolve, reject) => { - fs.lstat(path.join(directory, name), (error, stat) => { + fs.lstat(path.join(directory, name), (error, stats) => { if (error) { return reject(error) } - if (stat.isFile() && !name.startsWith('.')) { files.push(name) } + if (stats.isFile() && !name.startsWith('.')) { files.push(name) } resolve() }) }) @@ -56,6 +56,9 @@ thumbs.do = async () => { return _thumb.slice(0, -extname.length) }) + let success = 0 + let error = 0 + let skipped = 0 await new Promise((resolve, reject) => { const generate = async i => { const _upload = _uploads[i] @@ -66,16 +69,20 @@ thumbs.do = async () => { if (_thumbs.includes(basename) && !thumbs.force) { if (thumbs.verbose) { console.log(`${_upload}: thumb exists.`) } + skipped++ } else if (!thumbs.mayGenerateThumb(extname)) { if (thumbs.verbose) { console.log(`${_upload}: extension skipped.`) } + skipped++ } else { const generated = await utils.generateThumbs(_upload, thumbs.force) - console.log(`${_upload}: ${String(generated)}`) + console.log(`${_upload}: ${generated ? 'OK' : 'ERROR'}`) + generated ? success++ : error++ } - generate(i + 1) + return generate(i + 1) } - generate(0) + return generate(0) }) + console.log(`Success: ${success}\nError: ${error}\nSkipped: ${skipped}`) } thumbs.do()