From 026e0a3ef04efd97fda225391d6a72b7567f755d Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Mar 2017 01:36:50 -0300 Subject: [PATCH] Derps were made, and now fixed --- controllers/albumsController.js | 9 ++++- controllers/uploadController.js | 14 +++++-- controllers/utilsController.js | 66 +++++++++++++++------------------ 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 038d605..3a51e15 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -2,6 +2,7 @@ const config = require('../config.js') const db = require('knex')(config.database) const randomstring = require('randomstring') const utils = require('./utilsController.js') +const path = require('path') let albumsController = {} @@ -57,7 +58,6 @@ albumsController.list = function(req, res, next) { } albumsController.create = function(req, res, next) { - let token = req.headers.token if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' }) @@ -153,7 +153,12 @@ albumsController.get = function(req, res, next) { for (let file of files) { file.file = basedomain + '/' + file.name - utils.generateThumbs(file) + + let ext = path.extname(file.name).toLowerCase() + if (utils.extensions.includes(ext)) { + file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png' + utils.generateThumbs(file) + } } return res.json({ diff --git a/controllers/uploadController.js b/controllers/uploadController.js index acbf440..39a9364 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -1,5 +1,5 @@ -const path = require('path') const config = require('../config.js') +const path = require('path') const multer = require('multer') const randomstring = require('randomstring') const db = require('knex')(config.database) @@ -151,7 +151,11 @@ uploadsController.processFilesForDisplay = function(req, res, files, existingFil }) for (let file of files) { - utils.generateThumbs(file) + let ext = path.extname(file.name).toLowerCase() + if (utils.extensions.includes(ext)) { + file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png' + utils.generateThumbs(file) + } } }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) }) @@ -260,7 +264,11 @@ uploadsController.list = function(req, res) { if (file.userid !== undefined && file.userid !== null && file.userid !== '') userids.push(file.userid) - utils.generateThumbs(file) + let ext = path.extname(file.name).toLowerCase() + if (utils.extensions.includes(ext)) { + file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png' + utils.generateThumbs(file) + } } // If we are a normal user, send response diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 1ddec34..5ddec82 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -5,6 +5,7 @@ const gm = require('gm') const ffmpeg = require('fluent-ffmpeg') const utilsController = {} +utilsController.extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4'] utilsController.getPrettyDate = function(date) { return date.getFullYear() + '-' @@ -20,45 +21,38 @@ utilsController.getPrettyDate = function(date) { utilsController.generateThumbs = function(file, basedomain) { if (config.uploads.generateThumbnails !== true) return + const ext = path.extname(file.name).toLowerCase() - let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4'] - for (let ext of extensions) { - if (path.extname(file.name).toLowerCase() === ext) { - let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png') - fs.access(thumbname, function(err) { - if (err && err.code === 'ENOENT') { - // File doesnt exist - - if (ext === '.webm' || ext === '.mp4') { - ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name)) - .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 size = { - width: 200, - height: 200 - } - - gm(path.join(__dirname, '..', config.uploads.folder, file.name)) - .resize(size.width, size.height + '>') - .gravity('Center') - .extent(size.width, size.height) - .background('transparent') - .write(thumbname, (error) => { - if (error) console.log('Error - ', error) - }) - } + let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png') + fs.access(thumbname, (err) => { + if (err && err.code === 'ENOENT') { + if (ext === '.webm' || ext === '.mp4') { + ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name)) + .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 size = { + width: 200, + height: 200 } - }) + gm(path.join(__dirname, '..', config.uploads.folder, file.name)) + .resize(size.width, size.height + '>') + .gravity('Center') + .extent(size.width, size.height) + .background('transparent') + .write(thumbname, (error) => { + if (error) console.log('Error - ', error) + }) + } } - } + }) } module.exports = utilsController