filesafe/controllers/albumsController.js

171 lines
6.6 KiB
JavaScript
Raw Normal View History

2017-01-17 21:55:35 +00:00
const config = require('../config.js')
const db = require('knex')(config.database)
2017-02-07 07:32:55 +00:00
const randomstring = require('randomstring')
2017-03-17 04:14:24 +00:00
const utils = require('utilsController.js')
2017-01-17 21:55:35 +00:00
let albumsController = {}
2017-03-17 04:14:24 +00:00
albumsController.list = function(req, res, next) {
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-17 21:55:35 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
2017-01-18 05:40:14 +00:00
let fields = ['id', 'name']
2017-03-17 04:14:24 +00:00
if (req.params.sidebar === undefined) {
fields.push('timestamp')
2017-02-07 07:52:53 +00:00
fields.push('identifier')
}
2017-03-17 04:14:24 +00:00
db.table('albums').select(fields).where({ enabled: 1, userid: user[0].id }).then((albums) => {
if (req.params.sidebar !== undefined)
return res.json({ success: true, albums })
let ids = []
2017-03-17 04:14:24 +00:00
for (let album of albums) {
album.date = new Date(album.timestamp * 1000)
2017-03-17 04:14:24 +00:00
album.date = utils.getPrettyDate(album.date) // album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
2017-02-07 07:52:53 +00:00
let basedomain = req.get('host')
2017-03-17 04:14:24 +00:00
for (let domain of config.domains)
if (domain.host === req.get('host'))
if (domain.hasOwnProperty('resolve'))
2017-02-07 07:52:53 +00:00
basedomain = domain.resolve
2017-02-07 07:54:58 +00:00
album.identifier = basedomain + '/a/' + album.identifier
2017-02-07 07:52:53 +00:00
ids.push(album.id)
}
2017-01-18 05:40:14 +00:00
db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
2017-01-18 05:40:14 +00:00
let albumsCount = {}
2017-03-17 04:14:24 +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-01-18 05:40:14 +00:00
return res.json({ success: true, albums })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-17 21:55:35 +00:00
}
2017-03-17 04:14:24 +00:00
albumsController.create = function(req, res, next) {
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-17 21:55:35 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
2017-01-18 05:40:14 +00:00
let name = req.body.name
2017-03-17 04:14:24 +00:00
if (name === undefined || name === '')
return res.json({ success: false, description: 'No album name specified' })
2017-01-17 21:55:35 +00:00
db.table('albums').where({
name: name,
enabled: 1,
2017-01-30 07:42:15 +00:00
userid: user[0].id
}).then((album) => {
2017-03-17 04:14:24 +00:00
if (album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
db.table('albums').insert({
name: name,
enabled: 1,
2017-01-30 07:42:15 +00:00
userid: user[0].id,
2017-02-07 07:32:55 +00:00
identifier: randomstring.generate(8),
timestamp: Math.floor(Date.now() / 1000)
}).then(() => {
2017-03-17 04:14:24 +00:00
return res.json({ success: true })
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-17 21:55:35 +00:00
}
2017-03-17 04:14:24 +00:00
albumsController.delete = function(req, res, next) {
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-19 20:14:28 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
2017-01-19 20:14:28 +00:00
let id = req.body.id
2017-03-17 04:14:24 +00:00
if (id === undefined || id === ''){
return res.json({ success: false, description: 'No album specified' })
2017-03-17 04:14:24 +00:00
}
2017-03-17 04:14:24 +00:00
db.table('albums').where({ id: id, userid: user[0].id }).update({ enabled: 0 }).then(() => {
return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-19 20:14:28 +00:00
}
2017-03-17 04:14:24 +00:00
albumsController.rename = function(req, res, next) {
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
let id = req.body.id
2017-03-17 04:14:24 +00:00
if (id === undefined || id === '')
return res.json({ success: false, description: 'No album specified' })
let name = req.body.name
2017-03-17 04:14:24 +00:00
if (name === undefined || name === '')
return res.json({ success: false, description: 'No name specified' })
2017-03-17 04:14:24 +00:00
db.table('albums').where({ name: name, userid: user[0].id }).then((results) => {
if (results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
2017-03-17 04:14:24 +00:00
db.table('albums').where({ id: id, userid: user[0].id }).update({ name: name }).then(() => {
return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}
2017-01-18 05:40:14 +00:00
2017-03-17 04:14:24 +00:00
albumsController.get = function(req, res, next) {
2017-02-07 07:32:55 +00:00
let identifier = req.params.identifier
2017-03-17 04:14:24 +00:00
if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
2017-02-07 07:32:55 +00:00
db.table('albums')
.where('identifier', identifier)
.then((albums) => {
2017-03-17 04:14:24 +00:00
if (albums.length === 0) return res.json({ success: false, description: 'Album not found' })
2017-02-07 07:32:55 +00:00
let title = albums[0].name
db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
let basedomain = req.get('host')
2017-03-17 04:14:24 +00:00
for (let domain of config.domains)
if (domain.host === req.get('host'))
if (domain.hasOwnProperty('resolve'))
2017-02-07 07:32:55 +00:00
basedomain = domain.resolve
2017-03-17 04:14:24 +00:00
for (let file of files) {
2017-02-07 07:32:55 +00:00
file.file = basedomain + '/' + file.name
2017-03-17 04:14:24 +00:00
utils.generateThumbs(file)
2017-02-07 07:32:55 +00:00
}
return res.json({
success: true,
title: title,
2017-02-07 07:52:53 +00:00
count: files.length,
2017-02-07 07:32:55 +00:00
files
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-02-07 07:32:55 +00:00
}
2017-03-17 04:14:24 +00:00
module.exports = albumsController