albumsController.js 5.9 KB

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