album.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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', identifier).first();
  10. if (!album) return res.json({ success: false, description: 'Album not found' });
  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. return res.render('album', {
  33. layout: false,
  34. title: album.name,
  35. count: files.length,
  36. thumb,
  37. files
  38. });
  39. });
  40. module.exports = routes;