album.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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', async (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. const album = await db.table('albums').where({ identifier, enabled: 1 }).first();
  10. if (!album) return res.status(404).sendFile('404.html', { root: './pages/error/' });
  11. const files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC');
  12. let thumb = '';
  13. const basedomain = config.domain;
  14. for (let file of files) {
  15. file.file = `${basedomain}/${file.name}`;
  16. let ext = path.extname(file.name).toLowerCase();
  17. if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) {
  18. file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -ext.length)}.png`;
  19. /*
  20. If thumbnail for album is still not set, do it.
  21. A potential improvement would be to let the user upload a specific image as an album cover
  22. since embedding the first image could potentially result in nsfw content when pasting links.
  23. */
  24. if (thumb === '') {
  25. thumb = file.thumb;
  26. }
  27. file.thumb = `<img src="${file.thumb}"/>`;
  28. } else {
  29. file.thumb = `<h1 class="title">.${ext}</h1>`;
  30. }
  31. }
  32. let enableDownload = false;
  33. if (config.uploads.generateZips) enableDownload = true;
  34. return res.render('album', {
  35. layout: false,
  36. title: album.name,
  37. count: files.length,
  38. thumb,
  39. files,
  40. identifier,
  41. enableDownload
  42. });
  43. });
  44. module.exports = routes;