2019-04-05 17:32:52 +00:00
|
|
|
const { spawn } = require('child_process')
|
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')
|
2019-04-05 17:32:52 +00:00
|
|
|
const os = require('os')
|
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
|
|
|
|
2019-04-05 17:42:38 +00:00
|
|
|
const utilsController = {}
|
2019-04-12 00:45:33 +00:00
|
|
|
const _stats = {
|
|
|
|
system: {
|
|
|
|
cache: null,
|
2019-06-03 19:40:24 +00:00
|
|
|
generating: false,
|
|
|
|
generatedAt: 0
|
2019-04-12 00:45:33 +00:00
|
|
|
},
|
|
|
|
albums: {
|
|
|
|
cache: null,
|
2019-06-03 19:40:24 +00:00
|
|
|
generating: false,
|
|
|
|
generatedAt: 0,
|
|
|
|
invalidatedAt: 0
|
2019-04-12 00:45:33 +00:00
|
|
|
},
|
|
|
|
users: {
|
|
|
|
cache: null,
|
2019-06-03 19:40:24 +00:00
|
|
|
generating: false,
|
|
|
|
generatedAt: 0,
|
|
|
|
invalidatedAt: 0
|
2019-04-12 00:45:33 +00:00
|
|
|
},
|
|
|
|
uploads: {
|
|
|
|
cache: null,
|
2019-06-03 19:40:24 +00:00
|
|
|
generating: false,
|
|
|
|
generatedAt: 0,
|
|
|
|
invalidatedAt: 0
|
2019-04-12 00:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 17:32:52 +00:00
|
|
|
|
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 03:25:30 +00:00
|
|
|
utilsController.imageExtensions = ['.webp', '.jpg', '.jpeg', '.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
|
2019-07-20 22:15:59 +00:00
|
|
|
const unlink = await new Promise(resolve => {
|
2018-09-04 17:29:53 +00:00
|
|
|
fs.unlink(thumbname, error => {
|
2019-07-20 22:15:59 +00:00
|
|
|
if (error) console.error(error)
|
|
|
|
resolve(!error)
|
2018-09-04 17:29:53 +00:00
|
|
|
})
|
2019-07-20 22:15:59 +00:00
|
|
|
})
|
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)
|
2019-07-20 22:15:59 +00:00
|
|
|
if (!error && !force) return resolve(true)
|
2018-05-12 14:01:14 +00:00
|
|
|
|
2019-01-29 19:50:45 +00:00
|
|
|
// Full path to input file
|
|
|
|
const input = path.join(__dirname, '..', config.uploads.folder, name)
|
|
|
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
// If image extension
|
|
|
|
if (utilsController.imageExtensions.includes(extname)) {
|
|
|
|
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
|
|
|
}
|
2019-01-29 19:50:45 +00:00
|
|
|
}
|
|
|
|
const image = sharp(input)
|
|
|
|
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)
|
|
|
|
}
|
2018-05-09 08:41:30 +00:00
|
|
|
})
|
2019-01-29 19:50:45 +00:00
|
|
|
.then(() => resolve(true))
|
|
|
|
.catch(reject)
|
|
|
|
}
|
2018-05-12 14:01:14 +00:00
|
|
|
|
2019-01-29 19:50:45 +00:00
|
|
|
// Otherwise video extension
|
|
|
|
ffmpeg.ffprobe(input, (error, metadata) => {
|
|
|
|
if (error) return reject(error)
|
|
|
|
ffmpeg(input)
|
|
|
|
.inputOptions([
|
|
|
|
`-ss ${parseInt(metadata.format.duration) * 20 / 100}`
|
|
|
|
])
|
|
|
|
.output(thumbname)
|
|
|
|
.outputOptions([
|
|
|
|
'-vframes 1',
|
|
|
|
'-vf scale=200:200:force_original_aspect_ratio=decrease'
|
|
|
|
])
|
2019-07-20 22:15:59 +00:00
|
|
|
.on('error', error => {
|
|
|
|
// Attempt to unlink thumbnail
|
|
|
|
// Since ffmpeg may have already created an incomplete thumbnail
|
|
|
|
fs.unlink(thumbname, err => {
|
|
|
|
if (err && err.code !== 'ENOENT')
|
|
|
|
console.error(`${name}: ${err.toString()}`)
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
})
|
2019-01-29 19:50:45 +00:00
|
|
|
.on('end', () => resolve(true))
|
|
|
|
.run()
|
2018-04-29 12:47:24 +00:00
|
|
|
})
|
2019-01-29 19:50:45 +00:00
|
|
|
})
|
|
|
|
.then(resolve)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(`${name}: ${error.toString()}`)
|
2018-05-12 14:01:14 +00:00
|
|
|
fs.symlink(thumbUnavailable, thumbname, error => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (error) console.error(error)
|
2019-06-03 19:40:24 +00:00
|
|
|
// We return true if we could make a symlink to the placeholder image
|
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
|
|
|
})
|
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
|
|
|
|
2019-02-05 03:36:14 +00:00
|
|
|
// SQLITE_LIMIT_VARIABLE_NUMBER, which defaults to 999
|
|
|
|
// Read more: https://www.sqlite.org/limits.html
|
|
|
|
const MAX_VARIABLES_CHUNK_SIZE = 999
|
|
|
|
const chunks = []
|
|
|
|
const _values = values.slice() // Make a shallow copy of the array
|
|
|
|
while (_values.length)
|
|
|
|
chunks.push(_values.splice(0, MAX_VARIABLES_CHUNK_SIZE))
|
|
|
|
|
|
|
|
const failed = []
|
2018-10-13 11:06:58 +00:00
|
|
|
const ismoderator = perms.is(user, 'moderator')
|
2019-02-05 03:36:14 +00:00
|
|
|
await Promise.all(chunks.map((chunk, index) => {
|
2019-08-03 23:37:55 +00:00
|
|
|
// It's much too dirty to code the function below without async function
|
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
2019-02-05 03:36:14 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const files = await db.table('files')
|
|
|
|
.whereIn(field, chunk)
|
|
|
|
.where(function () {
|
|
|
|
if (!ismoderator)
|
|
|
|
this.where('userid', user.id)
|
|
|
|
})
|
|
|
|
.catch(reject)
|
|
|
|
|
|
|
|
// Push files that could not be found in DB
|
|
|
|
failed.push.apply(failed, chunk.filter(v => !files.find(file => file[field] === v)))
|
|
|
|
|
|
|
|
// Delete all found files physically
|
|
|
|
const deletedFiles = []
|
|
|
|
await Promise.all(files.map(file =>
|
|
|
|
utilsController.deleteFile(file.name)
|
|
|
|
.then(() => deletedFiles.push(file))
|
|
|
|
.catch(error => {
|
|
|
|
failed.push(file[field])
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
))
|
|
|
|
|
|
|
|
if (!deletedFiles.length)
|
|
|
|
return resolve()
|
|
|
|
|
|
|
|
// Delete all found files from database
|
|
|
|
const deletedFromDb = await db.table('files')
|
|
|
|
.whereIn('id', deletedFiles.map(file => file.id))
|
|
|
|
.del()
|
|
|
|
.catch(reject)
|
|
|
|
|
|
|
|
if (set)
|
|
|
|
deletedFiles.forEach(file => {
|
|
|
|
const identifier = file.name.split('.')[0]
|
|
|
|
set.delete(identifier)
|
2019-08-03 23:42:35 +00:00
|
|
|
// console.log(`Removed ${identifier} from identifiers cache (bulkDeleteFiles)`)
|
2018-04-20 21:39:06 +00:00
|
|
|
})
|
2018-05-10 17:25:52 +00:00
|
|
|
|
2019-02-05 03:36:14 +00:00
|
|
|
// Update albums if necessary
|
|
|
|
if (deletedFromDb) {
|
|
|
|
const albumids = []
|
|
|
|
deletedFiles.forEach(file => {
|
|
|
|
if (file.albumid && !albumids.includes(file.albumid))
|
|
|
|
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-02-05 03:36:14 +00:00
|
|
|
// Purge Cloudflare's cache if necessary
|
|
|
|
if (config.cloudflare.purgeCache)
|
|
|
|
utilsController.purgeCloudflareCache(deletedFiles.map(file => file.name), true, true)
|
|
|
|
.then(results => {
|
|
|
|
for (const result of results)
|
|
|
|
if (result.errors.length)
|
|
|
|
result.errors.forEach(error => console.error(`CF: ${error}`))
|
|
|
|
})
|
2018-05-09 09:53:27 +00:00
|
|
|
|
2019-02-05 03:36:14 +00:00
|
|
|
return resolve()
|
2019-08-03 23:42:35 +00:00
|
|
|
}).catch(console.error)
|
2019-02-05 03:36:14 +00:00
|
|
|
}))
|
2018-05-05 19:44:58 +00:00
|
|
|
return failed
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:40:15 +00:00
|
|
|
utilsController.purgeCloudflareCache = async (names, uploads, thumbs) => {
|
2019-01-31 09:29:34 +00:00
|
|
|
if (!Array.isArray(names) || !names.length || !cloudflareAuth)
|
|
|
|
return [{
|
2019-01-09 10:11:45 +00:00
|
|
|
success: false,
|
|
|
|
files: [],
|
2019-01-31 09:29:34 +00:00
|
|
|
errors: ['An unexpected error occured.']
|
|
|
|
}]
|
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
|
|
|
|
2019-01-18 03:40:15 +00:00
|
|
|
const thumbNames = []
|
2018-05-09 09:53:27 +00:00
|
|
|
names = names.map(name => {
|
2019-01-06 08:26:43 +00:00
|
|
|
if (uploads) {
|
|
|
|
const url = `${domain}/${name}`
|
|
|
|
const extname = utilsController.extname(name)
|
2019-01-18 03:40:15 +00:00
|
|
|
if (thumbs && utilsController.mayGenerateThumb(extname))
|
|
|
|
thumbNames.push(`${domain}/thumbs/${name.slice(0, -extname.length)}.png`)
|
2019-01-06 08:26:43 +00:00
|
|
|
return url
|
|
|
|
} else {
|
|
|
|
return name === 'home' ? domain : `${domain}/${name}`
|
|
|
|
}
|
2018-05-09 09:53:27 +00:00
|
|
|
})
|
2019-01-31 09:29:34 +00:00
|
|
|
names = names.concat(thumbNames)
|
|
|
|
|
|
|
|
// Split array into multiple arrays with max length of 30 URLs
|
|
|
|
// https://api.cloudflare.com/#zone-purge-files-by-url
|
|
|
|
const MAX_LENGTH = 30
|
|
|
|
const files = []
|
|
|
|
while (names.length)
|
|
|
|
files.push(names.splice(0, MAX_LENGTH))
|
|
|
|
|
|
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${config.cloudflare.zoneId}/purge_cache`
|
|
|
|
const results = []
|
|
|
|
await new Promise(resolve => {
|
|
|
|
const purge = async i => {
|
|
|
|
const result = {
|
|
|
|
success: false,
|
|
|
|
files: files[i],
|
2019-01-31 09:37:50 +00:00
|
|
|
errors: []
|
2019-01-31 09:29:34 +00:00
|
|
|
}
|
2018-05-09 09:53:27 +00:00
|
|
|
|
2019-01-31 09:29:34 +00:00
|
|
|
try {
|
|
|
|
const fetchPurge = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
files: result.files
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Email': config.cloudflare.email,
|
|
|
|
'X-Auth-Key': config.cloudflare.apiKey
|
|
|
|
}
|
|
|
|
}).then(res => res.json())
|
|
|
|
result.success = fetchPurge.success
|
|
|
|
if (Array.isArray(fetchPurge.errors) && fetchPurge.errors.length)
|
|
|
|
result.errors = fetchPurge.errors.map(error => `${error.code}: ${error.message}`)
|
|
|
|
} catch (error) {
|
|
|
|
result.errors = [error.toString()]
|
2018-09-23 16:28:15 +00:00
|
|
|
}
|
2019-01-31 09:29:34 +00:00
|
|
|
|
|
|
|
results.push(result)
|
|
|
|
|
|
|
|
if (i < files.length - 1)
|
|
|
|
purge(i + 1)
|
|
|
|
else
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
purge(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
return results
|
2018-05-09 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
utilsController.getMemoryUsage = () => {
|
|
|
|
// For now this is linux-only. Not sure if darwin has this too.
|
2019-04-05 17:32:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const prc = spawn('free', ['-b'])
|
|
|
|
prc.stdout.setEncoding('utf8')
|
|
|
|
prc.stdout.on('data', data => {
|
|
|
|
const parsed = {}
|
|
|
|
const str = data.toString()
|
|
|
|
const lines = str.split(/\n/g)
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
lines[i] = lines[i].split(/\s+/)
|
|
|
|
if (i === 0) continue
|
|
|
|
const id = lines[i][0].toLowerCase().slice(0, -1)
|
|
|
|
if (!id) continue
|
|
|
|
if (!parsed[id]) parsed[id] = {}
|
|
|
|
for (let j = 1; j < lines[i].length; j++) {
|
|
|
|
const bytes = parseInt(lines[i][j])
|
|
|
|
parsed[id][lines[0][j]] = isNaN(bytes) ? null : bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve(parsed)
|
|
|
|
})
|
|
|
|
prc.on('close', code => {
|
|
|
|
reject(new Error(`Process exited with code ${code}.`))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
utilsController.invalidateStatsCache = type => {
|
|
|
|
if (!['albums', 'users', 'uploads'].includes(type)) return
|
2019-06-03 19:40:24 +00:00
|
|
|
_stats[type].invalidatedAt = Date.now()
|
2019-04-12 00:45:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:32:52 +00:00
|
|
|
utilsController.stats = async (req, res, next) => {
|
|
|
|
const user = await utilsController.authorize(req, res)
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const isadmin = perms.is(user, 'admin')
|
|
|
|
if (!isadmin) return res.status(403).end()
|
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
const stats = {}
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
if (!_stats.system.cache && _stats.system.generating) {
|
|
|
|
stats.system = false
|
|
|
|
} else if ((Date.now() - _stats.system.generatedAt <= 1000) || _stats.system.generating) {
|
|
|
|
// Re-use system cache for only 1000ms
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.system = _stats.system.cache
|
|
|
|
} else {
|
2019-06-03 19:40:24 +00:00
|
|
|
_stats.system.generating = true
|
2019-04-12 00:45:33 +00:00
|
|
|
const platform = os.platform()
|
|
|
|
stats.system = {
|
|
|
|
platform: `${platform}-${os.arch()}`,
|
|
|
|
systemMemory: null,
|
|
|
|
nodeVersion: `${process.versions.node}`,
|
|
|
|
memoryUsage: process.memoryUsage().rss
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
if (platform === 'linux') {
|
|
|
|
const memoryUsage = await utilsController.getMemoryUsage()
|
|
|
|
stats.system.systemMemory = {
|
|
|
|
used: memoryUsage.mem.used,
|
|
|
|
total: memoryUsage.mem.total
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete stats.system.systemMemory
|
|
|
|
}
|
2019-04-05 17:32:52 +00:00
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
if (platform !== 'win32')
|
|
|
|
stats.system.loadAverage = `${os.loadavg().map(load => load.toFixed(2)).join(', ')}`
|
2019-04-05 17:32:52 +00:00
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
// Update cache
|
|
|
|
_stats.system.cache = stats.system
|
|
|
|
_stats.system.generatedAt = Date.now()
|
|
|
|
_stats.system.generating = false
|
2019-04-12 00:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-use albums, users, and uploads caches as long as they are still valid
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
if (!_stats.albums.cache && _stats.albums.generating) {
|
|
|
|
stats.albums = false
|
|
|
|
} else if ((_stats.albums.invalidatedAt < _stats.albums.generatedAt) || _stats.albums.generating) {
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.albums = _stats.albums.cache
|
|
|
|
} else {
|
2019-06-03 19:40:24 +00:00
|
|
|
_stats.albums.generating = true
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.albums = {
|
|
|
|
total: 0,
|
|
|
|
active: 0,
|
|
|
|
downloadable: 0,
|
|
|
|
public: 0,
|
|
|
|
zips: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const albums = await db.table('albums')
|
|
|
|
stats.albums.total = albums.length
|
|
|
|
const identifiers = []
|
|
|
|
for (const album of albums)
|
|
|
|
if (album.enabled) {
|
|
|
|
stats.albums.active++
|
|
|
|
if (album.download) stats.albums.downloadable++
|
|
|
|
if (album.public) stats.albums.public++
|
|
|
|
if (album.zipGeneratedAt) identifiers.push(album.identifier)
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
2019-04-12 00:45:33 +00:00
|
|
|
|
|
|
|
const zipsDir = path.join(uploadsDir, 'zips')
|
|
|
|
await Promise.all(identifiers.map(identifier => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const filePath = path.join(zipsDir, `${identifier}.zip`)
|
|
|
|
fs.access(filePath, error => {
|
|
|
|
if (!error) stats.albums.zips++
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
// Update cache
|
|
|
|
_stats.albums.cache = stats.albums
|
|
|
|
_stats.albums.generatedAt = Date.now()
|
|
|
|
_stats.albums.generating = false
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
if (!_stats.users.cache && _stats.users.generating) {
|
|
|
|
stats.users = false
|
|
|
|
} else if ((_stats.users.invalidatedAt < _stats.users.generatedAt) || _stats.users.generating) {
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.users = _stats.users.cache
|
|
|
|
} else {
|
2019-06-03 19:40:24 +00:00
|
|
|
_stats.users.generating = true
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.users = {
|
|
|
|
total: 0,
|
|
|
|
disabled: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const permissionKeys = Object.keys(perms.permissions)
|
|
|
|
permissionKeys.forEach(p => {
|
|
|
|
stats.users[p] = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
const users = await db.table('users')
|
|
|
|
stats.users.total = users.length
|
|
|
|
for (const user of users) {
|
|
|
|
if (user.enabled === false || user.enabled === 0)
|
|
|
|
stats.users.disabled++
|
|
|
|
|
|
|
|
// This may be inaccurate on installations with customized permissions
|
|
|
|
user.permission = user.permission || 0
|
|
|
|
for (const p of permissionKeys)
|
|
|
|
if (user.permission === perms.permissions[p]) {
|
|
|
|
stats.users[p]++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 17:32:52 +00:00
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
// Update cache
|
|
|
|
_stats.users.cache = stats.users
|
|
|
|
_stats.users.generatedAt = Date.now()
|
|
|
|
_stats.users.generating = false
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
if (!_stats.uploads.cache && _stats.uploads.generating) {
|
|
|
|
stats.uploads = false
|
|
|
|
} else if ((_stats.uploads.invalidatedAt < _stats.uploads.generatedAt) || _stats.uploads.generating) {
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.uploads = _stats.uploads.cache
|
|
|
|
} else {
|
2019-06-03 19:40:24 +00:00
|
|
|
_stats.uploads.generating = true
|
2019-04-12 00:45:33 +00:00
|
|
|
stats.uploads = {
|
|
|
|
total: 0,
|
|
|
|
size: 0,
|
|
|
|
images: 0,
|
|
|
|
videos: 0,
|
|
|
|
others: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploads = await db.table('files')
|
|
|
|
stats.uploads.total = uploads.length
|
|
|
|
for (const upload of uploads) {
|
|
|
|
stats.uploads.size += parseInt(upload.size)
|
|
|
|
const extname = utilsController.extname(upload.name)
|
|
|
|
if (utilsController.imageExtensions.includes(extname))
|
|
|
|
stats.uploads.images++
|
|
|
|
else if (utilsController.videoExtensions.includes(extname))
|
|
|
|
stats.uploads.videos++
|
|
|
|
else
|
|
|
|
stats.uploads.others++
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:40:24 +00:00
|
|
|
// Update cache
|
|
|
|
_stats.uploads.cache = stats.uploads
|
|
|
|
_stats.uploads.generatedAt = Date.now()
|
|
|
|
_stats.uploads.generating = false
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 00:45:33 +00:00
|
|
|
return res.json({ success: true, stats })
|
2019-04-05 17:32:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = utilsController
|