11'use strict'
22// response
33// external modules
4- const fs = require ( 'fs' )
5- const path = require ( 'path' )
6- const markdownpdf = require ( 'markdown-pdf' )
7- const shortId = require ( 'shortid' )
8- const querystring = require ( 'querystring' )
94const request = require ( 'request' )
10- const moment = require ( 'moment' )
115
126// core
137const config = require ( './config' )
@@ -73,35 +67,6 @@ function responseError (res, code, detail, msg) {
7367 } )
7468}
7569
76- function showIndex ( req , res , next ) {
77- var authStatus = req . isAuthenticated ( )
78- var deleteToken = ''
79-
80- var data = {
81- signin : authStatus ,
82- infoMessage : req . flash ( 'info' ) ,
83- errorMessage : req . flash ( 'error' ) ,
84- privacyStatement : fs . existsSync ( path . join ( config . docsPath , 'privacy.md' ) ) ,
85- termsOfUse : fs . existsSync ( path . join ( config . docsPath , 'terms-of-use.md' ) ) ,
86- deleteToken : deleteToken
87- }
88-
89- if ( authStatus ) {
90- models . User . findOne ( {
91- where : {
92- id : req . user . id
93- }
94- } ) . then ( function ( user ) {
95- if ( user ) {
96- data . deleteToken = user . deleteToken
97- res . render ( 'index.ejs' , data )
98- }
99- } )
100- } else {
101- res . render ( 'index.ejs' , data )
102- }
103- }
104-
10570function responseCodiMD ( res , note ) {
10671 var body = note . content
10772 var extracted = models . Note . extractMeta ( body )
@@ -207,14 +172,6 @@ function findNote (req, res, callback, include) {
207172 } )
208173}
209174
210- function actionPublish ( req , res , note ) {
211- res . redirect ( config . serverURL + '/s/' + ( note . alias || note . shortid ) )
212- }
213-
214- function actionSlide ( req , res , note ) {
215- res . redirect ( config . serverURL + '/p/' + ( note . alias || note . shortid ) )
216- }
217-
218175function actionDownload ( req , res , note ) {
219176 var body = note . content
220177 var title = models . Note . decodeTitle ( note . title )
@@ -232,162 +189,6 @@ function actionDownload (req, res, note) {
232189 res . send ( body )
233190}
234191
235- function actionInfo ( req , res , note ) {
236- var body = note . content
237- var extracted = models . Note . extractMeta ( body )
238- var markdown = extracted . markdown
239- var meta = models . Note . parseMeta ( extracted . meta )
240- var createtime = note . createdAt
241- var updatetime = note . lastchangeAt
242- var title = models . Note . decodeTitle ( note . title )
243- var data = {
244- title : meta . title || title ,
245- description : meta . description || ( markdown ? models . Note . generateDescription ( markdown ) : null ) ,
246- viewcount : note . viewcount ,
247- createtime : createtime ,
248- updatetime : updatetime
249- }
250- res . set ( {
251- 'Access-Control-Allow-Origin' : '*' , // allow CORS as API
252- 'Access-Control-Allow-Headers' : 'Range' ,
253- 'Access-Control-Expose-Headers' : 'Cache-Control, Content-Encoding, Content-Range' ,
254- 'Cache-Control' : 'private' , // only cache by client
255- 'X-Robots-Tag' : 'noindex, nofollow' // prevent crawling
256- } )
257- res . send ( data )
258- }
259-
260- function actionPDF ( req , res , note ) {
261- var url = config . serverURL || 'http://' + req . get ( 'host' )
262- var body = note . content
263- var extracted = models . Note . extractMeta ( body )
264- var content = extracted . markdown
265- var title = models . Note . decodeTitle ( note . title )
266-
267- var highlightCssPath = path . join ( config . appRootPath , '/node_modules/highlight.js/styles/github-gist.css' )
268-
269- if ( ! fs . existsSync ( config . tmpPath ) ) {
270- fs . mkdirSync ( config . tmpPath )
271- }
272- var pdfPath = config . tmpPath + '/' + Date . now ( ) + '.pdf'
273- content = content . replace ( / \] \( \/ / g, '](' + url + '/' )
274- var markdownpdfOptions = {
275- highlightCssPath : highlightCssPath
276- }
277- markdownpdf ( markdownpdfOptions ) . from . string ( content ) . to ( pdfPath , function ( ) {
278- if ( ! fs . existsSync ( pdfPath ) ) {
279- logger . error ( 'PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath )
280- return errorInternalError ( res )
281- }
282- var stream = fs . createReadStream ( pdfPath )
283- var filename = title
284- // Be careful of special characters
285- filename = encodeURIComponent ( filename )
286- // Ideally this should strip them
287- res . setHeader ( 'Content-disposition' , 'attachment; filename="' + filename + '.pdf"' )
288- res . setHeader ( 'Cache-Control' , 'private' )
289- res . setHeader ( 'Content-Type' , 'application/pdf; charset=UTF-8' )
290- res . setHeader ( 'X-Robots-Tag' , 'noindex, nofollow' ) // prevent crawling
291- stream . pipe ( res )
292- fs . unlinkSync ( pdfPath )
293- } )
294- }
295-
296- function actionGist ( req , res , note ) {
297- var data = {
298- client_id : config . github . clientID ,
299- redirect_uri : config . serverURL + '/auth/github/callback/' + models . Note . encodeNoteId ( note . id ) + '/gist' ,
300- scope : 'gist' ,
301- state : shortId . generate ( )
302- }
303- var query = querystring . stringify ( data )
304- res . redirect ( 'https://github.com/login/oauth/authorize?' + query )
305- }
306-
307- function actionRevision ( req , res , note ) {
308- var actionId = req . params . actionId
309- if ( actionId ) {
310- var time = moment ( parseInt ( actionId ) )
311- if ( time . isValid ( ) ) {
312- models . Revision . getPatchedNoteRevisionByTime ( note , time , function ( err , content ) {
313- if ( err ) {
314- logger . error ( err )
315- return errorInternalError ( res )
316- }
317- if ( ! content ) {
318- return errorNotFound ( res )
319- }
320- res . set ( {
321- 'Access-Control-Allow-Origin' : '*' , // allow CORS as API
322- 'Access-Control-Allow-Headers' : 'Range' ,
323- 'Access-Control-Expose-Headers' : 'Cache-Control, Content-Encoding, Content-Range' ,
324- 'Cache-Control' : 'private' , // only cache by client
325- 'X-Robots-Tag' : 'noindex, nofollow' // prevent crawling
326- } )
327- res . send ( content )
328- } )
329- } else {
330- return errorNotFound ( res )
331- }
332- } else {
333- models . Revision . getNoteRevisions ( note , function ( err , data ) {
334- if ( err ) {
335- logger . error ( err )
336- return errorInternalError ( res )
337- }
338- var out = {
339- revision : data
340- }
341- res . set ( {
342- 'Access-Control-Allow-Origin' : '*' , // allow CORS as API
343- 'Access-Control-Allow-Headers' : 'Range' ,
344- 'Access-Control-Expose-Headers' : 'Cache-Control, Content-Encoding, Content-Range' ,
345- 'Cache-Control' : 'private' , // only cache by client
346- 'X-Robots-Tag' : 'noindex, nofollow' // prevent crawling
347- } )
348- res . send ( out )
349- } )
350- }
351- }
352-
353- function noteActions ( req , res , next ) {
354- var noteId = req . params . noteId
355- findNote ( req , res , function ( note ) {
356- var action = req . params . action
357- switch ( action ) {
358- case 'publish' :
359- case 'pretty' : // pretty deprecated
360- actionPublish ( req , res , note )
361- break
362- case 'slide' :
363- actionSlide ( req , res , note )
364- break
365- case 'download' :
366- actionDownload ( req , res , note )
367- break
368- case 'info' :
369- actionInfo ( req , res , note )
370- break
371- case 'pdf' :
372- if ( config . allowPDFExport ) {
373- actionPDF ( req , res , note )
374- } else {
375- logger . error ( 'PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details' )
376- errorForbidden ( res )
377- }
378- break
379- case 'gist' :
380- actionGist ( req , res , note )
381- break
382- case 'revision' :
383- actionRevision ( req , res , note )
384- break
385- default :
386- return res . redirect ( config . serverURL + '/' + noteId )
387- }
388- } )
389- }
390-
391192function publishNoteActions ( req , res , next ) {
392193 findNote ( req , res , function ( note ) {
393194 var action = req . params . action
@@ -579,17 +380,13 @@ function showPublishSlide (req, res, next) {
579380 disqus : meta . disqus ,
580381 cspNonce : res . locals . nonce
581382 }
582- return renderPublishSlide ( data , res )
383+ res . set ( {
384+ 'Cache-Control' : 'private' // only cache by client
385+ } )
386+ res . render ( 'slide.ejs' , data )
583387 } ) . catch ( function ( err ) {
584388 logger . error ( err )
585389 return errorInternalError ( res )
586390 } )
587391 } , include )
588392}
589-
590- function renderPublishSlide ( data , res ) {
591- res . set ( {
592- 'Cache-Control' : 'private' // only cache by client
593- } )
594- res . render ( 'slide.ejs' , data )
595- }
0 commit comments