aboutsummaryrefslogtreecommitdiff
path: root/routes
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 /routes
parentLittle bit of this (diff)
downloadhost.fuwn.me-702075b66dcc22bfa5e018f6a764ab23c8f9a9f5.tar.xz
host.fuwn.me-702075b66dcc22bfa5e018f6a764ab23c8f9a9f5.zip
ES6 rewrite
Diffstat (limited to 'routes')
-rw-r--r--routes/album.js107
-rw-r--r--routes/api.js66
2 files changed, 79 insertions, 94 deletions
diff --git a/routes/album.js b/routes/album.js
index 20ccc26..ab1ddfa 100644
--- a/routes/album.js
+++ b/routes/album.js
@@ -1,61 +1,50 @@
-const config = require('../config.js')
-const routes = require('express').Router()
-const db = require('knex')(config.database)
-const path = require('path')
-const utils = require('../controllers/utilsController.js')
-
-routes.get('/a/:identifier', (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 thumb = ''
- 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'
-
- /*
- If thumbnail for album is still not set, do it.
- A potential improvement would be to let the user upload a specific image as an album cover
- since embedding the first image could potentially result in nsfw content when pasting links.
- */
-
- if (thumb === '') {
- thumb = file.thumb
- }
-
- file.thumb = `<img src="${file.thumb}"/>`
- } else {
- file.thumb = `<h1 class="title">.${ext}</h1>`
- }
+const config = require('../config.js');
+const routes = require('express').Router();
+const db = require('knex')(config.database);
+const path = require('path');
+const utils = require('../controllers/utilsController.js');
+
+routes.get('/a/:identifier', async (req, res, next) => {
+ let 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 files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC');
+ let thumb = '';
+ const basedomain = config.domain;
+
+ 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`;
+
+ /*
+ If thumbnail for album is still not set, do it.
+ A potential improvement would be to let the user upload a specific image as an album cover
+ since embedding the first image could potentially result in nsfw content when pasting links.
+ */
+
+ if (thumb === '') {
+ thumb = file.thumb;
}
- return res.render('album', {
- layout: false,
- title: title,
- count: files.length,
- thumb,
- 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 = routes
+ file.thumb = `<img src="${file.thumb}"/>`;
+ } else {
+ file.thumb = `<h1 class="title">.${ext}</h1>`;
+ }
+ }
+
+ return res.render('album', {
+ layout: false,
+ title: album.name,
+ count: files.length,
+ thumb,
+ files
+ });
+});
+
+module.exports = routes;
diff --git a/routes/api.js b/routes/api.js
index b3ff798..83d3bc1 100644
--- a/routes/api.js
+++ b/routes/api.js
@@ -1,40 +1,36 @@
-const config = require('../config.js')
-const routes = require('express').Router()
-const uploadController = require('../controllers/uploadController')
-const albumsController = require('../controllers/albumsController')
-const tokenController = require('../controllers/tokenController')
-const authController = require('../controllers/authController')
+const config = require('../config.js');
+const routes = require('express').Router();
+const uploadController = require('../controllers/uploadController');
+const albumsController = require('../controllers/albumsController');
+const tokenController = require('../controllers/tokenController');
+const authController = require('../controllers/authController');
-routes.get ('/check', (req, res, next) => {
- return res.json({
+routes.get('/check', (req, res, next) => {
+ return res.json({
private: config.private,
maxFileSize: config.uploads.maxSize
- })
-})
+ });
+});
-routes.post ('/login', (req, res, next) => authController.verify(req, res, next))
-routes.post ('/register', (req, res, next) => authController.register(req, res, next))
-routes.post ('/password/change', (req, res, next) => authController.changePassword(req, res, next))
+routes.post('/login', (req, res, next) => authController.verify(req, res, next));
+routes.post('/register', (req, res, next) => authController.register(req, res, next));
+routes.post('/password/change', (req, res, next) => authController.changePassword(req, res, next));
+routes.get('/uploads', (req, res, next) => uploadController.list(req, res, next));
+routes.get('/uploads/:page', (req, res, next) => uploadController.list(req, res, next));
+routes.post('/upload', (req, res, next) => uploadController.upload(req, res, next));
+routes.post('/upload/delete', (req, res, next) => uploadController.delete(req, res, next));
+routes.post('/upload/:albumid', (req, res, next) => uploadController.upload(req, res, next));
+routes.get('/album/get/:identifier', (req, res, next) => albumsController.get(req, res, next));
+routes.get('/album/:id', (req, res, next) => uploadController.list(req, res, next));
+routes.get('/album/:id/:page', (req, res, next) => uploadController.list(req, res, next));
+routes.get('/albums', (req, res, next) => albumsController.list(req, res, next));
+routes.get('/albums/:sidebar', (req, res, next) => albumsController.list(req, res, next));
+routes.post('/albums', (req, res, next) => albumsController.create(req, res, next));
+routes.post('/albums/delete', (req, res, next) => albumsController.delete(req, res, next));
+routes.post('/albums/rename', (req, res, next) => albumsController.rename(req, res, next));
+routes.get('/albums/test', (req, res, next) => albumsController.test(req, res, next));
+routes.get('/tokens', (req, res, next) => tokenController.list(req, res, next));
+routes.post('/tokens/verify', (req, res, next) => tokenController.verify(req, res, next));
+routes.post('/tokens/change', (req, res, next) => tokenController.change(req, res, next));
-routes.get ('/uploads', (req, res, next) => uploadController.list(req, res))
-routes.get ('/uploads/:page', (req, res, next) => uploadController.list(req, res))
-routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, next))
-routes.post ('/upload/delete', (req, res, next) => uploadController.delete(req, res, next))
-routes.post ('/upload/:albumid', (req, res, next) => uploadController.upload(req, res, next))
-
-routes.get ('/album/get/:identifier', (req, res, next) => albumsController.get(req, res, next))
-routes.get ('/album/:id', (req, res, next) => uploadController.list(req, res, next))
-routes.get ('/album/:id/:page', (req, res, next) => uploadController.list(req, res, next))
-
-routes.get ('/albums', (req, res, next) => albumsController.list(req, res, next))
-routes.get ('/albums/:sidebar', (req, res, next) => albumsController.list(req, res, next))
-routes.post ('/albums', (req, res, next) => albumsController.create(req, res, next))
-routes.post ('/albums/delete', (req, res, next) => albumsController.delete(req, res, next))
-routes.post ('/albums/rename', (req, res, next) => albumsController.rename(req, res, next))
-routes.get ('/albums/test', (req, res, next) => albumsController.test(req, res, next))
-
-routes.get ('/tokens', (req, res, next) => tokenController.list(req, res))
-routes.post ('/tokens/verify', (req, res, next) => tokenController.verify(req, res))
-routes.post ('/tokens/change', (req, res, next) => tokenController.change(req, res))
-
-module.exports = routes
+module.exports = routes;