albumsController.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 fs = require('fs');
  7. const Zip = require('jszip');
  8. const albumsController = {};
  9. albumsController.list = async (req, res, next) => {
  10. const user = await utils.authorize(req, res);
  11. const fields = ['id', 'name'];
  12. if (req.params.sidebar === undefined) {
  13. fields.push('timestamp');
  14. fields.push('identifier');
  15. }
  16. const albums = await db.table('albums').select(fields).where({ enabled: 1, userid: user.id });
  17. if (req.params.sidebar !== undefined) {
  18. return res.json({ success: true, albums });
  19. }
  20. let ids = [];
  21. for (let album of albums) {
  22. album.date = new Date(album.timestamp * 1000);
  23. album.date = utils.getPrettyDate(album.date);
  24. album.identifier = `${config.domain}/a/${album.identifier}`;
  25. ids.push(album.id);
  26. }
  27. const files = await db.table('files').whereIn('albumid', ids).select('albumid');
  28. const albumsCount = {};
  29. for (let id of ids) albumsCount[id] = 0;
  30. for (let file of files) albumsCount[file.albumid] += 1;
  31. for (let album of albums) album.files = albumsCount[album.id];
  32. return res.json({ success: true, albums });
  33. };
  34. albumsController.create = async (req, res, next) => {
  35. const user = await utils.authorize(req, res);
  36. const name = req.body.name;
  37. if (name === undefined || name === '') {
  38. return res.json({ success: false, description: 'No album name specified' });
  39. }
  40. const album = await db.table('albums').where({
  41. name: name,
  42. enabled: 1,
  43. userid: user.id
  44. }).first();
  45. if (album) {
  46. return res.json({ success: false, description: 'There\'s already an album with that name' });
  47. }
  48. await db.table('albums').insert({
  49. name: name,
  50. enabled: 1,
  51. userid: user.id,
  52. identifier: randomstring.generate(8),
  53. timestamp: Math.floor(Date.now() / 1000)
  54. });
  55. return res.json({ success: true });
  56. };
  57. albumsController.delete = async (req, res, next) => {
  58. const user = await utils.authorize(req, res);
  59. const id = req.body.id;
  60. if (id === undefined || id === '') {
  61. return res.json({ success: false, description: 'No album specified' });
  62. }
  63. await db.table('albums').where({ id: id, userid: user.id }).update({ enabled: 0 });
  64. return res.json({ success: true });
  65. };
  66. albumsController.rename = async (req, res, next) => {
  67. const user = await utils.authorize(req, res);
  68. const id = req.body.id;
  69. if (id === undefined || id === '') {
  70. return res.json({ success: false, description: 'No album specified' });
  71. }
  72. const name = req.body.name;
  73. if (name === undefined || name === '') {
  74. return res.json({ success: false, description: 'No name specified' });
  75. }
  76. const album = await db.table('albums').where({ name: name, userid: user.id }).first();
  77. if (album) {
  78. return res.json({ success: false, description: 'Name already in use' });
  79. }
  80. await db.table('albums').where({ id: id, userid: user.id }).update({ name: name });
  81. return res.json({ success: true });
  82. };
  83. albumsController.get = async (req, res, next) => {
  84. const identifier = req.params.identifier;
  85. if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' });
  86. const album = await db.table('albums').where({ identifier, enabled: 1 }).first();
  87. if (!album) return res.json({ success: false, description: 'Album not found' });
  88. const title = album.name;
  89. const files = await db.table('files').select('name').where('albumid', album.id).orderBy('id', 'DESC');
  90. for (let file of files) {
  91. file.file = `${config.domain}/${file.name}`;
  92. const ext = path.extname(file.name).toLowerCase();
  93. if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) {
  94. file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -ext.length)}.png`;
  95. }
  96. }
  97. return res.json({
  98. success: true,
  99. title: title,
  100. count: files.length,
  101. files
  102. });
  103. };
  104. albumsController.generateZip = async (req, res, next) => {
  105. const identifier = req.params.identifier;
  106. if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' });
  107. if (!config.uploads.generateZips) return res.status(401).json({ success: false, description: 'Zip generation disabled' });
  108. const album = await db.table('albums').where({ identifier, enabled: 1 }).first();
  109. if (!album) return res.json({ success: false, description: 'Album not found' });
  110. if (album.zipGeneratedAt > album.editedAt) {
  111. const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`);
  112. const fileName = `${album.name}.zip`;
  113. return res.download(filePath, fileName);
  114. } else {
  115. console.log(`Generating zip for album identifier: ${identifier}`);
  116. const files = await db.table('files').select('name').where('albumid', album.id);
  117. if (files.length === 0) return res.json({ success: false, description: 'There are no files in the album' });
  118. const zipPath = path.join(__dirname, '..', config.uploads.folder, 'zips', `${album.identifier}.zip`);
  119. let archive = new Zip();
  120. for (let file of files) {
  121. try {
  122. const exists = fs.statSync(path.join(__dirname, '..', config.uploads.folder, file.name));
  123. archive.file(file.name, fs.readFileSync(path.join(__dirname, '..', config.uploads.folder, file.name)));
  124. } catch (err) {
  125. console.log(err);
  126. }
  127. }
  128. archive
  129. .generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  130. .pipe(fs.createWriteStream(zipPath))
  131. .on('finish', async () => {
  132. console.log(`Generated zip for album identifier: ${identifier}`);
  133. await db.table('albums')
  134. .where('id', album.id)
  135. .update({ zipGeneratedAt: Math.floor(Date.now() / 1000) });
  136. const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`);
  137. const fileName = `${album.name}.zip`;
  138. return res.download(filePath, fileName);
  139. });
  140. }
  141. };
  142. module.exports = albumsController;