mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 08:26:22 +00:00
7991a63315
NOTICE: Please update your config.js. Use config.sample.js as the template. There were a couple of renames and restructures. * Album zipper API route will now internally save its state when it's generating zip files, and any subsequent requests will silently be "postponed" until the first spawned task is finished. This will guarantee that there are no multiple zipping tasks for the same album. The method may seem a bit hackish though. * All instances of console.log(error) were replaced with console.error(error). This will guarantee that any error goes to stderr instead of stdout. * Deleting file by names will now properly remove successful files from the textarea. There was a logic flaw. * Failure to generate thumbnails will no longer print the full stack, but instead only the error message. It will also then symlink a template image from /public/images/unavailable.png (it's only a simple image that says that it failed to generate thumbnail). This haven't been tested in Windows machines, but it'll probably work fine. I thought of adding a new column to files table which will store information whether the thumbnail generation is sucessful or not, but oh well, I'll go with this method for now.
72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
const config = require('./../config')
|
|
const routes = require('express').Router()
|
|
const db = require('knex')(config.database)
|
|
const path = require('path')
|
|
const utils = require('./../controllers/utilsController')
|
|
|
|
const homeDomain = config.homeDomain || config.domain
|
|
|
|
routes.get('/a/:identifier', async (req, res, next) => {
|
|
const identifier = req.params.identifier
|
|
if (identifier === undefined) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
description: 'No identifier provided.'
|
|
})
|
|
}
|
|
|
|
const album = await db.table('albums')
|
|
.where({
|
|
identifier,
|
|
enabled: 1
|
|
})
|
|
.first()
|
|
|
|
if (!album) {
|
|
return res.status(404).sendFile('404.html', { root: './pages/error/' })
|
|
} else if (album.public === 0) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
description: 'This album is not available for public.'
|
|
})
|
|
}
|
|
|
|
const files = await db.table('files')
|
|
.select('name', 'size')
|
|
.where('albumid', album.id)
|
|
.orderBy('id', 'DESC')
|
|
|
|
let thumb = ''
|
|
const basedomain = config.domain
|
|
|
|
for (const file of files) {
|
|
file.file = `${basedomain}/${file.name}`
|
|
file.size = utils.getPrettyBytes(parseInt(file.size))
|
|
file.extname = path.extname(file.name).toLowerCase()
|
|
|
|
if (!utils.mayGenerateThumb(file.extname)) { continue }
|
|
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.
|
|
*/
|
|
if (thumb === '') { thumb = file.thumb }
|
|
}
|
|
|
|
return res.render('album', {
|
|
title: album.name,
|
|
count: files.length,
|
|
thumb,
|
|
files,
|
|
identifier,
|
|
generateZips: config.uploads.generateZips,
|
|
downloadLink: album.download === 0 ? null : `../api/album/zip/${album.identifier}?v=${album.editedAt}`,
|
|
editedAt: album.editedAt,
|
|
url: `${homeDomain}/a/${album.identifier}`
|
|
})
|
|
})
|
|
|
|
module.exports = routes
|