aboutsummaryrefslogtreecommitdiff
path: root/controllers/albumsController.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2017-10-03 21:13:38 -0300
committerPitu <[email protected]>2017-10-03 21:13:38 -0300
commit702075b66dcc22bfa5e018f6a764ab23c8f9a9f5 (patch)
tree31c6e5107f916617be3ffdb072210e84f92d9c56 /controllers/albumsController.js
parentLittle bit of this (diff)
downloadhost.fuwn.me-702075b66dcc22bfa5e018f6a764ab23c8f9a9f5.tar.xz
host.fuwn.me-702075b66dcc22bfa5e018f6a764ab23c8f9a9f5.zip
ES6 rewrite
Diffstat (limited to 'controllers/albumsController.js')
-rw-r--r--controllers/albumsController.js301
1 files changed, 129 insertions, 172 deletions
diff --git a/controllers/albumsController.js b/controllers/albumsController.js
index 7b9cc46..7c9c7da 100644
--- a/controllers/albumsController.js
+++ b/controllers/albumsController.js
@@ -1,175 +1,132 @@
-const config = require('../config.js')
-const db = require('knex')(config.database)
-const randomstring = require('randomstring')
-const utils = require('./utilsController.js')
-const path = require('path')
-
-let albumsController = {}
-
-albumsController.list = function(req, res, next) {
-
- let token = req.headers.token
- if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
-
- db.table('users').where('token', token).then((user) => {
- if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
-
- let fields = ['id', 'name']
-
- if (req.params.sidebar === undefined) {
- fields.push('timestamp')
- fields.push('identifier')
+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 albumsController = {};
+
+albumsController.list = async (req, res, next) => {
+ const user = await utils.authorize(req, res);
+
+ const fields = ['id', 'name'];
+ if (req.params.sidebar === undefined) {
+ fields.push('timestamp');
+ fields.push('identifier');
+ }
+
+ 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 });
+ }
+
+ let ids = [];
+ for (let album of albums) {
+ album.date = new Date(album.timestamp * 1000)
+ album.date = utils.getPrettyDate(album.date)
+
+ album.identifier = `${config.domain}/a/${album.identifier}`;
+ ids.push(album.id);
+ }
+
+ const files = await db.table('files').whereIn('albumid', ids).select('albumid');
+ const albumsCount = {};
+
+ 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];
+
+ return res.json({ success: true, albums });
+};
+
+albumsController.create = async (req, res, next) => {
+ const user = await utils.authorize(req, res);
+
+ const name = req.body.name;
+ if (name === undefined || name === '') {
+ return res.json({ success: false, description: 'No album name specified' });
+ }
+
+ const album = await db.table('albums').where({
+ name: name,
+ enabled: 1,
+ userid: user.id
+ }).first();
+
+ if (album) {
+ return res.json({ success: false, description: 'There\'s already an album with that name' })
+ }
+
+ await db.table('albums').insert({
+ name: name,
+ enabled: 1,
+ userid: user.id,
+ identifier: randomstring.generate(8),
+ timestamp: Math.floor(Date.now() / 1000)
+ });
+
+ return res.json({ success: true });
+};
+
+albumsController.delete = async (req, res, next) => {
+ const user = await utils.authorize(req, res);
+
+ const id = req.body.id;
+ if (id === undefined || id === '') {
+ return res.json({ success: false, description: 'No album specified' });
+ }
+
+ await db.table('albums').where({ id: id, userid: user.id }).update({ enabled: 0 });
+ return res.json({ success: true });
+};
+
+albumsController.rename = async (req, res, next) => {
+ const user = await utils.authorize(req, res);
+
+ const id = req.body.id;
+ if (id === undefined || id === '') {
+ return res.json({ success: false, description: 'No album specified' });
+ }
+
+ const name = req.body.name;
+ if (name === undefined || name === '') {
+ return res.json({ success: false, description: 'No name specified' });
+ }
+
+ const album = await db.table('albums').where({ name: name, userid: user.id }).first();
+ if (album) {
+ return res.json({ success: false, description: 'Name already in use' })
+ }
+
+ await db.table('albums').where({ id: id, userid: user.id }).update({ name: name })
+ return res.json({ success: true });
+};
+
+albumsController.get = async (req, res, next) => {
+ const identifier = req.params.identifier;
+ if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' });
+
+ const album = await db.table('albums').where('identifier', identifier).first();
+ if (!album) return res.json({ success: false, description: 'Album not found' });
+
+ const title = album.name;
+ const files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC');
+
+ for (let file of files) {
+ file.file = `${config.domain}/${file.name}`;
+
+ const ext = path.extname(file.name).toLowerCase();
+ if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) {
+ file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -ext.length)}.png`;
}
+ }
- 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 = []
- for (let album of albums) {
- album.date = new Date(album.timestamp * 1000)
- 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()
-
- let basedomain = req.get('host')
- for (let domain of config.domains)
- if (domain.host === req.get('host'))
- if (domain.hasOwnProperty('resolve'))
- basedomain = domain.resolve
-
- album.identifier = basedomain + '/a/' + album.identifier
-
- ids.push(album.id)
- }
-
- db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
-
- let albumsCount = {}
-
- 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]
-
- return res.json({ success: true, albums })
- }).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' }) })
-
-}
-
-albumsController.create = function(req, res, next) {
- let token = req.headers.token
- if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
-
- db.table('users').where('token', token).then((user) => {
- if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
-
- let name = req.body.name
- if (name === undefined || name === '')
- return res.json({ success: false, description: 'No album name specified' })
-
- db.table('albums').where({
- name: name,
- enabled: 1,
- userid: user[0].id
- }).then((album) => {
- 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,
- userid: user[0].id,
- identifier: randomstring.generate(8),
- timestamp: Math.floor(Date.now() / 1000)
- }).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' }) })
-
-}
-
-albumsController.delete = function(req, res, next) {
- let token = req.headers.token
- if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
-
- db.table('users').where('token', token).then((user) => {
- if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
-
- let id = req.body.id
- if (id === undefined || id === ''){
- return res.json({ success: false, description: 'No album specified' })
- }
+ return res.json({
+ success: true,
+ title: title,
+ count: files.length,
+ files
+ });
+};
- 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' }) })
-}
-
-albumsController.rename = function(req, res, next) {
- let token = req.headers.token
- if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
-
- db.table('users').where('token', token).then((user) => {
- if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
-
- let id = req.body.id
- if (id === undefined || id === '')
- return res.json({ success: false, description: 'No album specified' })
-
- let name = req.body.name
- if (name === undefined || name === '')
- return res.json({ success: false, description: 'No name specified' })
-
- 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' })
-
- 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' }) })
-
-}
-
-albumsController.get = function(req, res, next) {
- let identifier = req.params.identifier
- if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
-
- db.table('albums')
- .where('identifier', identifier)
- .then((albums) => {
- if (albums.length === 0) return res.json({ success: false, description: 'Album not found' })
-
- 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')
- for (let domain of config.domains)
- if (domain.host === req.get('host'))
- if (domain.hasOwnProperty('resolve'))
- basedomain = domain.resolve
-
- for (let file of files) {
- file.file = basedomain + '/' + file.name
-
- let ext = path.extname(file.name).toLowerCase()
- if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) {
- file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
- utils.generateThumbs(file)
- }
- }
-
- return res.json({
- success: true,
- title: title,
- count: files.length,
- files
- })
-
- }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
- }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
-}
-
-module.exports = albumsController
+module.exports = albumsController;