albumDELETE.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const Route = require('../../structures/Route');
  2. const config = require('../../../../config');
  3. const db = require('knex')(config.server.database);
  4. const Util = require('../../utils/Util');
  5. const log = require('../../utils/Log');
  6. class albumDELETE extends Route {
  7. constructor() {
  8. super('/album/:id/:purge*?', 'delete');
  9. }
  10. async run(req, res, user) {
  11. const { id, purge } = req.params;
  12. if (!id) return res.status(400).json({ message: 'Invalid album ID supplied' });
  13. /*
  14. Check fi the album exists
  15. */
  16. const album = await db.table('albums').where({ id, userId: user.id }).first();
  17. if (!album) return res.status(400).json({ message: 'The file doesn\'t exist or doesn\'t belong to the user' });
  18. try {
  19. /*
  20. Should we also delete every file of that album?
  21. */
  22. if (purge) {
  23. await Util.deleteAllFilesFromAlbum(id);
  24. }
  25. /*
  26. Delete the album
  27. */
  28. await db.table('albums').where({ id }).delete();
  29. return res.json({ message: 'The album was deleted successfully' });
  30. } catch (error) {
  31. log.error(error);
  32. return res.json({ message: 'There was a problem deleting the album' });
  33. }
  34. }
  35. }
  36. module.exports = albumDELETE;