albumsController.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const config = require('../config.js');
  2. const db = require('knex')(config.database);
  3. const randomstring = require('randomstring');
  4. const utils = require('./utilsController.js');
  5. const path = require('path');
  6. const albumsController = {};
  7. albumsController.list = async (req, res, next) => {
  8. const user = await utils.authorize(req, res);
  9. const fields = ['id', 'name'];
  10. if (req.params.sidebar === undefined) {
  11. fields.push('timestamp');
  12. fields.push('identifier');
  13. }
  14. const albums = await db.table('albums').select(fields).where({ enabled: 1, userid: user.id });
  15. if (req.params.sidebar !== undefined) {
  16. return res.json({ success: true, albums });
  17. }
  18. let ids = [];
  19. for (let album of albums) {
  20. album.date = new Date(album.timestamp * 1000)
  21. album.date = utils.getPrettyDate(album.date)
  22. album.identifier = `${config.domain}/a/${album.identifier}`;
  23. ids.push(album.id);
  24. }
  25. const files = await db.table('files').whereIn('albumid', ids).select('albumid');
  26. const albumsCount = {};
  27. for (let id of ids) albumsCount[id] = 0;
  28. for (let file of files) albumsCount[file.albumid] += 1;
  29. for (let album of albums) album.files = albumsCount[album.id];
  30. return res.json({ success: true, albums });
  31. };
  32. albumsController.create = async (req, res, next) => {
  33. const user = await utils.authorize(req, res);
  34. const name = req.body.name;
  35. if (name === undefined || name === '') {
  36. return res.json({ success: false, description: 'No album name specified' });
  37. }
  38. const album = await db.table('albums').where({
  39. name: name,
  40. enabled: 1,
  41. userid: user.id
  42. }).first();
  43. if (album) {
  44. return res.json({ success: false, description: 'There\'s already an album with that name' })
  45. }
  46. await db.table('albums').insert({
  47. name: name,
  48. enabled: 1,
  49. userid: user.id,
  50. identifier: randomstring.generate(8),
  51. timestamp: Math.floor(Date.now() / 1000)
  52. });
  53. return res.json({ success: true });
  54. };
  55. albumsController.delete = async (req, res, next) => {
  56. const user = await utils.authorize(req, res);
  57. const id = req.body.id;
  58. if (id === undefined || id === '') {
  59. return res.json({ success: false, description: 'No album specified' });
  60. }
  61. await db.table('albums').where({ id: id, userid: user.id }).update({ enabled: 0 });
  62. return res.json({ success: true });
  63. };
  64. albumsController.rename = async (req, res, next) => {
  65. const user = await utils.authorize(req, res);
  66. const id = req.body.id;
  67. if (id === undefined || id === '') {
  68. return res.json({ success: false, description: 'No album specified' });
  69. }
  70. const name = req.body.name;
  71. if (name === undefined || name === '') {
  72. return res.json({ success: false, description: 'No name specified' });
  73. }
  74. const album = await db.table('albums').where({ name: name, userid: user.id }).first();
  75. if (album) {
  76. return res.json({ success: false, description: 'Name already in use' })
  77. }
  78. await db.table('albums').where({ id: id, userid: user.id }).update({ name: name })
  79. return res.json({ success: true });
  80. };
  81. albumsController.get = async (req, res, next) => {
  82. const identifier = req.params.identifier;
  83. if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' });
  84. const album = await db.table('albums').where('identifier', identifier).first();
  85. if (!album) return res.json({ success: false, description: 'Album not found' });
  86. const title = album.name;
  87. const files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC');
  88. for (let file of files) {
  89. file.file = `${config.domain}/${file.name}`;
  90. const ext = path.extname(file.name).toLowerCase();
  91. if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) {
  92. file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -ext.length)}.png`;
  93. }
  94. }
  95. return res.json({
  96. success: true,
  97. title: title,
  98. count: files.length,
  99. files
  100. });
  101. };
  102. module.exports = albumsController;