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