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)
|
|
|
|
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-18 21:00:36 +00:00
|
|
|
const homeDomain = config.homeDomain || config.domain
|
|
|
|
|
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-01-23 20:06:30 +00:00
|
|
|
const fields = ['id', 'name']
|
|
|
|
if (req.params.sidebar === undefined) {
|
|
|
|
fields.push('timestamp')
|
|
|
|
fields.push('identifier')
|
|
|
|
}
|
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))
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-18 21:00:36 +00:00
|
|
|
album.identifier = `${homeDomain}/a/${album.identifier}`
|
2018-01-23 20:06:30 +00:00
|
|
|
ids.push(album.id)
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const files = await db.table('files').whereIn('albumid', ids).select('albumid')
|
|
|
|
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-01-23 20:06:30 +00:00
|
|
|
return res.json({ success: true, albums })
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
identifier: randomstring.generate(8),
|
2018-02-07 16:56:32 +00:00
|
|
|
timestamp: Math.floor(Date.now() / 1000),
|
|
|
|
editedAt: 0,
|
|
|
|
zipGeneratedAt: 0
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
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-04-05 12:54:24 +00:00
|
|
|
let failedids = []
|
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-04-05 12:54:24 +00:00
|
|
|
failedids = await utils.bulkDeleteFilesByIds(ids, user)
|
2018-03-30 02:39:53 +00:00
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
if (failedids.length === ids.length) {
|
2018-03-30 02:39:53 +00:00
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'Could not delete any of the files associated with the album.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.table('albums')
|
|
|
|
.where({
|
|
|
|
id,
|
|
|
|
userid: user.id
|
|
|
|
})
|
|
|
|
.update({ enabled: 0 })
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
2018-04-05 12:54:24 +00:00
|
|
|
failedids
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
albumsController.rename = 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
|
|
|
|
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-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,
|
|
|
|
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: 'Name already in use.' })
|
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({
|
|
|
|
name
|
|
|
|
})
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
albumsController.get = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const identifier = req.params.identifier
|
2018-03-28 17:40:50 +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-01-23 20:06:30 +00:00
|
|
|
const album = await db.table('albums').where({ identifier, enabled: 1 }).first()
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!album) { return res.json({ success: false, description: 'Album not found.' }) }
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const title = album.name
|
|
|
|
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-03-24 13:52:47 +00:00
|
|
|
if ((config.uploads.generateThumbnails.image && utils.imageExtensions.includes(ext)) || (config.uploads.generateThumbnails.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-26 21:04:21 +00:00
|
|
|
const download = (filePath, fileName) => {
|
|
|
|
const headers = { 'Access-Control-Allow-Origin': '*' }
|
|
|
|
// Album page will append zipGeneratedAt timestamp to the download link by default
|
|
|
|
if (parseInt(req.query.v) > 0) {
|
|
|
|
// 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-04-26 21:04:21 +00:00
|
|
|
if (!config.uploads.generateZips) {
|
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'Zip generation disabled.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const album = await db.table('albums')
|
|
|
|
.where({ identifier, enabled: 1 })
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!album) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'Album not found.'
|
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
if (album.zipGeneratedAt > album.editedAt) {
|
|
|
|
const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`)
|
|
|
|
const fileName = `${album.name}.zip`
|
2018-04-26 21:04:21 +00:00
|
|
|
return download(filePath, fileName)
|
2018-01-23 20:06:30 +00:00
|
|
|
} else {
|
|
|
|
console.log(`Generating zip for album identifier: ${identifier}`)
|
2018-03-30 02:39:53 +00:00
|
|
|
const files = await db.table('files')
|
|
|
|
.select('name')
|
|
|
|
.where('albumid', album.id)
|
2018-04-26 21:04:21 +00:00
|
|
|
if (files.length === 0) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'There are no files in the album.'
|
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const zipPath = path.join(__dirname, '..', config.uploads.folder, 'zips', `${album.identifier}.zip`)
|
2018-04-05 10:52:57 +00:00
|
|
|
const archive = new Zip()
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const file of files) {
|
2018-01-23 20:06:30 +00:00
|
|
|
try {
|
|
|
|
// const exists = fs.statSync(path.join(__dirname, '..', config.uploads.folder, file.name))
|
|
|
|
archive.file(file.name, fs.readFileSync(path.join(__dirname, '..', config.uploads.folder, file.name)))
|
2018-03-29 23:22:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
archive
|
2018-04-26 21:04:21 +00:00
|
|
|
.generateNodeStream({
|
|
|
|
type: 'nodebuffer',
|
|
|
|
streamFiles: true
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
.pipe(fs.createWriteStream(zipPath))
|
|
|
|
.on('finish', async () => {
|
|
|
|
console.log(`Generated zip for album identifier: ${identifier}`)
|
|
|
|
await db.table('albums')
|
|
|
|
.where('id', album.id)
|
|
|
|
.update({ zipGeneratedAt: Math.floor(Date.now() / 1000) })
|
|
|
|
|
|
|
|
const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`)
|
|
|
|
const fileName = `${album.name}.zip`
|
2018-04-26 21:04:21 +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) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: 'Album doesn\'t exist or it doesn\'t belong to the user.'
|
|
|
|
})
|
|
|
|
}
|
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-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
|
|
|
|
|
|
|
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-04-05 12:54:24 +00:00
|
|
|
failedids.push(file.id)
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2018-04-05 12:54:24 +00:00
|
|
|
if (failedids.length < ids.length) {
|
|
|
|
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-03-30 02:39:53 +00:00
|
|
|
return res.json({
|
|
|
|
success: true,
|
2018-04-05 12:54:24 +00:00
|
|
|
failedids
|
2018-03-30 02:39:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
description: `Could not ${albumid === null ? 'add' : 'remove'} any of the selected files ${albumid === null ? 'to' : 'from'} the album.`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = albumsController
|