added lolisafe upstream compat to /api/album/:id

it will re-map body of /api/album/get/:id into upstream-compatible body.
prep for lolisafe albums support for magane plugin.

/api/album/:id/:page will stil respond with the old format as that's
what the dashboard use and expect.

list views of uploads, users and albums in dashboard will now show
total items count on the table's top right corner.
This commit is contained in:
Bobby Wibowo 2020-12-26 19:54:41 +07:00
parent 1382afd8c8
commit e85e8e886d
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
3 changed files with 52 additions and 16 deletions

View File

@ -5,6 +5,7 @@ const randomstring = require('randomstring')
const Zip = require('jszip')
const paths = require('./pathsController')
const perms = require('./permissionController')
const uploadController = require('./uploadController')
const utils = require('./utilsController')
const config = require('./../config')
const logger = require('./../logger')
@ -382,16 +383,8 @@ self.get = async (req, res, next) => {
})
.first()
if (!album) {
return res.json({
success: false,
description: 'Album not found.'
})
} else if (album.public === 0) {
return res.status(403).json({
success: false,
description: 'This album is not available for public.'
})
if (!album || album.public === 0) {
return res.status(404).json({ success: false, description: 'The album could not be found.' })
}
const title = album.name
@ -401,17 +394,24 @@ self.get = async (req, res, next) => {
.orderBy('id', 'desc')
for (const file of files) {
if (req._upstreamCompat) {
file.url = `${config.domain}/${file.name}`
} else {
file.file = `${config.domain}/${file.name}`
}
const extname = utils.extname(file.name)
if (utils.mayGenerateThumb(extname)) {
file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -extname.length)}.png`
if (req._upstreamCompat) file.thumbSquare = file.thumb
}
}
return res.json({
success: true,
description: 'Successfully retrieved files.',
title,
download: Boolean(album.download),
count: files.length,
files
})
@ -552,6 +552,42 @@ self.generateZip = async (req, res, next) => {
}
}
self.listFiles = async (req, res, next) => {
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, next)
} else {
return uploadController.list(req, res, next)
}
}
self.addFiles = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) return

View File

@ -33,8 +33,8 @@ routes.post('/upload/finishchunks', (req, res, next) => uploadController.finishC
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/zip/:identifier', (req, res, next) => albumsController.generateZip(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('/album/:id', (req, res, next) => albumsController.listFiles(req, res, next))
routes.get('/album/:id/:page', (req, res, next) => albumsController.listFiles(req, res, next))
routes.get('/albums', (req, res, next) => albumsController.list(req, res, next))
routes.get('/albums/:page', (req, res, next) => albumsController.list(req, res, next))
routes.post('/albums', (req, res, next) => albumsController.create(req, res, next))

View File

@ -808,7 +808,7 @@ page.getUploads = (params = {}) => {
${params.all ? '<th title="Key: ip">IP</th>' : ''}
<th title="Key: timestamp">Upload date</th>
${hasExpiryDateColumn ? '<th title="Key: expirydate">Expiry date</th>' : ''}
<th></th>
<th class="has-text-right">(${response.data.count} total)</th>
</tr>
</thead>
<tbody id="table">
@ -1748,7 +1748,7 @@ page.getAlbums = (params = {}) => {
<th>Uploads</th>
<th>Created at</th>
<th>Public link</th>
<th></th>
<th class="has-text-right">(${response.data.count} total)</th>
</tr>
</thead>
<tbody id="table">
@ -2386,7 +2386,7 @@ page.getUsers = (params = {}) => {
<th title="Key: permission">Group</th>
<th title="Key: registration">Registration date</th>
<th title="Key: timestamp">Last token update</th>
<th></th>
<th class="has-text-right">(${response.data.count} total)</th>
</tr>
</thead>
<tbody id="table">