refactor: req.params -> .path_parameters

direct hyper-express prop get
This commit is contained in:
Bobby Wibowo 2022-07-22 01:44:15 +07:00
parent 9a3c81b8bd
commit 7b9fca0bc3
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
5 changed files with 57 additions and 53 deletions

View File

@ -106,7 +106,7 @@ self.list = async (req, res) => {
return res.json({ success: true, albums, count }) return res.json({ success: true, albums, count })
} else { } else {
let offset = Number(req.params.page) let offset = Number(req.path_parameters.page)
if (isNaN(offset)) offset = 0 if (isNaN(offset)) offset = 0
else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) 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) => { self.get = async (req, res) => {
const identifier = req.params.identifier const identifier = req.path_parameters.identifier
if (identifier === undefined) { if (identifier === undefined) {
throw new ClientError('No identifier provided.') throw new ClientError('No identifier provided.')
} }
@ -454,7 +454,7 @@ self.get = async (req, res) => {
.orderBy('id', 'desc') .orderBy('id', 'desc')
for (const file of files) { for (const file of files) {
if (req._upstreamCompat) { if (req.locals.upstreamCompat) {
file.url = `${utils.conf.domain}/${file.name}` file.url = `${utils.conf.domain}/${file.name}`
} else { } else {
file.file = `${utils.conf.domain}/${file.name}` file.file = `${utils.conf.domain}/${file.name}`
@ -463,7 +463,9 @@ self.get = async (req, res) => {
const extname = utils.extname(file.name) const extname = utils.extname(file.name)
if (utils.mayGenerateThumb(extname)) { if (utils.mayGenerateThumb(extname)) {
file.thumb = `${utils.conf.domain}/thumbs/${file.name.slice(0, -extname.length)}.png` 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) => { self.generateZip = async (req, res) => {
const versionString = parseInt(req.query.v) const versionString = parseInt(req.query.v)
const identifier = req.params.identifier const identifier = req.path_parameters.identifier
if (identifier === undefined) { if (identifier === undefined) {
throw new ClientError('No identifier provided.') throw new ClientError('No identifier provided.')
} }
@ -593,42 +627,6 @@ self.generateZip = async (req, res) => {
return res.download(filePath, fileName) 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) => { self.addFiles = async (req, res) => {
const user = await utils.authorize(req) const user = await utils.authorize(req)

View File

@ -363,7 +363,7 @@ self.listUsers = async (req, res) => {
return res.json({ success: true, users: [], count }) 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 if (isNaN(offset)) offset = 0
else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset)

View File

@ -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 if (isNaN(albumid)) albumid = null
const age = self.assertRetentionPeriod(user, req.headers.age) const age = self.assertRetentionPeriod(user, req.headers.age)
@ -1161,7 +1161,9 @@ self.list = async (req, res) => {
if (filters) { if (filters) {
const keywords = [] 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 // Only allow filtering by 'ip' and 'user' keys when listing all uploads
if (all) keywords.push('ip', 'user') 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 // 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 // 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) { for (const obQuery of filterObj.queries.sort) {
const tmp = obQuery.toLowerCase().split(':') 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 // Then, refine using any of the supplied 'albumid' keys and/or NULL flag
// Same prioritization logic as 'userid' and 'ip' above // Same prioritization logic as 'userid' and 'ip' above
if (req.params.id === undefined) { if (req.path_parameters.albumid === undefined) {
this.andWhere(function () { this.andWhere(function () {
if (filterObj.queries.exclude.albumid) { if (filterObj.queries.exclude.albumid) {
this.whereNotIn('albumid', filterObj.queries.exclude.albumid) this.whereNotIn('albumid', filterObj.queries.exclude.albumid)
@ -1486,7 +1492,7 @@ self.list = async (req, res) => {
}) })
} else if (!all) { } else if (!all) {
// If not listing all uploads, list uploads from user's album // 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 // Then, refine using the supplied 'date' ranges
@ -1566,7 +1572,7 @@ self.list = async (req, res) => {
return res.json({ success: true, files: [], count }) 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 if (isNaN(offset)) offset = 0
else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset) 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 user = await utils.authorize(req)
const ismoderator = perms.is(user, 'moderator') const ismoderator = perms.is(user, 'moderator')
const identifier = req.params.identifier const identifier = req.path_parameters.identifier
if (identifier === undefined) { if (identifier === undefined) {
throw new ClientError('No identifier provided.') throw new ClientError('No identifier provided.')
} }

View File

@ -6,7 +6,7 @@ const utils = require('./../controllers/utilsController')
const config = require('./../config') const config = require('./../config')
routes.get('/a/:identifier', async (req, res) => { routes.get('/a/:identifier', async (req, res) => {
const identifier = req.params.identifier const identifier = req.path_parameters.identifier
if (identifier === undefined) { if (identifier === undefined) {
return errors.handleNotFound(req, res) return errors.handleNotFound(req, res)
} }

View File

@ -44,8 +44,8 @@ routes.get('/upload/get/:identifier', uploadController.get)
routes.post('/upload/:albumid', uploadController.upload) routes.post('/upload/:albumid', uploadController.upload)
routes.get('/album/get/:identifier', albumsController.get) routes.get('/album/get/:identifier', albumsController.get)
routes.get('/album/zip/:identifier', albumsController.generateZip) routes.get('/album/zip/:identifier', albumsController.generateZip)
routes.get('/album/:id', albumsController.listFiles) routes.get('/album/:identifier', albumsController.getUpstreamCompat)
routes.get('/album/:id/:page', albumsController.listFiles) routes.get('/album/:albumid/:page', uploadController.list)
routes.get('/albums', albumsController.list) routes.get('/albums', albumsController.list)
routes.get('/albums/:page', albumsController.list) routes.get('/albums/:page', albumsController.list)
routes.post('/albums', albumsController.create) routes.post('/albums', albumsController.create)