input.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const whiskers = require('whiskers');
  2. const fs = require('fs');
  3. const PasteContent = require('../models/pasteContent.js').PasteContent;
  4. const mCrypto = require('../src/crypto.js');
  5. const Security = require('../src/security.js');
  6. async function renderRawPage(app, res, entity) {
  7. if (entity.type === 'paste')
  8. return await app.routerUtils.staticServe(res, app.getData(entity.privId));
  9. app.routerUtils.onInternalError(res, "Unknown type: " +entity.type);
  10. }
  11. async function renderPublicPage(app, res, entity) {
  12. if (entity.type === 'paste')
  13. return await app.routerUtils.staticServe(res, app.getData(entity.privId));
  14. app.routerUtils.onInternalError(res, "Unknown type: " +entity.type);
  15. }
  16. function renderPrivatePage(app, res, entity) {
  17. let stat;
  18. try { stat = fs.statSync(app.dataDir+entity.privId); } catch (e) { stat = { error: e }; }
  19. app.routerUtils.jsonResponse(res, { ...entity.describe(), ...stat, ...{ path: app.getData(entity.privId) } });
  20. }
  21. module.exports = { register: app => {
  22. app.router.get("/", (req, res) => {
  23. app.routerUtils.redirect(res, '/pastit');
  24. });
  25. app.router.get("/pastit", (req, res) => {
  26. let context = app.routerUtils.commonRenderInfos();
  27. context.page_title += " - Pastit";
  28. res.end(whiskers.render(require('../templates/pastit.js'), context));
  29. });
  30. app.router.post("/pastit", async (req, res) => {
  31. const content = req.body.content;
  32. const privId = mCrypto.string(content);
  33. const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], req.headers['x-forwarded-for'] || req.socket.remoteAddress);
  34. let entity = await app.databaseHelper.findOne(PasteContent, { privId: privId });
  35. if (!captchaOk)
  36. return app.routerUtils.jsonResponse(res, { err: "Invalid captcha input", id: null });
  37. if (!content || !content.length)
  38. return app.routerUtils.jsonResponse(res, { err: "Empty input", id: null });
  39. if (entity && !entity.expired) {
  40. entity.renew();
  41. await app.databaseHelper.update({privId: privId}, entity);
  42. } else {
  43. entity = entity || new PasteContent(privId, "paste");
  44. entity.expired = false;
  45. entity.renew();
  46. fs.writeFileSync(app.getData(privId), content);
  47. await app.databaseHelper.upsertOne(entity);
  48. }
  49. app.routerUtils.jsonResponse(res, { err: null, id: entity.publicId });
  50. });
  51. app.router.get("/pastit/:id", async (req, res) => {
  52. let entity = await app.databaseHelper.findOne(PasteContent, { privId: req.params.id, publicId: req.params.id }, " or ");
  53. if (entity && entity.privId === req.params.id)
  54. return renderPrivatePage(app, res, entity);
  55. if (entity && !entity.expired)
  56. return renderPublicPage(app, res, entity);
  57. app.routerUtils.onPageNotFound(res);
  58. });
  59. app.router.get("/pastit/raw/:id", async (req, res) => {
  60. let entity = await app.databaseHelper.findOne(PasteContent, { privId: req.params.id, publicId: req.params.id }, " or ");
  61. if (entity && !entity.expired)
  62. return renderRawPage(app, res, entity);
  63. app.routerUtils.onPageNotFound(res);
  64. });
  65. }};