2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
2018-01-23 20:06:30 +00:00
|
|
|
const db = require('knex')(config.database)
|
2018-05-09 08:41:30 +00:00
|
|
|
const EventEmitter = require('events')
|
2018-01-23 20:06:30 +00:00
|
|
|
const fs = require('fs')
|
2018-04-13 16:20:57 +00:00
|
|
|
const path = require('path')
|
|
|
|
const randomstring = require('randomstring')
|
|
|
|
const utils = require('./utilsController')
|
2018-01-23 20:06:30 +00:00
|
|
|
const Zip = require('jszip')
|
2018-04-13 16:20:57 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const albumsController = {}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const maxTries = config.uploads.maxTries || 1
|
2018-04-18 21:00:36 +00:00
|
|
|
const homeDomain = config.homeDomain || config.domain
|
2018-04-28 17:26:39 +00:00
|
|
|
const uploadsDir = path.join(__dirname, '..', config.uploads.folder)
|
|
|
|
const zipsDir = path.join(uploadsDir, 'zips')
|
2018-05-09 08:41:30 +00:00
|
|
|
const zipMaxTotalSize = config.cloudflare.zipMaxTotalSize
|
|
|
|
const zipMaxTotalSizeBytes = parseInt(config.cloudflare.zipMaxTotalSize) * 1000000
|
|
|
|
|
|
|
|
albumsController.zipEmitters = new Map()
|
|
|
|
|
|
|
|
class ZipEmitter extends EventEmitter {
|
|
|
|
constructor (identifier) {
|
|
|
|
super()
|
|
|
|
this.identifier = identifier
|
2018-05-09 09:53:27 +00:00
|
|
|
this.once('done', () => albumsController.zipEmitters.delete(this.identifier))
|
2018-05-09 08:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-18 21:00:36 +00:00
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
albumsController.list = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
let fields = ['id', 'name']
|
2018-01-23 20:06:30 +00:00
|
|
|
if (req.params.sidebar === undefined) {
|
2018-04-28 17:26:39 +00:00
|
|
|
fields = fields.concat(fields, ['timestamp', 'identifier', 'editedAt', 'download', 'public'])
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
const albums = await db.table('albums')
|
|
|
|
.select(fields)
|
|
|
|
.where({
|
|
|
|
enabled: 1,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
if (req.params.sidebar !== undefined) {
|
|
|
|
return res.json({ success: true, albums })
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
const ids = []
|
|
|
|
for (const album of albums) {
|
2018-03-30 02:39:53 +00:00
|
|
|
album.date = utils.getPrettyDate(new Date(album.timestamp * 1000))
|
2018-04-28 17:26:39 +00:00
|
|
|
album.download = album.download !== 0
|
|
|
|
album.public = album.public !== 0
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
ids.push(album.id)
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const files = await db.table('files')
|
|
|
|
.whereIn('albumid', ids)
|
|
|
|
.select('albumid')
|
2018-01-23 20:06:30 +00:00
|
|
|
const albumsCount = {}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const id of ids) { albumsCount[id] = 0 }
|
|
|
|
for (const file of files) { albumsCount[file.albumid] += 1 }
|
|
|
|
for (const album of albums) { album.files = albumsCount[album.id] }
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.json({ success: true, albums, homeDomain })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
albumsController.create = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const name = req.body.name
|
|
|
|
if (name === undefined || name === '') {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'No album name specified.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
const album = await db.table('albums')
|
|
|
|
.where({
|
|
|
|
name,
|
|
|
|
enabled: 1,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.first()
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
if (album) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'There\'s already an album with that name.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const identifier = await albumsController.getUniqueRandomName()
|
|
|
|
.catch(error => {
|
|
|
|
res.json({ success: false, description: error.toString() })
|
|
|
|
})
|
|
|
|
if (!identifier) { return }
|
|
|
|
|
2018-05-06 14:14:57 +00:00
|
|
|
const ids = await db.table('albums').insert({
|
2018-03-30 02:39:53 +00:00
|
|
|
name,
|
2018-01-23 20:06:30 +00:00
|
|
|
enabled: 1,
|
|
|
|
userid: user.id,
|
2018-04-28 17:26:39 +00:00
|
|
|
identifier,
|
2018-02-07 16:56:32 +00:00
|
|
|
timestamp: Math.floor(Date.now() / 1000),
|
|
|
|
editedAt: 0,
|
2018-04-28 17:26:39 +00:00
|
|
|
zipGeneratedAt: 0,
|
2018-05-06 14:14:57 +00:00
|
|
|
download: (req.body.download === false || req.body.download === 0) ? 0 : 1,
|
|
|
|
public: (req.body.public === false || req.body.public === 0) ? 0 : 1
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
|
2018-05-06 14:14:57 +00:00
|
|
|
return res.json({ success: true, id: ids[0] })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
albumsController.getUniqueRandomName = () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const select = i => {
|
|
|
|
const identifier = randomstring.generate(config.uploads.albumIdentifierLength)
|
|
|
|
db.table('albums')
|
|
|
|
.where('identifier', identifier)
|
|
|
|
.then(rows => {
|
|
|
|
if (!rows || !rows.length) { return resolve(identifier) }
|
2018-04-30 17:32:50 +00:00
|
|
|
console.log(`An album with identifier ${identifier} already exists (${++i}/${maxTries}).`)
|
2018-04-28 17:26:39 +00:00
|
|
|
if (i < maxTries) { return select(i) }
|
|
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
|
|
|
return reject('Sorry, we could not allocate a unique random identifier. Try again?')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Get us a unique random identifier
|
|
|
|
select(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
albumsController.delete = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const id = req.body.id
|
2018-03-30 02:39:53 +00:00
|
|
|
const purge = req.body.purge
|
2018-01-23 20:06:30 +00:00
|
|
|
if (id === undefined || id === '') {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'No album specified.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
let ids = []
|
2018-05-05 19:44:58 +00:00
|
|
|
let failed = []
|
2018-03-30 02:39:53 +00:00
|
|
|
if (purge) {
|
|
|
|
const files = await db.table('files')
|
|
|
|
.where({
|
|
|
|
albumid: id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
|
|
|
|
ids = files.map(file => file.id)
|
2018-05-05 19:44:58 +00:00
|
|
|
failed = await utils.bulkDeleteFiles('id', ids, user)
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
if (failed.length === ids.length) {
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.json({ success: false, description: 'Could not delete any of the files associated with the album.' })
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.table('albums')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
2018-04-28 17:26:39 +00:00
|
|
|
.update('enabled', 0)
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const identifier = await db.table('albums')
|
|
|
|
.select('identifier')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
.then(row => row.identifier)
|
|
|
|
|
|
|
|
// Unlink zip archive of the album if it exists
|
|
|
|
const zipPath = path.join(zipsDir, `${identifier}.zip`)
|
2018-04-29 12:47:24 +00:00
|
|
|
fs.unlink(zipPath, error => {
|
|
|
|
if (error && error.code !== 'ENOENT') {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-05-05 19:44:58 +00:00
|
|
|
return res.json({ success: false, description: error.toString(), failed })
|
2018-04-29 12:47:24 +00:00
|
|
|
}
|
2018-05-05 19:44:58 +00:00
|
|
|
res.json({ success: true, failed })
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
albumsController.edit = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!user) { return }
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const id = parseInt(req.body.id)
|
|
|
|
if (isNaN(id)) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'No album specified.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const name = req.body.name
|
|
|
|
if (name === undefined || name === '') {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'No name specified.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
const album = await db.table('albums')
|
|
|
|
.where({
|
|
|
|
name,
|
2018-04-28 17:26:39 +00:00
|
|
|
userid: user.id,
|
|
|
|
enabled: 1
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
.first()
|
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
if (album && (album.id !== id)) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Name already in use.' })
|
2018-04-28 17:26:39 +00:00
|
|
|
} else if (req._old && (album.id === id)) {
|
|
|
|
return res.json({ success: false, description: 'You did not specify a new name.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
await db.table('albums')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.update({
|
2018-04-28 17:26:39 +00:00
|
|
|
name,
|
|
|
|
download: Boolean(req.body.download),
|
|
|
|
public: Boolean(req.body.public)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
if (req.body.requestLink) {
|
|
|
|
const oldIdentifier = await db.table('albums')
|
|
|
|
.select('identifier')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
.then(row => row.identifier)
|
|
|
|
|
|
|
|
const identifier = await albumsController.getUniqueRandomName()
|
|
|
|
.catch(error => {
|
|
|
|
res.json({ success: false, description: error.toString() })
|
|
|
|
})
|
|
|
|
if (!identifier) { return }
|
|
|
|
|
|
|
|
await db.table('albums')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.update('identifier', identifier)
|
|
|
|
|
|
|
|
// Rename zip archive of the album if it exists
|
|
|
|
const zipPath = path.join(zipsDir, `${oldIdentifier}.zip`)
|
|
|
|
return fs.access(zipPath, error => {
|
|
|
|
if (error) { return res.json({ success: true, identifier }) }
|
|
|
|
fs.rename(zipPath, path.join(zipsDir, `${identifier}.zip`), error => {
|
|
|
|
if (!error) { return res.json({ success: true, identifier }) }
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-04-28 17:26:39 +00:00
|
|
|
res.json({ success: false, description: error.toString() })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({ success: true, name })
|
|
|
|
}
|
|
|
|
|
|
|
|
albumsController.rename = async (req, res, next) => {
|
|
|
|
req._old = true
|
|
|
|
req.body = { name: req.body.name }
|
|
|
|
return albumsController.edit(req, res, next)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
albumsController.get = async (req, res, next) => {
|
2018-04-28 17:26:39 +00:00
|
|
|
// TODO:
|
2018-01-23 20:06:30 +00:00
|
|
|
const identifier = req.params.identifier
|
2018-04-28 17:26:39 +00:00
|
|
|
if (identifier === undefined) {
|
|
|
|
return res.status(401).json({ success: false, description: 'No identifier provided.' })
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const album = await db.table('albums')
|
|
|
|
.where({
|
|
|
|
identifier,
|
|
|
|
enabled: 1
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!album) {
|
|
|
|
return res.json({ success: false, description: 'Album not found.' })
|
|
|
|
} else if (album.public === 0) {
|
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'This album is not available for public.'
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const title = album.name
|
2018-04-28 17:26:39 +00:00
|
|
|
const files = await db.table('files')
|
|
|
|
.select('name')
|
|
|
|
.where('albumid', album.id)
|
|
|
|
.orderBy('id', 'DESC')
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const file of files) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.file = `${config.domain}/${file.name}`
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const ext = path.extname(file.name).toLowerCase()
|
2018-05-09 08:41:30 +00:00
|
|
|
if ((config.uploads.generateThumbs.image && utils.imageExtensions.includes(ext)) || (config.uploads.generateThumbs.video && utils.videoExtensions.includes(ext))) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -ext.length)}.png`
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 01:17:49 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
return res.json({
|
|
|
|
success: true,
|
2018-03-30 02:39:53 +00:00
|
|
|
title,
|
2018-01-23 20:06:30 +00:00
|
|
|
count: files.length,
|
|
|
|
files
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 05:05:38 +00:00
|
|
|
|
|
|
|
albumsController.generateZip = async (req, res, next) => {
|
2018-04-27 03:03:13 +00:00
|
|
|
const versionString = parseInt(req.query.v)
|
2018-04-26 21:04:21 +00:00
|
|
|
const download = (filePath, fileName) => {
|
|
|
|
const headers = { 'Access-Control-Allow-Origin': '*' }
|
2018-04-27 03:03:13 +00:00
|
|
|
if (versionString > 0) {
|
2018-04-26 21:04:21 +00:00
|
|
|
// Cache-Control header is useful when using CDN (max-age: 30 days)
|
|
|
|
headers['Cache-Control'] = 'public, max-age=2592000, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800'
|
|
|
|
}
|
|
|
|
return res.download(filePath, fileName, { headers })
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const identifier = req.params.identifier
|
2018-04-26 21:04:21 +00:00
|
|
|
if (identifier === undefined) {
|
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'No identifier provided.'
|
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-05-09 08:41:30 +00:00
|
|
|
if (!config.uploads.generateZips) {
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'Zip generation disabled.' })
|
2018-04-26 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const album = await db.table('albums')
|
2018-04-28 17:26:39 +00:00
|
|
|
.where({
|
|
|
|
identifier,
|
|
|
|
enabled: 1
|
|
|
|
})
|
2018-04-26 21:04:21 +00:00
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!album) {
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.json({ success: false, description: 'Album not found.' })
|
|
|
|
} else if (album.download === 0) {
|
|
|
|
return res.json({ success: false, description: 'Download for this album is disabled.' })
|
2018-04-26 21:04:21 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-27 03:03:13 +00:00
|
|
|
if ((!versionString || versionString <= 0) && album.editedAt) {
|
|
|
|
return res.redirect(`${album.identifier}?v=${album.editedAt}`)
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
if (album.zipGeneratedAt > album.editedAt) {
|
2018-04-28 17:26:39 +00:00
|
|
|
const filePath = path.join(zipsDir, `${identifier}.zip`)
|
|
|
|
const exists = await new Promise(resolve => fs.access(filePath, error => resolve(!error)))
|
|
|
|
if (exists) {
|
|
|
|
const fileName = `${album.name}.zip`
|
|
|
|
return download(filePath, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:41:30 +00:00
|
|
|
if (albumsController.zipEmitters.has(identifier)) {
|
|
|
|
console.log(`Waiting previous zip task for album: ${identifier}.`)
|
|
|
|
return albumsController.zipEmitters.get(identifier).once('done', (filePath, fileName, json) => {
|
|
|
|
if (filePath && fileName) {
|
|
|
|
download(filePath, fileName)
|
|
|
|
} else if (json) {
|
|
|
|
res.json(json)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
albumsController.zipEmitters.set(identifier, new ZipEmitter(identifier))
|
|
|
|
|
|
|
|
console.log(`Starting zip task for album: ${identifier}.`)
|
2018-04-28 17:26:39 +00:00
|
|
|
const files = await db.table('files')
|
|
|
|
.select('name', 'size')
|
|
|
|
.where('albumid', album.id)
|
|
|
|
if (files.length === 0) {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.log(`Finished zip task for album: ${identifier} (no files).`)
|
|
|
|
const json = { success: false, description: 'There are no files in the album.' }
|
|
|
|
albumsController.zipEmitters.get(identifier).emit('done', null, null, json)
|
|
|
|
return res.json(json)
|
2018-04-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 08:41:30 +00:00
|
|
|
if (zipMaxTotalSize) {
|
2018-04-28 17:26:39 +00:00
|
|
|
const totalSizeBytes = files.reduce((accumulator, file) => accumulator + parseInt(file.size), 0)
|
2018-05-09 08:41:30 +00:00
|
|
|
if (totalSizeBytes > zipMaxTotalSizeBytes) {
|
|
|
|
console.log(`Finished zip task for album: ${identifier} (size exceeds).`)
|
|
|
|
const json = {
|
2018-04-26 21:04:21 +00:00
|
|
|
success: false,
|
2018-05-09 08:41:30 +00:00
|
|
|
description: `Total size of all files in the album exceeds the configured limit (${zipMaxTotalSize}).`
|
|
|
|
}
|
|
|
|
albumsController.zipEmitters.get(identifier).emit('done', null, null, json)
|
|
|
|
return res.json(json)
|
2018-04-26 21:04:21 +00:00
|
|
|
}
|
2018-04-28 17:26:39 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
const zipPath = path.join(zipsDir, `${album.identifier}.zip`)
|
|
|
|
const archive = new Zip()
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
let iteration = 0
|
|
|
|
for (const file of files) {
|
|
|
|
fs.readFile(path.join(uploadsDir, file.name), (error, data) => {
|
|
|
|
if (error) {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-04-28 17:26:39 +00:00
|
|
|
} else {
|
|
|
|
archive.file(file.name, data)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
iteration++
|
|
|
|
if (iteration === files.length) {
|
|
|
|
archive
|
|
|
|
.generateNodeStream({
|
|
|
|
type: 'nodebuffer',
|
|
|
|
streamFiles: true,
|
|
|
|
compression: 'DEFLATE',
|
|
|
|
compressionOptions: { level: 1 }
|
|
|
|
})
|
|
|
|
.pipe(fs.createWriteStream(zipPath))
|
|
|
|
.on('finish', async () => {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.log(`Finished zip task for album: ${identifier} (success).`)
|
2018-04-28 17:26:39 +00:00
|
|
|
await db.table('albums')
|
|
|
|
.where('id', album.id)
|
|
|
|
.update('zipGeneratedAt', Math.floor(Date.now() / 1000))
|
|
|
|
|
|
|
|
const filePath = path.join(zipsDir, `${identifier}.zip`)
|
|
|
|
const fileName = `${album.name}.zip`
|
2018-05-09 08:41:30 +00:00
|
|
|
|
|
|
|
albumsController.zipEmitters.get(identifier).emit('done', filePath, fileName)
|
2018-04-28 17:26:39 +00:00
|
|
|
return download(filePath, fileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
albumsController.addFiles = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
if (!user) { return }
|
|
|
|
|
|
|
|
const ids = req.body.ids
|
2018-04-20 21:39:06 +00:00
|
|
|
if (!ids || !ids.length) {
|
2018-03-30 02:39:53 +00:00
|
|
|
return res.json({ success: false, description: 'No files specified.' })
|
|
|
|
}
|
|
|
|
|
|
|
|
let albumid = req.body.albumid
|
|
|
|
if (typeof albumid !== 'number') { albumid = parseInt(albumid) }
|
2018-04-03 15:01:19 +00:00
|
|
|
if (isNaN(albumid) || (albumid < 0)) { albumid = null }
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
const albumids = []
|
2018-04-05 03:10:10 +00:00
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
if (albumid !== null) {
|
|
|
|
const album = await db.table('albums')
|
|
|
|
.where({
|
|
|
|
id: albumid,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!album) {
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.json({ success: false, description: 'Album doesn\'t exist or it doesn\'t belong to the user.' })
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
2018-04-05 03:10:10 +00:00
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
albumids.push(albumid)
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const files = await db.table('files')
|
|
|
|
.whereIn('id', ids)
|
|
|
|
.where(function () {
|
|
|
|
if (user.username !== 'root') {
|
|
|
|
this.where('userid', user.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
const failed = ids.filter(id => !files.find(file => file.id === id))
|
2018-03-30 02:39:53 +00:00
|
|
|
|
|
|
|
await Promise.all(files.map(file => {
|
2018-04-05 12:54:24 +00:00
|
|
|
if (file.albumid && !albumids.includes(file.albumid)) {
|
|
|
|
albumids.push(file.albumid)
|
2018-04-05 03:10:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 02:39:53 +00:00
|
|
|
return db.table('files')
|
|
|
|
.where('id', file.id)
|
|
|
|
.update('albumid', albumid)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
2018-05-05 19:44:58 +00:00
|
|
|
failed.push(file.id)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
if (failed.length < ids.length) {
|
2018-04-05 12:54:24 +00:00
|
|
|
await Promise.all(albumids.map(albumid => {
|
2018-04-05 03:10:10 +00:00
|
|
|
return db.table('albums')
|
2018-03-30 02:39:53 +00:00
|
|
|
.where('id', albumid)
|
|
|
|
.update('editedAt', Math.floor(Date.now() / 1000))
|
2018-04-05 03:10:10 +00:00
|
|
|
}))
|
2018-04-20 21:39:06 +00:00
|
|
|
|
2018-05-05 19:44:58 +00:00
|
|
|
return res.json({ success: true, failed })
|
2018-03-30 02:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
2018-05-05 19:44:58 +00:00
|
|
|
description: `Could not ${albumid === null ? 'add' : 'remove'} any files ${albumid === null ? 'to' : 'from'} the album.`
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = albumsController
|