2018-01-23 20:06:30 +00:00
|
|
|
const config = require('../config.js')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const randomstring = require('randomstring')
|
|
|
|
const utils = require('./utilsController.js')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs')
|
|
|
|
const Zip = require('jszip')
|
|
|
|
const albumsController = {}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
albumsController.list = async (req, res, next) => {
|
2018-02-07 06:45:18 +00:00
|
|
|
const albumDomain = config.albumDomain || config.domain
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-03-24 19:47:41 +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-01-23 20:06:30 +00:00
|
|
|
const albums = await db.table('albums').select(fields).where({ enabled: 1, userid: user.id })
|
|
|
|
if (req.params.sidebar !== undefined) {
|
|
|
|
return res.json({ success: true, albums })
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
let ids = []
|
|
|
|
for (let album of albums) {
|
|
|
|
album.date = new Date(album.timestamp * 1000)
|
|
|
|
album.date = utils.getPrettyDate(album.date)
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-02-07 06:45:18 +00:00
|
|
|
album.identifier = `${albumDomain}/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-01-23 20:06:30 +00:00
|
|
|
for (let id of ids) albumsCount[id] = 0
|
|
|
|
for (let file of files) albumsCount[file.albumid] += 1
|
|
|
|
for (let 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-24 19:47:41 +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
|
|
|
}
|
|
|
|
|
|
|
|
const album = await db.table('albums').where({
|
|
|
|
name: name,
|
|
|
|
enabled: 1,
|
|
|
|
userid: user.id
|
|
|
|
}).first()
|
|
|
|
|
|
|
|
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({
|
|
|
|
name: name,
|
|
|
|
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-24 19:47:41 +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
|
|
|
await db.table('albums').where({ id: id, userid: user.id }).update({ enabled: 0 })
|
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
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-24 19:47:41 +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-01-23 20:06:30 +00:00
|
|
|
const album = await db.table('albums').where({ name: name, userid: user.id }).first()
|
|
|
|
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-01-23 20:06:30 +00:00
|
|
|
await db.table('albums').where({ id: id, userid: user.id }).update({ name: name })
|
|
|
|
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-24 13:52:47 +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-24 13:52:47 +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-01-23 20:06:30 +00:00
|
|
|
for (let file of files) {
|
|
|
|
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,
|
|
|
|
title: title,
|
|
|
|
count: files.length,
|
|
|
|
files
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 05:05:38 +00:00
|
|
|
|
|
|
|
albumsController.generateZip = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const identifier = req.params.identifier
|
2018-03-24 13:52:47 +00:00
|
|
|
if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided.' })
|
|
|
|
if (!config.uploads.generateZips) return res.status(401).json({ success: false, description: 'Zip generation disabled.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const album = await db.table('albums').where({ identifier, enabled: 1 }).first()
|
2018-03-24 13:52:47 +00:00
|
|
|
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`
|
|
|
|
return res.download(filePath, fileName)
|
|
|
|
} else {
|
|
|
|
console.log(`Generating zip for album identifier: ${identifier}`)
|
|
|
|
const files = await db.table('files').select('name').where('albumid', album.id)
|
2018-03-24 13:52:47 +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`)
|
|
|
|
let archive = new Zip()
|
|
|
|
|
|
|
|
for (let file of files) {
|
|
|
|
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)))
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
archive
|
|
|
|
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
|
|
|
|
.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`
|
|
|
|
return res.download(filePath, fileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = albumsController
|