Updated utilsController.js

* Simplified some logics.

* Try to unlink thumbnail when failing to generate video thumbnail,
since ffmpeg may have already created an incomplete thumbnail.
This commit is contained in:
Bobby Wibowo 2019-07-21 05:15:59 +07:00
parent cf20bdbd1a
commit 83eb4872e9
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -165,17 +165,17 @@ utilsController.generateThumbs = (name, force) => {
if (!error && stats.isSymbolicLink()) { if (!error && stats.isSymbolicLink()) {
// Unlink symlink // Unlink symlink
const unlink = await new Promise((resolve, reject) => { const unlink = await new Promise(resolve => {
fs.unlink(thumbname, error => { fs.unlink(thumbname, error => {
if (error) return reject(error) if (error) console.error(error)
return resolve(true) resolve(!error)
}) })
}).catch(console.error) })
if (!unlink) return resolve(false) if (!unlink) return resolve(false)
} }
// Only make thumbnail if it does not exist (ENOENT) // Only make thumbnail if it does not exist (ENOENT)
if (!error && !stats.isSymbolicLink() && !force) return resolve(true) if (!error && !force) return resolve(true)
// Full path to input file // Full path to input file
const input = path.join(__dirname, '..', config.uploads.folder, name) const input = path.join(__dirname, '..', config.uploads.folder, name)
@ -235,7 +235,15 @@ utilsController.generateThumbs = (name, force) => {
'-vframes 1', '-vframes 1',
'-vf scale=200:200:force_original_aspect_ratio=decrease' '-vf scale=200:200:force_original_aspect_ratio=decrease'
]) ])
.on('error', reject) .on('error', error => {
// Attempt to unlink thumbnail
// Since ffmpeg may have already created an incomplete thumbnail
fs.unlink(thumbname, err => {
if (err && err.code !== 'ENOENT')
console.error(`${name}: ${err.toString()}`)
reject(error)
})
})
.on('end', () => resolve(true)) .on('end', () => resolve(true))
.run() .run()
}) })