2018-01-23 20:06:30 +00:00
|
|
|
const path = require('path')
|
|
|
|
const config = require('../config.js')
|
|
|
|
const fs = require('fs')
|
|
|
|
const gm = require('gm')
|
|
|
|
const ffmpeg = require('fluent-ffmpeg')
|
|
|
|
const db = require('knex')(config.database)
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const utilsController = {}
|
|
|
|
utilsController.imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png']
|
|
|
|
utilsController.videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov']
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
utilsController.getPrettyDate = function (date) {
|
|
|
|
return date.getFullYear() + '-' +
|
|
|
|
(date.getMonth() + 1) + '-' +
|
|
|
|
date.getDate() + ' ' +
|
|
|
|
(date.getHours() < 10 ? '0' : '') +
|
|
|
|
date.getHours() + ':' +
|
|
|
|
(date.getMinutes() < 10 ? '0' : '') +
|
|
|
|
date.getMinutes() + ':' +
|
|
|
|
(date.getSeconds() < 10 ? '0' : '') +
|
|
|
|
date.getSeconds()
|
2017-03-17 04:14:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
utilsController.authorize = async (req, res) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const token = req.headers.token
|
|
|
|
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await db.table('users').where('token', token).first()
|
|
|
|
if (!user) return res.status(401).json({ success: false, description: 'Invalid token' })
|
|
|
|
return user
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
utilsController.generateThumbs = function (file, basedomain) {
|
|
|
|
const ext = path.extname(file.name).toLowerCase()
|
2018-02-07 06:22:31 +00:00
|
|
|
const isVideoExt = utilsController.videoExtensions.includes(ext)
|
|
|
|
const isImageExt = utilsController.imageExtensions.includes(ext)
|
|
|
|
|
|
|
|
if (!isVideoExt && !isImageExt) return
|
|
|
|
if (isVideoExt && config.uploads.generateVideoThumbnails !== true) return
|
|
|
|
if (isImageExt && config.uploads.generateImageThumbnails !== true) return
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
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') {
|
2018-02-07 06:22:31 +00:00
|
|
|
if (isVideoExt) {
|
2018-01-23 20:06:30 +00:00
|
|
|
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))
|
2018-02-07 06:22:31 +00:00
|
|
|
} else if (isImageExt) {
|
2018-01-23 20:06:30 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = utilsController
|