Last active
June 2, 2020 16:14
-
-
Save Fuwn/8952fde31c0e36f06abd9e6999f4ceb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const config = require('../config.js'); | |
const fs = require('fs'); | |
const sharp = require('sharp'); | |
const ffmpeg = require('fluent-ffmpeg'); | |
const utilsController = {}; | |
utilsController.videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov']; | |
utilsController.generateThumbs = function(file, basedomain) { | |
if (config.uploads.generateThumbnails !== true) return; | |
const ext = path.extname(file).toLowerCase(); | |
// had to remove the .name property from everywhere it was called, cba to figure out why | |
let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.slice(0, -ext.length) + '.png'); | |
fs.access(thumbname, err => { | |
if (err && err.code === 'ENOENT') { | |
if (utilsController.videoExtensions.includes(ext)) { | |
ffmpeg(path.join(__dirname, '..', config.uploads.folder, file)) | |
.thumbnail({ | |
timestamps: [0], | |
filename: '%b.png', | |
folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'), | |
size: '200x?' | |
}) | |
.on('error', error => console.log('Error - ', error.message)); | |
} else { | |
let resizeOptions = { | |
width: 200, | |
height: 200, | |
fit: 'contain', | |
background: { | |
r: 0, | |
g: 0, | |
b: 0, | |
alpha: 0 | |
} | |
}; | |
sharp(path.join(__dirname, '..', config.uploads.folder, file)) | |
.resize(resizeOptions) | |
.toFile(thumbname) | |
.catch((error) => { | |
if (error) { | |
console.log('Error - ', error); | |
} | |
}); | |
} | |
} | |
}); | |
}; | |
fs.readdir(path.join(__dirname, '..', config.uploads.folder), (err, files) => { | |
if (err) console.log("Unable to get images: " + err); | |
files.forEach((file) => { | |
utilsController.generateThumbs(file); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment