2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
2018-01-23 20:06:30 +00:00
|
|
|
const routes = require('express').Router()
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const path = require('path')
|
2018-04-13 16:20:57 +00:00
|
|
|
const utils = require('./../controllers/utilsController')
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-18 21:00:36 +00:00
|
|
|
const homeDomain = config.homeDomain || config.domain
|
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
routes.get('/a/:identifier', async (req, res, next) => {
|
2018-04-05 10:52:57 +00:00
|
|
|
const identifier = req.params.identifier
|
2018-03-29 23:22:08 +00:00
|
|
|
if (identifier === undefined) {
|
2018-04-26 18:33:11 +00:00
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
2018-04-26 21:04:21 +00:00
|
|
|
description: 'No identifier provided.'
|
2018-04-26 18:33:11 +00:00
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-26 18:33:11 +00:00
|
|
|
const album = await db.table('albums')
|
2018-04-28 17:26:39 +00:00
|
|
|
.where({
|
|
|
|
identifier,
|
|
|
|
enabled: 1
|
|
|
|
})
|
2018-04-26 18:33:11 +00:00
|
|
|
.first()
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
if (!album) {
|
|
|
|
return res.status(404).sendFile('404.html', { root: './pages/error/' })
|
2018-04-28 17:26:39 +00:00
|
|
|
} else if (album.public === 0) {
|
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'This album is not available for public.'
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-26 18:33:11 +00:00
|
|
|
const files = await db.table('files')
|
|
|
|
.select('name', 'size')
|
|
|
|
.where('albumid', album.id)
|
|
|
|
.orderBy('id', 'DESC')
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
let thumb = ''
|
|
|
|
const basedomain = config.domain
|
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const file of files) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.file = `${basedomain}/${file.name}`
|
2018-04-26 18:33:11 +00:00
|
|
|
file.size = utils.getPrettyBytes(parseInt(file.size))
|
2018-04-29 12:47:24 +00:00
|
|
|
file.extname = path.extname(file.name).toLowerCase()
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
if (!utils.mayGenerateThumb(file.extname)) { continue }
|
|
|
|
file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -file.extname.length)}.png`
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
/*
|
|
|
|
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 }
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.render('album', {
|
|
|
|
title: album.name,
|
|
|
|
count: files.length,
|
|
|
|
thumb,
|
|
|
|
files,
|
|
|
|
identifier,
|
2018-04-28 17:26:39 +00:00
|
|
|
generateZips: config.uploads.generateZips && config.uploads.generateZips.enabled,
|
|
|
|
downloadLink: album.download === 0 ? null : `../api/album/zip/${album.identifier}?v=${album.editedAt}`,
|
2018-04-26 21:30:11 +00:00
|
|
|
editedAt: album.editedAt,
|
2018-04-18 21:00:36 +00:00
|
|
|
url: `${homeDomain}/a/${album.identifier}`
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = routes
|