From 57345b06f7ed13659abbf351e1269510aac99c63 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Sun, 5 Jan 2020 07:43:01 +0800 Subject: [PATCH] refactor: noteActions Signed-off-by: BoHong Li --- lib/response.js | 211 +----------------------------------------------- 1 file changed, 4 insertions(+), 207 deletions(-) diff --git a/lib/response.js b/lib/response.js index f9bf5f0d..4779afb8 100644 --- a/lib/response.js +++ b/lib/response.js @@ -1,13 +1,7 @@ 'use strict' // response // external modules -const fs = require('fs') -const path = require('path') -const markdownpdf = require('markdown-pdf') -const shortId = require('shortid') -const querystring = require('querystring') const request = require('request') -const moment = require('moment') // core const config = require('./config') @@ -73,35 +67,6 @@ function responseError (res, code, detail, msg) { }) } -function showIndex (req, res, next) { - var authStatus = req.isAuthenticated() - var deleteToken = '' - - var data = { - signin: authStatus, - infoMessage: req.flash('info'), - errorMessage: req.flash('error'), - privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')), - termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')), - deleteToken: deleteToken - } - - if (authStatus) { - models.User.findOne({ - where: { - id: req.user.id - } - }).then(function (user) { - if (user) { - data.deleteToken = user.deleteToken - res.render('index.ejs', data) - } - }) - } else { - res.render('index.ejs', data) - } -} - function responseCodiMD (res, note) { var body = note.content var extracted = models.Note.extractMeta(body) @@ -207,14 +172,6 @@ function findNote (req, res, callback, include) { }) } -function actionPublish (req, res, note) { - res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid)) -} - -function actionSlide (req, res, note) { - res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) -} - function actionDownload (req, res, note) { var body = note.content var title = models.Note.decodeTitle(note.title) @@ -232,162 +189,6 @@ function actionDownload (req, res, note) { res.send(body) } -function actionInfo (req, res, note) { - var body = note.content - var extracted = models.Note.extractMeta(body) - var markdown = extracted.markdown - var meta = models.Note.parseMeta(extracted.meta) - var createtime = note.createdAt - var updatetime = note.lastchangeAt - var title = models.Note.decodeTitle(note.title) - var data = { - title: meta.title || title, - description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null), - viewcount: note.viewcount, - createtime: createtime, - updatetime: updatetime - } - res.set({ - 'Access-Control-Allow-Origin': '*', // allow CORS as API - 'Access-Control-Allow-Headers': 'Range', - 'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range', - 'Cache-Control': 'private', // only cache by client - 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling - }) - res.send(data) -} - -function actionPDF (req, res, note) { - var url = config.serverURL || 'http://' + req.get('host') - var body = note.content - var extracted = models.Note.extractMeta(body) - var content = extracted.markdown - var title = models.Note.decodeTitle(note.title) - - var highlightCssPath = path.join(config.appRootPath, '/node_modules/highlight.js/styles/github-gist.css') - - if (!fs.existsSync(config.tmpPath)) { - fs.mkdirSync(config.tmpPath) - } - var pdfPath = config.tmpPath + '/' + Date.now() + '.pdf' - content = content.replace(/\]\(\//g, '](' + url + '/') - var markdownpdfOptions = { - highlightCssPath: highlightCssPath - } - markdownpdf(markdownpdfOptions).from.string(content).to(pdfPath, function () { - if (!fs.existsSync(pdfPath)) { - logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath) - return errorInternalError(res) - } - var stream = fs.createReadStream(pdfPath) - var filename = title - // Be careful of special characters - filename = encodeURIComponent(filename) - // Ideally this should strip them - res.setHeader('Content-disposition', 'attachment; filename="' + filename + '.pdf"') - res.setHeader('Cache-Control', 'private') - res.setHeader('Content-Type', 'application/pdf; charset=UTF-8') - res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling - stream.pipe(res) - fs.unlinkSync(pdfPath) - }) -} - -function actionGist (req, res, note) { - var data = { - client_id: config.github.clientID, - redirect_uri: config.serverURL + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist', - scope: 'gist', - state: shortId.generate() - } - var query = querystring.stringify(data) - res.redirect('https://github.com/login/oauth/authorize?' + query) -} - -function actionRevision (req, res, note) { - var actionId = req.params.actionId - if (actionId) { - var time = moment(parseInt(actionId)) - if (time.isValid()) { - models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) { - if (err) { - logger.error(err) - return errorInternalError(res) - } - if (!content) { - return errorNotFound(res) - } - res.set({ - 'Access-Control-Allow-Origin': '*', // allow CORS as API - 'Access-Control-Allow-Headers': 'Range', - 'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range', - 'Cache-Control': 'private', // only cache by client - 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling - }) - res.send(content) - }) - } else { - return errorNotFound(res) - } - } else { - models.Revision.getNoteRevisions(note, function (err, data) { - if (err) { - logger.error(err) - return errorInternalError(res) - } - var out = { - revision: data - } - res.set({ - 'Access-Control-Allow-Origin': '*', // allow CORS as API - 'Access-Control-Allow-Headers': 'Range', - 'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range', - 'Cache-Control': 'private', // only cache by client - 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling - }) - res.send(out) - }) - } -} - -function noteActions (req, res, next) { - var noteId = req.params.noteId - findNote(req, res, function (note) { - var action = req.params.action - switch (action) { - case 'publish': - case 'pretty': // pretty deprecated - actionPublish(req, res, note) - break - case 'slide': - actionSlide(req, res, note) - break - case 'download': - actionDownload(req, res, note) - break - case 'info': - actionInfo(req, res, note) - break - case 'pdf': - if (config.allowPDFExport) { - actionPDF(req, res, note) - } else { - logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details') - errorForbidden(res) - } - break - case 'gist': - actionGist(req, res, note) - break - case 'revision': - actionRevision(req, res, note) - break - default: - return res.redirect(config.serverURL + '/' + noteId) - } - }) -} - function publishNoteActions (req, res, next) { findNote(req, res, function (note) { var action = req.params.action @@ -579,17 +380,13 @@ function showPublishSlide (req, res, next) { disqus: meta.disqus, cspNonce: res.locals.nonce } - return renderPublishSlide(data, res) + res.set({ + 'Cache-Control': 'private' // only cache by client + }) + res.render('slide.ejs', data) }).catch(function (err) { logger.error(err) return errorInternalError(res) }) }, include) } - -function renderPublishSlide (data, res) { - res.set({ - 'Cache-Control': 'private' // only cache by client - }) - res.render('slide.ejs', data) -}