album.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const config = require('../config.js')
  2. const routes = require('express').Router()
  3. const db = require('knex')(config.database)
  4. const path = require('path')
  5. const utils = require('../controllers/utilsController.js')
  6. routes.get('/a/:identifier', (req, res, next) => {
  7. let identifier = req.params.identifier
  8. if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
  9. db.table('albums')
  10. .where('identifier', identifier)
  11. .then((albums) => {
  12. if (albums.length === 0) return res.json({ success: false, description: 'Album not found' })
  13. let title = albums[0].name
  14. db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
  15. let thumb = ''
  16. let basedomain = req.get('host')
  17. for (let domain of config.domains)
  18. if (domain.host === req.get('host'))
  19. if (domain.hasOwnProperty('resolve'))
  20. basedomain = domain.resolve
  21. for (let file of files) {
  22. file.file = basedomain + '/' + file.name
  23. let ext = path.extname(file.name).toLowerCase()
  24. if (utils.extensions.includes(ext)) {
  25. file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
  26. /*
  27. If thumbnail for album is still not set, do it.
  28. A potential improvement would be to let the user upload a specific image as an album cover
  29. since embedding the first image could potentially result in nsfw content when pasting links.
  30. */
  31. if (thumb === '') {
  32. thumb = file.thumb
  33. }
  34. file.thumb = `<img src="${file.thumb}"/>`
  35. } else {
  36. file.thumb = `<h1 class="title">.${ext}</h1>`
  37. }
  38. }
  39. return res.render('album', {
  40. layout: false,
  41. title: title,
  42. count: files.length,
  43. thumb,
  44. files
  45. })
  46. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  47. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  48. })
  49. module.exports = routes