| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const whiskers = require('whiskers');
- const fs = require('fs');
- const PasteContent = require('../models/pasteContent.js').PasteContent;
- const mCrypto = require('../src/crypto.js');
- const Security = require('../src/security.js');
- async function renderRawPage(app, res, entity) {
- if (entity.type === 'paste')
- return await app.routerUtils.staticServe(res, app.getData(entity.privId));
- app.routerUtils.onInternalError(res, "Unknown type: " +entity.type);
- }
- async function renderPublicPage(app, res, entity) {
- if (entity.type === 'paste')
- return await app.routerUtils.staticServe(res, app.getData(entity.privId));
- app.routerUtils.onInternalError(res, "Unknown type: " +entity.type);
- }
- function renderPrivatePage(app, res, entity) {
- let stat;
- try { stat = fs.statSync(app.dataDir+entity.privId); } catch (e) { stat = { error: e }; }
- app.routerUtils.jsonResponse(res, { ...entity.describe(), ...stat, ...{ path: app.getData(entity.privId) } });
- }
- module.exports = { register: app => {
- app.router.get("/", (req, res) => {
- app.routerUtils.redirect(res, '/pastit');
- });
- app.router.get("/pastit", (req, res) => {
- let context = app.routerUtils.commonRenderInfos();
- context.page_title += " - Pastit";
- res.end(whiskers.render(require('../templates/pastit.js'), context));
- });
- app.router.post("/pastit", async (req, res) => {
- const content = req.body.content;
- const privId = mCrypto.string(content);
- const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], req.headers['x-forwarded-for'] || req.socket.remoteAddress);
- let entity = await app.databaseHelper.findOne(PasteContent, { privId: privId });
- if (!captchaOk)
- return app.routerUtils.jsonResponse(res, { err: "Invalid captcha input", id: null });
- if (!content || !content.length)
- return app.routerUtils.jsonResponse(res, { err: "Empty input", id: null });
- if (entity && !entity.expired) {
- entity.renew();
- await app.databaseHelper.update({privId: privId}, entity);
- } else {
- entity = entity || new PasteContent(privId, "paste");
- entity.expired = false;
- entity.renew();
- fs.writeFileSync(app.getData(privId), content);
- await app.databaseHelper.upsertOne(entity);
- }
- app.routerUtils.jsonResponse(res, { err: null, id: entity.publicId });
- });
- app.router.get("/pastit/:id", async (req, res) => {
- let entity = await app.databaseHelper.findOne(PasteContent, { privId: req.params.id, publicId: req.params.id }, " or ");
- if (entity && entity.privId === req.params.id)
- return renderPrivatePage(app, res, entity);
- if (entity && !entity.expired)
- return renderPublicPage(app, res, entity);
- app.routerUtils.onPageNotFound(res);
- });
- app.router.get("/pastit/raw/:id", async (req, res) => {
- let entity = await app.databaseHelper.findOne(PasteContent, { privId: req.params.id, publicId: req.params.id }, " or ");
- if (entity && !entity.expired)
- return renderRawPage(app, res, entity);
- app.routerUtils.onPageNotFound(res);
- });
- }};
|