2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
|
|
|
const db = require('knex')(config.database)
|
2018-09-23 16:28:15 +00:00
|
|
|
const fetch = require('node-fetch')
|
2018-04-13 16:20:57 +00:00
|
|
|
const ffmpeg = require('fluent-ffmpeg')
|
2018-01-23 20:06:30 +00:00
|
|
|
const fs = require('fs')
|
2018-04-13 16:20:57 +00:00
|
|
|
const path = require('path')
|
2018-10-13 11:06:58 +00:00
|
|
|
const perms = require('./permissionController')
|
2018-12-03 07:20:13 +00:00
|
|
|
const sharp = require('sharp')
|
2017-03-17 04:14:10 +00:00
|
|
|
|
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 10:07:23 +00:00
|
|
|
const thumbUnavailable = path.join(__dirname, '../public/images/unavailable.png')
|
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
|
|
|
|
2019-01-18 01:15:53 +00:00
|
|
|
utilsController.imageExtensions = ['.webp', '.jpg', '.jpeg', '.bmp', '.gif', '.png', '.tiff', '.tif', '.svg']
|
2018-03-28 11:36:28 +00:00
|
|
|
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-09-17 19:32:27 +00:00
|
|
|
// expand if necessary (must be lower case); for now only preserves some known tarballs
|
|
|
|
utilsController.preserves = ['.tar.gz', '.tar.z', '.tar.bz2', '.tar.lzma', '.tar.lzo', '.tar.xz']
|
|
|
|
|
|
|
|
utilsController.extname = filename => {
|
2018-11-28 17:52:12 +00:00
|
|
|
// Always return blank string if the filename does not seem to have a valid extension
|
|
|
|
// Files such as .DS_Store (anything that starts with a dot, without any extension after) will still be accepted
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!/\../.test(filename)) return ''
|
2018-11-28 17:52:12 +00:00
|
|
|
|
2018-09-17 19:32:27 +00:00
|
|
|
let lower = filename.toLowerCase() // due to this, the returned extname will always be lower case
|
|
|
|
let multi = ''
|
|
|
|
let extname = ''
|
|
|
|
|
|
|
|
// check for multi-archive extensions (.001, .002, and so on)
|
|
|
|
if (/\.\d{3}$/.test(lower)) {
|
|
|
|
multi = lower.slice(lower.lastIndexOf('.') - lower.length)
|
|
|
|
lower = lower.slice(0, lower.lastIndexOf('.'))
|
|
|
|
}
|
|
|
|
|
|
|
|
// check against extensions that must be preserved
|
2018-12-18 17:01:28 +00:00
|
|
|
for (let i = 0; i < utilsController.preserves.length; i++)
|
2018-09-17 19:32:27 +00:00
|
|
|
if (lower.endsWith(utilsController.preserves[i])) {
|
|
|
|
extname = utilsController.preserves[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!extname)
|
2018-09-17 19:32:27 +00:00
|
|
|
extname = lower.slice(lower.lastIndexOf('.') - lower.length) // path.extname(lower)
|
|
|
|
|
|
|
|
return extname + multi
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:09:46 +00:00
|
|
|
utilsController.escape = string => {
|
|
|
|
// MIT License
|
|
|
|
// Copyright(c) 2012-2013 TJ Holowaychuk
|
|
|
|
// Copyright(c) 2015 Andreas Lubbe
|
|
|
|
// Copyright(c) 2015 Tiancheng "Timothy" Gu
|
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!string) return string
|
2018-12-13 09:09:46 +00:00
|
|
|
|
|
|
|
const str = '' + string
|
|
|
|
const match = /["'&<>]/.exec(str)
|
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!match) return str
|
2018-12-13 09:09:46 +00:00
|
|
|
|
|
|
|
let escape
|
|
|
|
let html = ''
|
|
|
|
let index = 0
|
|
|
|
let lastIndex = 0
|
|
|
|
|
|
|
|
for (index = match.index; index < str.length; index++) {
|
|
|
|
switch (str.charCodeAt(index)) {
|
|
|
|
case 34: // "
|
|
|
|
escape = '"'
|
|
|
|
break
|
|
|
|
case 38: // &
|
|
|
|
escape = '&'
|
|
|
|
break
|
|
|
|
case 39: // '
|
|
|
|
escape = '''
|
|
|
|
break
|
|
|
|
case 60: // <
|
|
|
|
escape = '<'
|
|
|
|
break
|
|
|
|
case 62: // >
|
|
|
|
escape = '>'
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (lastIndex !== index)
|
2018-12-13 09:09:46 +00:00
|
|
|
html += str.substring(lastIndex, index)
|
|
|
|
|
|
|
|
lastIndex = index + 1
|
|
|
|
html += escape
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastIndex !== index
|
|
|
|
? html + str.substring(lastIndex, index)
|
|
|
|
: html
|
|
|
|
}
|
|
|
|
|
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-10-09 19:52:41 +00:00
|
|
|
if (user) {
|
|
|
|
if (user.enabled === false || user.enabled === 0) {
|
|
|
|
res.json({ success: false, description: 'This account has been disabled.' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2018-04-29 12:47:24 +00:00
|
|
|
|
2018-10-09 19:52: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-05-12 14:01:14 +00:00
|
|
|
utilsController.generateThumbs = (name, force) => {
|
|
|
|
return new Promise(resolve => {
|
2018-09-17 19:32:27 +00:00
|
|
|
const extname = utilsController.extname(name)
|
2018-05-12 14:01:14 +00:00
|
|
|
const thumbname = path.join(thumbsDir, name.slice(0, -extname.length) + '.png')
|
2018-09-04 17:29:53 +00:00
|
|
|
fs.lstat(thumbname, async (error, stats) => {
|
2018-05-12 14:01:14 +00:00
|
|
|
if (error && error.code !== 'ENOENT') {
|
|
|
|
console.error(error)
|
|
|
|
return resolve(false)
|
|
|
|
}
|
|
|
|
|
2018-09-04 17:29:53 +00:00
|
|
|
if (!error && stats.isSymbolicLink()) {
|
|
|
|
// Unlink symlink
|
|
|
|
const unlink = await new Promise((resolve, reject) => {
|
|
|
|
fs.unlink(thumbname, error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error) return reject(error)
|
2018-09-04 17:29:53 +00:00
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
}).catch(console.error)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!unlink) return resolve(false)
|
2018-09-04 17:29:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 14:01:14 +00:00
|
|
|
// Only make thumbnail if it does not exist (ENOENT)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!error && !stats.isSymbolicLink() && !force) return resolve(true)
|
2018-05-12 14:01:14 +00:00
|
|
|
|
|
|
|
// If image extension
|
|
|
|
if (utilsController.imageExtensions.includes(extname)) {
|
2018-12-03 07:20:13 +00:00
|
|
|
const resizeOptions = {
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
fit: 'contain',
|
|
|
|
background: {
|
|
|
|
r: 0,
|
|
|
|
g: 0,
|
|
|
|
b: 0,
|
|
|
|
alpha: 0
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 01:54:46 +00:00
|
|
|
const image = sharp(path.join(__dirname, '..', config.uploads.folder, name))
|
|
|
|
return image
|
|
|
|
.metadata()
|
|
|
|
.then(metadata => {
|
|
|
|
if (metadata.width > resizeOptions.width || metadata.height > resizeOptions.height) {
|
|
|
|
return image
|
|
|
|
.resize(resizeOptions)
|
|
|
|
.toFile(thumbname)
|
|
|
|
} else if (metadata.width === resizeOptions.width && metadata.height === resizeOptions.height) {
|
|
|
|
return image
|
|
|
|
.toFile(thumbname)
|
|
|
|
} else {
|
|
|
|
const x = resizeOptions.width - metadata.width
|
|
|
|
const y = resizeOptions.height - metadata.height
|
|
|
|
return image
|
|
|
|
.extend({
|
|
|
|
top: Math.floor(y / 2),
|
|
|
|
bottom: Math.ceil(y / 2),
|
|
|
|
left: Math.floor(x / 2),
|
|
|
|
right: Math.ceil(x / 2),
|
|
|
|
background: resizeOptions.background
|
|
|
|
})
|
|
|
|
.toFile(thumbname)
|
|
|
|
}
|
|
|
|
})
|
2019-01-06 06:27:17 +00:00
|
|
|
.then(() => {
|
|
|
|
resolve(true)
|
|
|
|
})
|
2018-12-03 07:20:13 +00:00
|
|
|
.catch(error => {
|
2019-01-03 01:54:46 +00:00
|
|
|
console.error(`${name}: ${error.toString()}`)
|
2018-05-09 10:07:23 +00:00
|
|
|
fs.symlink(thumbUnavailable, thumbname, error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error) console.error(error)
|
2018-05-12 14:01:14 +00:00
|
|
|
resolve(!error)
|
2018-05-09 08:41:30 +00:00
|
|
|
})
|
2018-05-12 14:01:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise video extension
|
|
|
|
ffmpeg(path.join(__dirname, '..', config.uploads.folder, name))
|
|
|
|
.thumbnail({
|
|
|
|
timestamps: ['1%'],
|
|
|
|
filename: '%b.png',
|
|
|
|
folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'),
|
|
|
|
size: '200x?'
|
2018-04-29 12:47:24 +00:00
|
|
|
})
|
2018-05-12 14:01:14 +00:00
|
|
|
.on('error', error => {
|
|
|
|
console.log(`${name}: ${error.message}`)
|
|
|
|
fs.symlink(thumbUnavailable, thumbname, error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error) console.error(error)
|
2018-05-12 14:01:14 +00:00
|
|
|
resolve(!error)
|
|
|
|
})
|
2018-05-09 10:07:23 +00:00
|
|
|
})
|
2018-05-12 14:01:14 +00:00
|
|
|
.on('end', () => {
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
2017-03-17 04:14:10 +00:00
|
|
|
|
2018-12-20 11:53:37 +00:00
|
|
|
utilsController.deleteFile = (filename, set) => {
|
2018-03-30 02:39:53 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-20 11:53:37 +00:00
|
|
|
const extname = utilsController.extname(filename)
|
|
|
|
return fs.unlink(path.join(uploadsDir, filename), error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error && error.code !== 'ENOENT') return reject(error)
|
2018-12-20 11:53:37 +00:00
|
|
|
const identifier = filename.split('.')[0]
|
|
|
|
// eslint-disable-next-line curly
|
|
|
|
if (set) {
|
|
|
|
set.delete(identifier)
|
|
|
|
// console.log(`Removed ${identifier} from identifiers cache (deleteFile)`)
|
|
|
|
}
|
2018-04-29 12:47:24 +00:00
|
|
|
if (utilsController.imageExtensions.includes(extname) || utilsController.videoExtensions.includes(extname)) {
|
2018-12-20 11:53:37 +00:00
|
|
|
const thumb = `${identifier}.png`
|
2018-04-29 12:47:24 +00:00
|
|
|
return fs.unlink(path.join(thumbsDir, thumb), error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error && error.code !== 'ENOENT') return reject(error)
|
2018-04-29 12:47:24 +00:00
|
|
|
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-12-03 09:18:52 +00:00
|
|
|
utilsController.bulkDeleteFiles = async (field, values, user, set) => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user || !['id', 'name'].includes(field)) return
|
2018-05-10 17:25:52 +00:00
|
|
|
|
2018-10-13 11:06:58 +00:00
|
|
|
const ismoderator = perms.is(user, 'moderator')
|
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 () {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!ismoderator)
|
2018-03-30 02:39:53 +00:00
|
|
|
this.where('userid', user.id)
|
|
|
|
})
|
|
|
|
|
2018-12-03 09:18:52 +00:00
|
|
|
// an array of file object
|
|
|
|
const deletedFiles = []
|
|
|
|
|
|
|
|
// an array of value of the specified field
|
2018-05-05 19:44:58 +00:00
|
|
|
const failed = values.filter(value => !files.find(file => file[field] === value))
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-05-10 17:25:52 +00:00
|
|
|
// Delete all files physically
|
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 => {
|
2018-05-10 17:25:52 +00:00
|
|
|
await utilsController.deleteFile(file.name)
|
2018-12-03 09:18:52 +00:00
|
|
|
.then(() => deletedFiles.push(file))
|
2018-04-20 21:39:06 +00:00
|
|
|
.catch(error => {
|
2018-05-05 19:44:58 +00:00
|
|
|
failed.push(file[field])
|
2018-04-20 21:39:06 +00:00
|
|
|
console.error(error)
|
|
|
|
})
|
2018-05-11 14:34:13 +00:00
|
|
|
resolve()
|
2018-04-20 21:39:06 +00:00
|
|
|
})
|
2018-03-30 02:39:53 +00:00
|
|
|
}))
|
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!deletedFiles.length) return failed
|
2018-05-10 17:25:52 +00:00
|
|
|
|
|
|
|
// Delete all files from database
|
2018-12-03 09:18:52 +00:00
|
|
|
const deletedIds = deletedFiles.map(file => file.id)
|
2018-05-10 17:25:52 +00:00
|
|
|
const deleteDb = await db.table('files')
|
2018-12-03 09:18:52 +00:00
|
|
|
.whereIn('id', deletedIds)
|
2018-05-10 17:25:52 +00:00
|
|
|
.del()
|
|
|
|
.catch(console.error)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!deleteDb) return failed
|
2018-05-10 17:25:52 +00:00
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (set)
|
2018-12-03 09:18:52 +00:00
|
|
|
deletedFiles.forEach(file => {
|
|
|
|
const identifier = file.name.split('.')[0]
|
|
|
|
set.delete(identifier)
|
|
|
|
// console.log(`Removed ${identifier} from identifiers cache (bulkDeleteFiles)`)
|
|
|
|
})
|
2018-12-18 17:01:28 +00:00
|
|
|
|
2018-12-03 09:18:52 +00:00
|
|
|
const filtered = files.filter(file => deletedIds.includes(file.id))
|
2018-05-10 17:25:52 +00:00
|
|
|
|
2018-04-20 21:39:06 +00:00
|
|
|
// Update albums if necessary
|
2018-05-10 17:25:52 +00:00
|
|
|
if (deleteDb) {
|
|
|
|
const albumids = []
|
|
|
|
filtered.forEach(file => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (file.albumid && !albumids.includes(file.albumid))
|
2018-05-10 17:25:52 +00:00
|
|
|
albumids.push(file.albumid)
|
|
|
|
})
|
|
|
|
await db.table('albums')
|
|
|
|
.whereIn('id', albumids)
|
|
|
|
.update('editedAt', Math.floor(Date.now() / 1000))
|
|
|
|
.catch(console.error)
|
2018-04-20 21:39:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 10:11:45 +00:00
|
|
|
// Purge Cloudflare's cache if necessary
|
2019-01-06 06:27:17 +00:00
|
|
|
if (config.cloudflare.purgeCache)
|
|
|
|
utilsController.purgeCloudflareCache(filtered.map(file => file.name), true)
|
2019-01-09 10:11:45 +00:00
|
|
|
.then(result => {
|
|
|
|
if (!result.errors.length) return
|
|
|
|
result.errors.forEach(error => console.error(`CF: ${error}`))
|
|
|
|
})
|
2018-05-09 09:53:27 +00:00
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
return failed
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 10:11:45 +00:00
|
|
|
utilsController.purgeCloudflareCache = async (names, uploads) => {
|
|
|
|
if (!cloudflareAuth)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
files: [],
|
|
|
|
errors: ['Missing auth.']
|
|
|
|
}
|
2019-01-06 06:27:17 +00:00
|
|
|
|
|
|
|
let domain = config.domain
|
|
|
|
if (!uploads) domain = config.homeDomain
|
2018-05-09 09:53:27 +00:00
|
|
|
|
|
|
|
const thumbs = []
|
|
|
|
names = names.map(name => {
|
2019-01-06 08:26:43 +00:00
|
|
|
if (uploads) {
|
|
|
|
const url = `${domain}/${name}`
|
|
|
|
const extname = utilsController.extname(name)
|
|
|
|
if (utilsController.mayGenerateThumb(extname))
|
|
|
|
thumbs.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`)
|
|
|
|
return url
|
|
|
|
} else {
|
|
|
|
return name === 'home' ? domain : `${domain}/${name}`
|
|
|
|
}
|
2018-05-09 09:53:27 +00:00
|
|
|
})
|
|
|
|
|
2019-01-09 10:11:45 +00:00
|
|
|
const files = names.concat(thumbs)
|
|
|
|
let success = false
|
|
|
|
let errors = []
|
2018-09-23 16:28:15 +00:00
|
|
|
try {
|
|
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${config.cloudflare.zoneId}/purge_cache`
|
|
|
|
const fetchPurge = await fetch(url, {
|
|
|
|
method: 'POST',
|
2019-01-06 06:27:17 +00:00
|
|
|
body: JSON.stringify({ files }),
|
2018-09-23 16:28:15 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Email': config.cloudflare.email,
|
|
|
|
'X-Auth-Key': config.cloudflare.apiKey
|
|
|
|
}
|
|
|
|
}).then(res => res.json())
|
2019-01-09 10:11:45 +00:00
|
|
|
success = fetchPurge.success
|
2019-01-06 06:27:17 +00:00
|
|
|
if (Array.isArray(fetchPurge.errors) && fetchPurge.errors.length)
|
2019-01-09 10:11:45 +00:00
|
|
|
errors = fetchPurge.errors.map(error => `${error.code}: ${error.message}`)
|
2018-09-23 16:28:15 +00:00
|
|
|
} catch (error) {
|
2019-01-09 10:11:45 +00:00
|
|
|
errors.push(error.toString())
|
2018-05-09 09:53:27 +00:00
|
|
|
}
|
2019-01-09 10:11:45 +00:00
|
|
|
return { success, files, errors }
|
2018-05-09 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = utilsController
|