2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const ffmpeg = require('fluent-ffmpeg')
|
2018-01-23 20:06:30 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const gm = require('gm')
|
2018-04-13 16:20:57 +00:00
|
|
|
const path = require('path')
|
2018-05-09 09:53:27 +00:00
|
|
|
const snekfetch = require('snekfetch')
|
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-04-29 12:47:24 +00:00
|
|
|
const uploadsDir = path.join(__dirname, '..', config.uploads.folder)
|
|
|
|
const thumbsDir = path.join(uploadsDir, 'thumbs')
|
2018-05-09 09:53:27 +00:00
|
|
|
const cloudflareAuth = config.cloudflare.apiKey && config.cloudflare.email && config.cloudflare.zoneId
|
2018-04-29 12:47:24 +00:00
|
|
|
|
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-04-29 12:47:24 +00:00
|
|
|
utilsController.mayGenerateThumb = extname => {
|
2018-05-09 08:41:30 +00:00
|
|
|
return (config.uploads.generateThumbs.image && utilsController.imageExtensions.includes(extname)) ||
|
|
|
|
(config.uploads.generateThumbs.video && utilsController.videoExtensions.includes(extname))
|
2018-04-29 12:47:24 +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-04-29 12:47:24 +00:00
|
|
|
|
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-04-29 12:47:24 +00:00
|
|
|
const extname = path.extname(file.name).toLowerCase()
|
|
|
|
if (!utilsController.mayGenerateThumb(extname)) { return }
|
2018-02-07 06:22:31 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
const thumbname = path.join(thumbsDir, file.name.slice(0, -extname.length) + '.png')
|
2018-03-29 23:22:08 +00:00
|
|
|
fs.access(thumbname, error => {
|
2018-04-29 12:47:24 +00:00
|
|
|
// Only make thumbnail if it does not exist (ENOENT)
|
|
|
|
if (!error || error.code !== 'ENOENT') { return }
|
|
|
|
|
|
|
|
// If image extension
|
|
|
|
if (utilsController.imageExtensions.includes(extname)) {
|
|
|
|
const size = { width: 200, height: 200 }
|
|
|
|
return 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-05-09 08:41:30 +00:00
|
|
|
if (error) {
|
|
|
|
console.error(`${file.name}: ${error.message.trim()}`)
|
|
|
|
const placeholder = path.join(__dirname, '../public/images/unavailable.png')
|
|
|
|
fs.symlink(placeholder, thumbname, error => {
|
|
|
|
if (error) { console.error(error) }
|
|
|
|
})
|
|
|
|
}
|
2018-04-29 12:47:24 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2018-04-29 12:47:24 +00:00
|
|
|
|
|
|
|
// Otherwise video extension
|
|
|
|
ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name))
|
|
|
|
.thumbnail({
|
|
|
|
timestamps: ['1%'],
|
|
|
|
filename: '%b.png',
|
|
|
|
folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'),
|
|
|
|
size: '200x?'
|
|
|
|
})
|
2018-05-09 08:41:30 +00:00
|
|
|
.on('error', error => console.log(`${file.name}: ${error.message}`))
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-04-20 21:39:06 +00:00
|
|
|
utilsController.deleteFile = file => {
|
2018-03-30 02:39:53 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-29 12:47:24 +00:00
|
|
|
const extname = path.extname(file).toLowerCase()
|
|
|
|
return fs.unlink(path.join(uploadsDir, file), error => {
|
|
|
|
if (error && error.code !== 'ENOENT') { return reject(error) }
|
|
|
|
|
|
|
|
if (utilsController.imageExtensions.includes(extname) || utilsController.videoExtensions.includes(extname)) {
|
|
|
|
const thumb = file.substr(0, file.lastIndexOf('.')) + '.png'
|
|
|
|
return fs.unlink(path.join(thumbsDir, thumb), error => {
|
|
|
|
if (error && error.code !== 'ENOENT') { return reject(error) }
|
|
|
|
resolve(true)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
2018-04-29 12:47:24 +00:00
|
|
|
}
|
|
|
|
resolve(true)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
/**
|
|
|
|
* Delete files by matching whether the specified field contains any value
|
|
|
|
* in the array of values. This will return an array of values that could
|
|
|
|
* not be deleted. At the moment it's hard-coded to only accept either
|
|
|
|
* "id" or "name" field.
|
|
|
|
*
|
|
|
|
* @param {string} field
|
|
|
|
* @param {any} values
|
|
|
|
* @param {user} user
|
|
|
|
* @return {any[]} failed
|
|
|
|
*/
|
|
|
|
utilsController.bulkDeleteFiles = async (field, values, user) => {
|
2018-03-30 02:39:53 +00:00
|
|
|
if (!user) { return }
|
2018-05-05 19:44:58 +00:00
|
|
|
if (!['id', 'name'].includes(field)) { return }
|
2018-03-30 02:39:53 +00:00
|
|
|
const files = await db.table('files')
|
2018-05-05 19:44:58 +00:00
|
|
|
.whereIn(field, values)
|
2018-03-30 02:39:53 +00:00
|
|
|
.where(function () {
|
|
|
|
if (user.username !== 'root') {
|
|
|
|
this.where('userid', user.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
const failed = values.filter(value => !files.find(file => file[field] === value))
|
2018-04-20 21:39:06 +00:00
|
|
|
const albumids = []
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-04-20 21:39:06 +00:00
|
|
|
// Delete all files
|
2018-03-30 02:39:53 +00:00
|
|
|
await Promise.all(files.map(file => {
|
2018-04-20 21:39:06 +00:00
|
|
|
return new Promise(async resolve => {
|
|
|
|
const deleteFile = await utilsController.deleteFile(file.name)
|
|
|
|
.catch(error => {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-05-05 19:44:58 +00:00
|
|
|
failed.push(file[field])
|
2018-04-20 21:39:06 +00:00
|
|
|
})
|
|
|
|
if (!deleteFile) { return resolve() }
|
|
|
|
|
|
|
|
await db.table('files')
|
2018-05-05 19:44:58 +00:00
|
|
|
.where(field, file[field])
|
2018-04-20 21:39:06 +00:00
|
|
|
.del()
|
|
|
|
.then(() => {
|
|
|
|
if (file.albumid && !albumids.includes(file.albumid)) {
|
|
|
|
albumids.push(file.albumid)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
2018-05-05 19:44:58 +00:00
|
|
|
failed.push(file[field])
|
2018-04-20 21:39:06 +00:00
|
|
|
})
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-04-20 21:39:06 +00:00
|
|
|
return resolve()
|
|
|
|
})
|
2018-03-30 02:39:53 +00:00
|
|
|
}))
|
|
|
|
|
2018-04-20 21:39:06 +00:00
|
|
|
// Update albums if necessary
|
|
|
|
if (albumids.length) {
|
|
|
|
await Promise.all(albumids.map(albumid => {
|
|
|
|
return db.table('albums')
|
|
|
|
.where('id', albumid)
|
|
|
|
.update('editedAt', Math.floor(Date.now() / 1000))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:53:27 +00:00
|
|
|
if (config.cloudflare.purgeCache) {
|
|
|
|
// purgeCloudflareCache() is an async function, but let us not wait for it
|
|
|
|
const names = files.filter(file => !failed.includes(file[field])).map(file => file.name)
|
|
|
|
utilsController.purgeCloudflareCache(names)
|
|
|
|
}
|
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
return failed
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:53:27 +00:00
|
|
|
utilsController.purgeCloudflareCache = async names => {
|
|
|
|
if (!cloudflareAuth) { return }
|
|
|
|
|
|
|
|
const thumbs = []
|
|
|
|
names = names.map(name => {
|
|
|
|
const url = `${config.domain}/${name}`
|
|
|
|
const extname = path.extname(name).toLowerCase()
|
|
|
|
if (utilsController.mayGenerateThumb(extname)) {
|
|
|
|
thumbs.push(`${config.domain}/thumbs/${name.slice(0, -extname.length)}.png`)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
})
|
|
|
|
|
|
|
|
const purge = await snekfetch
|
|
|
|
.post(`https://api.cloudflare.com/client/v4/zones/${config.cloudflare.zoneId}/purge_cache`)
|
|
|
|
.set({
|
|
|
|
'X-Auth-Email': config.cloudflare.email,
|
|
|
|
'X-Auth-Key': config.cloudflare.apiKey
|
|
|
|
})
|
|
|
|
.send({ files: names.concat(thumbs) })
|
|
|
|
.catch(error => error)
|
|
|
|
|
|
|
|
if (purge.body && !purge.body.success) {
|
|
|
|
purge.body.errors.forEach(error => console.error(`CF: ${error.code}: ${error.message}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = utilsController
|