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-03-24 16:45:51 +00:00
|
|
|
const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const utilsController = {}
|
2018-03-28 11:36:28 +00:00
|
|
|
utilsController.imageExtensions = ['.webp', '.jpg', '.jpeg', '.bmp', '.gif', '.png']
|
|
|
|
utilsController.videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov', '.mkv']
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
utilsController.getPrettyDate = date => {
|
2018-01-23 20:06:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
utilsController.getPrettyBytes = num => {
|
2018-03-24 16:45:51 +00:00
|
|
|
// MIT License
|
|
|
|
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!Number.isFinite(num)) { return num }
|
2018-03-24 16:45:51 +00:00
|
|
|
|
|
|
|
const neg = num < 0
|
2018-03-28 17:40:50 +00:00
|
|
|
if (neg) { num = -num }
|
|
|
|
if (num < 1) { return (neg ? '-' : '') + num + ' B' }
|
2018-03-24 16:45:51 +00:00
|
|
|
|
|
|
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), units.length - 1)
|
|
|
|
const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3))
|
|
|
|
const unit = units[exponent]
|
|
|
|
|
|
|
|
return (neg ? '-' : '') + numStr + ' ' + unit
|
|
|
|
}
|
|
|
|
|
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
|
2018-03-24 19:47:41 +00:00
|
|
|
if (token === undefined) {
|
|
|
|
res.status(401).json({ success: false, description: 'No token provided.' })
|
|
|
|
return
|
|
|
|
}
|
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()
|
2018-03-28 17:40:50 +00:00
|
|
|
if (user) { return user }
|
2018-03-24 19:47:41 +00:00
|
|
|
res.status(401).json({ success: false, description: 'Invalid token.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
utilsController.generateThumbs = (file, basedomain) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
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)
|
|
|
|
|
2018-04-04 17:38:15 +00:00
|
|
|
if ((!isVideoExt && !isImageExt) ||
|
|
|
|
(isVideoExt && config.uploads.generateThumbnails.video !== true) ||
|
|
|
|
(isImageExt && config.uploads.generateThumbnails.image !== true)) {
|
|
|
|
return
|
|
|
|
}
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
const thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png')
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.access(thumbname, error => {
|
|
|
|
if (error && error.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({
|
2018-03-28 11:36:28 +00:00
|
|
|
timestamps: ['1%'],
|
2018-01-23 20:06:30 +00:00
|
|
|
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-04-05 10:52:57 +00:00
|
|
|
const size = {
|
2018-01-23 20:06:30 +00:00
|
|
|
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 => {
|
2018-03-28 17:40:50 +00:00
|
|
|
if (error) { console.log('Error - ', error) }
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
utilsController.deleteFile = async file => {
|
|
|
|
const ext = path.extname(file).toLowerCase()
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.stat(path.join(__dirname, '..', config.uploads.folder, file), (error, stats) => {
|
|
|
|
if (error) { return reject(error) }
|
|
|
|
fs.unlink(path.join(__dirname, '..', config.uploads.folder, file), error => {
|
|
|
|
if (error) { return reject(error) }
|
|
|
|
if (!utilsController.imageExtensions.includes(ext) && !utilsController.videoExtensions.includes(ext)) {
|
|
|
|
return resolve()
|
|
|
|
}
|
|
|
|
file = file.substr(0, file.lastIndexOf('.')) + '.png'
|
|
|
|
fs.stat(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), (error, stats) => {
|
|
|
|
if (error) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
console.log(error)
|
|
|
|
}
|
|
|
|
return resolve()
|
|
|
|
}
|
|
|
|
fs.unlink(path.join(__dirname, '..', config.uploads.folder, 'thumbs/', file), error => {
|
|
|
|
if (error) { return reject(error) }
|
|
|
|
return resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will return an array of IDs that could not be deleted
|
|
|
|
utilsController.bulkDeleteFilesByIds = async (ids, user) => {
|
|
|
|
if (!user) { return }
|
|
|
|
const files = await db.table('files')
|
|
|
|
.whereIn('id', ids)
|
|
|
|
.where(function () {
|
|
|
|
if (user.username !== 'root') {
|
|
|
|
this.where('userid', user.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
const failedids = ids.filter(id => !files.find(file => file.id === id))
|
2018-03-30 02:39:53 +00:00
|
|
|
|
|
|
|
// First, we delete all the physical files
|
|
|
|
await Promise.all(files.map(file => {
|
|
|
|
return utilsController.deleteFile(file.name).catch(error => {
|
|
|
|
// ENOENT is missing file, for whatever reason, then just delete from db anyways
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
console.log(error)
|
2018-04-05 12:54:24 +00:00
|
|
|
failedids.push(file.id)
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Second, we filter out failed IDs
|
2018-04-05 12:54:24 +00:00
|
|
|
const albumids = []
|
|
|
|
const updateDbIds = files.filter(file => !failedids.includes(file.id))
|
2018-04-03 15:01:19 +00:00
|
|
|
await Promise.all(updateDbIds.map(file => {
|
2018-03-30 02:39:53 +00:00
|
|
|
return db.table('files')
|
|
|
|
.where('id', file.id)
|
|
|
|
.del()
|
|
|
|
.then(() => {
|
2018-04-05 12:54:24 +00:00
|
|
|
if (file.albumid && !albumids.includes(file.albumid)) {
|
|
|
|
albumids.push(file.albumid)
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
2018-04-05 12:54:24 +00:00
|
|
|
failedids.push(file.id)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Third, we update albums, if necessary
|
2018-04-05 12:54:24 +00:00
|
|
|
await Promise.all(albumids.map(albumid => {
|
2018-03-30 02:39:53 +00:00
|
|
|
return db.table('albums')
|
|
|
|
.where('id', albumid)
|
|
|
|
.update('editedAt', Math.floor(Date.now() / 1000))
|
|
|
|
}))
|
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
return failedids
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = utilsController
|