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 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({

View File

@ -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

View File

@ -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