1
1
'use strict'
2
2
// response
3
3
// external modules
4
- var fs = require ( 'fs' )
5
- var path = require ( 'path' )
6
- var markdownpdf = require ( 'markdown-pdf' )
7
- var shortId = require ( 'shortid' )
8
- var querystring = require ( 'querystring' )
9
- var request = require ( 'request' )
10
- var moment = require ( 'moment' )
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' )
9
+ const request = require ( 'request' )
10
+ const moment = require ( 'moment' )
11
11
12
12
// core
13
- var config = require ( './config' )
14
- var logger = require ( './logger' )
15
- var models = require ( './models' )
16
- var utils = require ( './utils' )
17
- var history = require ( './history' )
13
+ const config = require ( './config' )
14
+ const logger = require ( './logger' )
15
+ const models = require ( './models' )
16
+ const utils = require ( './utils' )
17
+ const history = require ( './history' )
18
18
19
19
// public
20
- var response = {
21
- errorForbidden : function ( res ) {
22
- const { req } = res
23
- if ( req . user ) {
24
- responseError ( res , '403' , 'Forbidden' , 'oh no.' )
25
- } else {
26
- req . flash ( 'error' , 'You are not allowed to access this page. Maybe try logging in?' )
27
- res . redirect ( config . serverURL + '/' )
28
- }
29
- } ,
30
- errorNotFound : function ( res ) {
31
- responseError ( res , '404' , 'Not Found' , 'oops.' )
32
- } ,
33
- errorBadRequest : function ( res ) {
34
- responseError ( res , '400' , 'Bad Request' , 'something not right.' )
35
- } ,
36
- errorTooLong : function ( res ) {
37
- responseError ( res , '413' , 'Payload Too Large' , 'Shorten your note!' )
38
- } ,
39
- errorInternalError : function ( res ) {
40
- responseError ( res , '500' , 'Internal Error' , 'wtf.' )
41
- } ,
42
- errorServiceUnavailable : function ( res ) {
43
- res . status ( 503 ) . send ( "I'm busy right now, try again later." )
44
- } ,
45
- newNote : newNote ,
46
- showNote : showNote ,
47
- showPublishNote : showPublishNote ,
48
- showPublishSlide : showPublishSlide ,
49
- showIndex : showIndex ,
50
- noteActions : noteActions ,
51
- publishNoteActions : publishNoteActions ,
52
- publishSlideActions : publishSlideActions ,
53
- githubActions : githubActions ,
54
- gitlabActions : gitlabActions
20
+ exports . errorForbidden = errorForbidden
21
+ exports . errorNotFound = errorNotFound
22
+ exports . errorBadRequest = errorBadRequest
23
+ exports . errorTooLong = errorTooLong
24
+ exports . errorInternalError = errorInternalError
25
+ exports . errorServiceUnavailable = errorServiceUnavailable
26
+ exports . newNote = newNote
27
+ exports . showNote = showNote
28
+ exports . showPublishNote = showPublishNote
29
+ exports . showPublishSlide = showPublishSlide
30
+ exports . showIndex = showIndex
31
+ exports . noteActions = noteActions
32
+ exports . publishNoteActions = publishNoteActions
33
+ exports . publishSlideActions = publishSlideActions
34
+ exports . githubActions = githubActions
35
+ exports . gitlabActions = gitlabActions
36
+
37
+ function errorForbidden ( res ) {
38
+ const { req } = res
39
+ if ( req . user ) {
40
+ responseError ( res , '403' , 'Forbidden' , 'oh no.' )
41
+ } else {
42
+ req . flash ( 'error' , 'You are not allowed to access this page. Maybe try logging in?' )
43
+ res . redirect ( config . serverURL + '/' )
44
+ }
45
+ }
46
+ function errorNotFound ( res ) {
47
+ responseError ( res , '404' , 'Not Found' , 'oops.' )
48
+ }
49
+ function errorBadRequest ( res ) {
50
+ responseError ( res , '400' , 'Bad Request' , 'something not right.' )
51
+ }
52
+ function errorTooLong ( res ) {
53
+ responseError ( res , '413' , 'Payload Too Large' , 'Shorten your note!' )
54
+ }
55
+ function errorInternalError ( res ) {
56
+ responseError ( res , '500' , 'Internal Error' , 'wtf.' )
57
+ }
58
+ function errorServiceUnavailable ( res ) {
59
+ res . status ( 503 ) . send ( "I'm busy right now, try again later." )
55
60
}
56
61
57
62
function responseError ( res , code , detail , msg ) {
@@ -117,15 +122,15 @@ function newNote (req, res, next) {
117
122
var owner = null
118
123
var body = ''
119
124
if ( req . body && req . body . length > config . documentMaxLength ) {
120
- return response . errorTooLong ( res )
125
+ return errorTooLong ( res )
121
126
} else if ( req . body ) {
122
127
body = req . body
123
128
}
124
129
body = body . replace ( / [ \r ] / g, '' )
125
130
if ( req . isAuthenticated ( ) ) {
126
131
owner = req . user . id
127
132
} else if ( ! config . allowAnonymous ) {
128
- return response . errorForbidden ( res )
133
+ return errorForbidden ( res )
129
134
}
130
135
models . Note . create ( {
131
136
ownerId : owner ,
@@ -139,7 +144,7 @@ function newNote (req, res, next) {
139
144
return res . redirect ( config . serverURL + '/' + models . Note . encodeNoteId ( note . id ) )
140
145
} ) . catch ( function ( err ) {
141
146
logger . error ( err )
142
- return response . errorInternalError ( res )
147
+ return errorInternalError ( res )
143
148
} )
144
149
}
145
150
@@ -159,7 +164,7 @@ function findNote (req, res, callback, include) {
159
164
models . Note . parseNoteId ( id , function ( err , _id ) {
160
165
if ( err ) {
161
166
logger . error ( err )
162
- return response . errorInternalError ( res )
167
+ return errorInternalError ( res )
163
168
}
164
169
models . Note . findOne ( {
165
170
where : {
@@ -172,17 +177,17 @@ function findNote (req, res, callback, include) {
172
177
req . alias = noteId
173
178
return newNote ( req , res )
174
179
} else {
175
- return response . errorNotFound ( res )
180
+ return errorNotFound ( res )
176
181
}
177
182
}
178
183
if ( ! checkViewPermission ( req , note ) ) {
179
- return response . errorForbidden ( res )
184
+ return errorForbidden ( res )
180
185
} else {
181
186
return callback ( note )
182
187
}
183
188
} ) . catch ( function ( err ) {
184
189
logger . error ( err )
185
- return response . errorInternalError ( res )
190
+ return errorInternalError ( res )
186
191
} )
187
192
} )
188
193
}
@@ -213,7 +218,7 @@ function showPublishNote (req, res, next) {
213
218
}
214
219
note . increment ( 'viewcount' ) . then ( function ( note ) {
215
220
if ( ! note ) {
216
- return response . errorNotFound ( res )
221
+ return errorNotFound ( res )
217
222
}
218
223
var body = note . content
219
224
var extracted = models . Note . extractMeta ( body )
@@ -242,7 +247,7 @@ function showPublishNote (req, res, next) {
242
247
return renderPublish ( data , res )
243
248
} ) . catch ( function ( err ) {
244
249
logger . error ( err )
245
- return response . errorInternalError ( res )
250
+ return errorInternalError ( res )
246
251
} )
247
252
} , include )
248
253
}
@@ -319,7 +324,7 @@ function actionPDF (req, res, note) {
319
324
markdownpdf ( ) . from . string ( content ) . to ( path , function ( ) {
320
325
if ( ! fs . existsSync ( path ) ) {
321
326
logger . error ( 'PDF seems to not be generated as expected. File doesn\'t exist: ' + path )
322
- return response . errorInternalError ( res )
327
+ return errorInternalError ( res )
323
328
}
324
329
var stream = fs . createReadStream ( path )
325
330
var filename = title
@@ -354,10 +359,10 @@ function actionRevision (req, res, note) {
354
359
models . Revision . getPatchedNoteRevisionByTime ( note , time , function ( err , content ) {
355
360
if ( err ) {
356
361
logger . error ( err )
357
- return response . errorInternalError ( res )
362
+ return errorInternalError ( res )
358
363
}
359
364
if ( ! content ) {
360
- return response . errorNotFound ( res )
365
+ return errorNotFound ( res )
361
366
}
362
367
res . set ( {
363
368
'Access-Control-Allow-Origin' : '*' , // allow CORS as API
@@ -369,13 +374,13 @@ function actionRevision (req, res, note) {
369
374
res . send ( content )
370
375
} )
371
376
} else {
372
- return response . errorNotFound ( res )
377
+ return errorNotFound ( res )
373
378
}
374
379
} else {
375
380
models . Revision . getNoteRevisions ( note , function ( err , data ) {
376
381
if ( err ) {
377
382
logger . error ( err )
378
- return response . errorInternalError ( res )
383
+ return errorInternalError ( res )
379
384
}
380
385
var out = {
381
386
revision : data
@@ -415,7 +420,7 @@ function noteActions (req, res, next) {
415
420
actionPDF ( req , res , note )
416
421
} else {
417
422
logger . error ( 'PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details' )
418
- response . errorForbidden ( res )
423
+ errorForbidden ( res )
419
424
}
420
425
break
421
426
case 'gist' :
@@ -480,7 +485,7 @@ function githubActionGist (req, res, note) {
480
485
var code = req . query . code
481
486
var state = req . query . state
482
487
if ( ! code || ! state ) {
483
- return response . errorForbidden ( res )
488
+ return errorForbidden ( res )
484
489
} else {
485
490
var data = {
486
491
client_id : config . github . clientID ,
@@ -520,14 +525,14 @@ function githubActionGist (req, res, note) {
520
525
res . setHeader ( 'referer' , '' )
521
526
res . redirect ( body . html_url )
522
527
} else {
523
- return response . errorForbidden ( res )
528
+ return errorForbidden ( res )
524
529
}
525
530
} )
526
531
} else {
527
- return response . errorForbidden ( res )
532
+ return errorForbidden ( res )
528
533
}
529
534
} else {
530
- return response . errorForbidden ( res )
535
+ return errorForbidden ( res )
531
536
}
532
537
} )
533
538
}
@@ -555,7 +560,7 @@ function gitlabActionProjects (req, res, note) {
555
560
id : req . user . id
556
561
}
557
562
} ) . then ( function ( user ) {
558
- if ( ! user ) { return response . errorNotFound ( res ) }
563
+ if ( ! user ) { return errorNotFound ( res ) }
559
564
var ret = { baseURL : config . gitlab . baseURL , version : config . gitlab . version }
560
565
ret . accesstoken = user . accessToken
561
566
ret . profileid = user . profileid
@@ -572,10 +577,10 @@ function gitlabActionProjects (req, res, note) {
572
577
)
573
578
} ) . catch ( function ( err ) {
574
579
logger . error ( 'gitlab action projects failed: ' + err )
575
- return response . errorInternalError ( res )
580
+ return errorInternalError ( res )
576
581
} )
577
582
} else {
578
- return response . errorForbidden ( res )
583
+ return errorForbidden ( res )
579
584
}
580
585
}
581
586
@@ -593,7 +598,7 @@ function showPublishSlide (req, res, next) {
593
598
if ( ( note . alias && shortid !== note . alias ) || ( ! note . alias && shortid !== note . shortid ) ) { return res . redirect ( config . serverURL + '/p/' + ( note . alias || note . shortid ) ) }
594
599
note . increment ( 'viewcount' ) . then ( function ( note ) {
595
600
if ( ! note ) {
596
- return response . errorNotFound ( res )
601
+ return errorNotFound ( res )
597
602
}
598
603
var body = note . content
599
604
var extracted = models . Note . extractMeta ( body )
@@ -624,7 +629,7 @@ function showPublishSlide (req, res, next) {
624
629
return renderPublishSlide ( data , res )
625
630
} ) . catch ( function ( err ) {
626
631
logger . error ( err )
627
- return response . errorInternalError ( res )
632
+ return errorInternalError ( res )
628
633
} )
629
634
} , include )
630
635
}
@@ -635,5 +640,3 @@ function renderPublishSlide (data, res) {
635
640
} )
636
641
res . render ( 'slide.ejs' , data )
637
642
}
638
-
639
- module . exports = response
0 commit comments