Derps were made, and now fixed

This commit is contained in:
Pitu 2017-03-18 01:36:50 -03:00
parent de1af70b22
commit 026e0a3ef0
3 changed files with 48 additions and 41 deletions

View File

@ -2,6 +2,7 @@ const config = require('../config.js')
const db = require('knex')(config.database) const db = require('knex')(config.database)
const randomstring = require('randomstring') const randomstring = require('randomstring')
const utils = require('./utilsController.js') const utils = require('./utilsController.js')
const path = require('path')
let albumsController = {} let albumsController = {}
@ -57,7 +58,6 @@ albumsController.list = function(req, res, next) {
} }
albumsController.create = function(req, res, next) { albumsController.create = function(req, res, next) {
let token = req.headers.token let token = req.headers.token
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' }) 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) { for (let file of files) {
file.file = basedomain + '/' + file.name 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({ return res.json({

View File

@ -1,5 +1,5 @@
const path = require('path')
const config = require('../config.js') const config = require('../config.js')
const path = require('path')
const multer = require('multer') const multer = require('multer')
const randomstring = require('randomstring') const randomstring = require('randomstring')
const db = require('knex')(config.database) const db = require('knex')(config.database)
@ -151,7 +151,11 @@ uploadsController.processFilesForDisplay = function(req, res, files, existingFil
}) })
for (let file of files) { 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' }) }) }).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 !== '') if (file.userid !== undefined && file.userid !== null && file.userid !== '')
userids.push(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 // If we are a normal user, send response

View File

@ -5,6 +5,7 @@ const gm = require('gm')
const ffmpeg = require('fluent-ffmpeg') const ffmpeg = require('fluent-ffmpeg')
const utilsController = {} const utilsController = {}
utilsController.extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
utilsController.getPrettyDate = function(date) { utilsController.getPrettyDate = function(date) {
return date.getFullYear() + '-' return date.getFullYear() + '-'
@ -20,45 +21,38 @@ utilsController.getPrettyDate = function(date) {
utilsController.generateThumbs = function(file, basedomain) { utilsController.generateThumbs = function(file, basedomain) {
if (config.uploads.generateThumbnails !== true) return if (config.uploads.generateThumbnails !== true) return
const ext = path.extname(file.name).toLowerCase()
let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4'] let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png')
for (let ext of extensions) { fs.access(thumbname, (err) => {
if (path.extname(file.name).toLowerCase() === ext) { if (err && err.code === 'ENOENT') {
let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png') if (ext === '.webm' || ext === '.mp4') {
fs.access(thumbname, function(err) { ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name))
if (err && err.code === 'ENOENT') { .thumbnail({
// File doesnt exist timestamps: [0],
filename: '%b.png',
if (ext === '.webm' || ext === '.mp4') { folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'),
ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name)) size: '200x?'
.thumbnail({ })
timestamps: [0], .on('error', (error) => {
filename: '%b.png', console.log('Error - ', error.message)
folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'), })
size: '200x?' } else {
}) let size = {
.on('error', (error) => { width: 200,
console.log('Error - ', error.message) height: 200
})
} 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)
})
}
} }
}) 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 module.exports = utilsController