2018-01-23 20:06:30 +00:00
|
|
|
const config = require('../config.js')
|
|
|
|
const routes = require('express').Router()
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const path = require('path')
|
|
|
|
const utils = require('../controllers/utilsController.js')
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
routes.get('/a/:identifier', async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
let identifier = req.params.identifier
|
2018-03-29 23:22:08 +00:00
|
|
|
if (identifier === undefined) {
|
|
|
|
return res.status(401).json({ success: false, description: 'No identifier provided' })
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const album = await db.table('albums').where({ identifier, enabled: 1 }).first()
|
2018-03-29 23:22:08 +00:00
|
|
|
if (!album) {
|
|
|
|
return res.status(404).sendFile('404.html', { root: './pages/error/' })
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
const files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC')
|
|
|
|
let thumb = ''
|
|
|
|
const basedomain = config.domain
|
|
|
|
|
|
|
|
for (let file of files) {
|
|
|
|
file.file = `${basedomain}/${file.name}`
|
|
|
|
|
|
|
|
let ext = path.extname(file.name).toLowerCase()
|
2018-03-24 13:52:47 +00:00
|
|
|
if ((config.uploads.generateThumbnails.image && utils.imageExtensions.includes(ext)) || (config.uploads.generateThumbnails.video && utils.videoExtensions.includes(ext))) {
|
2018-01-23 20:06:30 +00:00
|
|
|
file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -ext.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (thumb === '') {
|
|
|
|
thumb = file.thumb
|
|
|
|
}
|
|
|
|
|
|
|
|
file.thumb = `<img src="${file.thumb}"/>`
|
|
|
|
} else {
|
2018-02-07 16:56:32 +00:00
|
|
|
file.thumb = `<h1 class="title">${ext}</h1>`
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let enableDownload = false
|
2018-03-29 23:22:08 +00:00
|
|
|
if (config.uploads.generateZips) { enableDownload = true }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
return res.render('album', {
|
|
|
|
layout: false,
|
|
|
|
title: album.name,
|
|
|
|
count: files.length,
|
|
|
|
thumb,
|
|
|
|
files,
|
|
|
|
identifier,
|
|
|
|
enableDownload
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = routes
|