Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

collection_delete.node.js 1007B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var wrench = require("wrench");
  2. module.exports = function(ctx) {
  3. ctx.getPostData(function(err, data) {
  4. var id = parseInt(data.id);
  5. if (isNaN(id))
  6. return ctx.fail("Invalid ID.");
  7. if (id === ctx.session.lastCollectionId)
  8. return deleteQuery();
  9. if (!ctx.session.loggedIn)
  10. return ctx.fail("You're not logged in.");
  11. ctx.db.query(
  12. "SELECT FROM collections "+
  13. "WHERE user_id = $1",
  14. [ctx.session.userId],
  15. function(err, res) {
  16. if (err)
  17. return ctx.fail(err);
  18. if (res.rows[0] === undefined)
  19. return ctx.fail("You don't own that collection.");
  20. deleteQuery();
  21. }
  22. );
  23. });
  24. function deleteQuery() {
  25. ctx.db.query(
  26. "DELETE FROM collections "+
  27. "WHERE id = $1",
  28. [ctx.postData.data.id],
  29. queryCallback
  30. );
  31. }
  32. function queryCallback(err, res) {
  33. if (err)
  34. return ctx.fail(err);
  35. try {
  36. wrench.rmdirSyncRecursive(
  37. ctx.conf.dir.imgs+"/"+
  38. ctx.postData.data.id
  39. );
  40. } catch (err) {
  41. return ctx.fail(err);
  42. }
  43. ctx.succeed();
  44. }
  45. }