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-12-18 17:41:42 +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-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-12-18 17:41:42 +00:00
|
|
|
if (!album)
|
2018-03-29 23:22:08 +00:00
|
|
|
return res.status(404).sendFile('404.html', { root: './pages/error/' })
|
2018-12-18 17:41:42 +00:00
|
|
|
else if (album.public === 0)
|
2018-04-28 17:26:39 +00:00
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'This album is not available for public.'
|
|
|
|
})
|
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-12-13 13:31:24 +00:00
|
|
|
let totalSize = 0
|
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-05-12 14:01:14 +00:00
|
|
|
file.extname = path.extname(file.name).toLowerCase()
|
|
|
|
if (utils.mayGenerateThumb(file.extname)) {
|
|
|
|
file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -file.extname.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.
|
|
|
|
*/
|
2018-12-18 17:41:42 +00:00
|
|
|
if (thumb === '') thumb = file.thumb
|
2018-05-12 14:01:14 +00:00
|
|
|
}
|
2018-12-18 17:41:42 +00:00
|
|
|
totalSize += parseInt(file.size)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.render('album', {
|
|
|
|
title: album.name,
|
2018-12-13 13:49:57 +00:00
|
|
|
description: album.description ? album.description.replace(/\n/g, '<br>') : null,
|
2018-01-23 20:06:30 +00:00
|
|
|
count: files.length,
|
|
|
|
thumb,
|
|
|
|
files,
|
|
|
|
identifier,
|
2018-05-09 08:41:30 +00:00
|
|
|
generateZips: config.uploads.generateZips,
|
2018-04-28 17:26:39 +00:00
|
|
|
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-12-13 13:31:24 +00:00
|
|
|
url: `${homeDomain}/a/${album.identifier}`,
|
2019-09-01 19:23:16 +00:00
|
|
|
totalSize,
|
|
|
|
nojs: req.query.nojs !== undefined
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = routes
|