From 7b9fca0bc3320a91db1b8d37f302cd3951346b2e Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 22 Jul 2022 01:44:15 +0700 Subject: [PATCH] refactor: req.params -> .path_parameters direct hyper-express prop get --- controllers/albumsController.js | 80 ++++++++++++++++----------------- controllers/authController.js | 2 +- controllers/uploadController.js | 22 +++++---- routes/album.js | 2 +- routes/api.js | 4 +- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 5abcb95..00fa3fe 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -106,7 +106,7 @@ self.list = async (req, res) => { return res.json({ success: true, albums, count }) } else { - let offset = Number(req.params.page) + let offset = Number(req.path_parameters.page) if (isNaN(offset)) offset = 0 else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) @@ -431,7 +431,7 @@ self.rename = async (req, res) => { } self.get = async (req, res) => { - const identifier = req.params.identifier + const identifier = req.path_parameters.identifier if (identifier === undefined) { throw new ClientError('No identifier provided.') } @@ -454,7 +454,7 @@ self.get = async (req, res) => { .orderBy('id', 'desc') for (const file of files) { - if (req._upstreamCompat) { + if (req.locals.upstreamCompat) { file.url = `${utils.conf.domain}/${file.name}` } else { file.file = `${utils.conf.domain}/${file.name}` @@ -463,7 +463,9 @@ self.get = async (req, res) => { const extname = utils.extname(file.name) if (utils.mayGenerateThumb(extname)) { file.thumb = `${utils.conf.domain}/thumbs/${file.name.slice(0, -extname.length)}.png` - if (req._upstreamCompat) file.thumbSquare = file.thumb + if (req.locals.upstreamCompat) { + file.thumbSquare = file.thumb + } } } @@ -477,10 +479,42 @@ self.get = async (req, res) => { }) } +self.getUpstreamCompat = async (req, res) => { + // If requested via /api/album/:identifier, + // map to .get() with chibisafe/upstream compatibility + // This API is known to be used in Pitu/Magane + req.locals.upstreamCompat = true + res._json = res.json + res.json = (body = {}) => { + // Rebuild JSON payload to match lolisafe upstream + const rebuild = {} + const maps = { + success: null, + description: 'message', + title: 'name', + download: 'downloadEnabled', + count: null + } + + Object.keys(body).forEach(key => { + if (maps[key] !== undefined) { + if (maps[key]) rebuild[maps[key]] = body[key] + } else { + rebuild[key] = body[key] + } + }) + + if (rebuild.message) rebuild.message = rebuild.message.replace(/\.$/, '') + return res._json(rebuild) + } + + return self.get(req, res) +} + self.generateZip = async (req, res) => { const versionString = parseInt(req.query.v) - const identifier = req.params.identifier + const identifier = req.path_parameters.identifier if (identifier === undefined) { throw new ClientError('No identifier provided.') } @@ -593,42 +627,6 @@ self.generateZip = async (req, res) => { return res.download(filePath, fileName) } -self.listFiles = async (req, res) => { - if (req.params.page === undefined) { - // Map to /api/album/get, but with lolisafe upstream compatibility, when accessed with this API route - req.params.identifier = req.params.id - delete req.params.id - - req._upstreamCompat = true - res._json = res.json - res.json = (body = {}) => { - // Rebuild JSON payload to match lolisafe upstream - const rebuild = {} - const maps = { - success: null, - description: 'message', - title: 'name', - download: 'downloadEnabled', - count: null - } - - Object.keys(body).forEach(key => { - if (maps[key] !== undefined) { - if (maps[key]) rebuild[maps[key]] = body[key] - } else { - rebuild[key] = body[key] - } - }) - - if (rebuild.message) rebuild.message = rebuild.message.replace(/\.$/, '') - return res._json(rebuild) - } - return self.get(req, res) - } else { - return uploadController.list(req, res) - } -} - self.addFiles = async (req, res) => { const user = await utils.authorize(req) diff --git a/controllers/authController.js b/controllers/authController.js index 9046baa..c616f6b 100644 --- a/controllers/authController.js +++ b/controllers/authController.js @@ -363,7 +363,7 @@ self.listUsers = async (req, res) => { return res.json({ success: true, users: [], count }) } - let offset = Number(req.params.page) + let offset = Number(req.path_parameters.page) if (isNaN(offset)) offset = 0 else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) diff --git a/controllers/uploadController.js b/controllers/uploadController.js index c3a2cd8..c740b51 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -210,7 +210,7 @@ self.upload = async (req, res) => { } } - let albumid = parseInt(req.headers.albumid || req.params.albumid) + let albumid = parseInt(req.headers.albumid || req.path_parameters.albumid) if (isNaN(albumid)) albumid = null const age = self.assertRetentionPeriod(user, req.headers.age) @@ -1161,7 +1161,9 @@ self.list = async (req, res) => { if (filters) { const keywords = [] - if (req.params.id === undefined) keywords.push('albumid') + if (req.path_parameters.albumid === undefined) { + keywords.push('albumid') + } // Only allow filtering by 'ip' and 'user' keys when listing all uploads if (all) keywords.push('ip', 'user') @@ -1360,10 +1362,14 @@ self.list = async (req, res) => { ] // Only allow sorting by 'albumid' when not listing album's uploads - if (req.params.id === undefined) allowed.push('albumid') + if (req.path_parameters.albumid === undefined) { + allowed.push('albumid') + } // Only allow sorting by 'ip' and 'userid' columns when listing all uploads - if (all) allowed.push('ip', 'userid') + if (all) { + allowed.push('ip', 'userid') + } for (const obQuery of filterObj.queries.sort) { const tmp = obQuery.toLowerCase().split(':') @@ -1468,7 +1474,7 @@ self.list = async (req, res) => { // Then, refine using any of the supplied 'albumid' keys and/or NULL flag // Same prioritization logic as 'userid' and 'ip' above - if (req.params.id === undefined) { + if (req.path_parameters.albumid === undefined) { this.andWhere(function () { if (filterObj.queries.exclude.albumid) { this.whereNotIn('albumid', filterObj.queries.exclude.albumid) @@ -1486,7 +1492,7 @@ self.list = async (req, res) => { }) } else if (!all) { // If not listing all uploads, list uploads from user's album - this.andWhere('albumid', req.params.id) + this.andWhere('albumid', req.path_parameters.albumid) } // Then, refine using the supplied 'date' ranges @@ -1566,7 +1572,7 @@ self.list = async (req, res) => { return res.json({ success: true, files: [], count }) } - let offset = Number(req.params.page) + let offset = Number(req.path_parameters.page) if (isNaN(offset)) offset = 0 else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) @@ -1674,7 +1680,7 @@ self.get = async (req, res) => { const user = await utils.authorize(req) const ismoderator = perms.is(user, 'moderator') - const identifier = req.params.identifier + const identifier = req.path_parameters.identifier if (identifier === undefined) { throw new ClientError('No identifier provided.') } diff --git a/routes/album.js b/routes/album.js index eac61a9..1c65f5d 100644 --- a/routes/album.js +++ b/routes/album.js @@ -6,7 +6,7 @@ const utils = require('./../controllers/utilsController') const config = require('./../config') routes.get('/a/:identifier', async (req, res) => { - const identifier = req.params.identifier + const identifier = req.path_parameters.identifier if (identifier === undefined) { return errors.handleNotFound(req, res) } diff --git a/routes/api.js b/routes/api.js index d9b3201..1e9b8f2 100644 --- a/routes/api.js +++ b/routes/api.js @@ -44,8 +44,8 @@ routes.get('/upload/get/:identifier', uploadController.get) routes.post('/upload/:albumid', uploadController.upload) routes.get('/album/get/:identifier', albumsController.get) routes.get('/album/zip/:identifier', albumsController.generateZip) -routes.get('/album/:id', albumsController.listFiles) -routes.get('/album/:id/:page', albumsController.listFiles) +routes.get('/album/:identifier', albumsController.getUpstreamCompat) +routes.get('/album/:albumid/:page', uploadController.list) routes.get('/albums', albumsController.list) routes.get('/albums/:page', albumsController.list) routes.post('/albums', albumsController.create)