@@ -20,48 +20,49 @@ var redApp = null;
2020var storage = require ( "../storage" ) ;
2121
2222function createLibrary ( type ) {
23-
24- redApp . get ( new RegExp ( "/library/" + type + "($|\/(.*))" ) , function ( req , res ) {
25- var path = req . params [ 1 ] || "" ;
26- storage . getLibraryEntry ( type , path ) . then ( function ( result ) {
27- if ( typeof result === "string" ) {
28- res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
29- res . write ( result ) ;
30- res . end ( ) ;
31- } else {
32- res . json ( result ) ;
33- }
34- } ) . otherwise ( function ( err ) {
35- if ( err ) {
36- util . log ( "[red] Error loading library entry '" + path + "' : " + err ) ;
37- if ( err . message . indexOf ( 'forbidden' ) === 0 ) {
38- res . send ( 403 ) ;
39- return ;
23+ if ( redApp ) {
24+ redApp . get ( new RegExp ( "/library/" + type + "($|\/(.*))" ) , function ( req , res ) {
25+ var path = req . params [ 1 ] || "" ;
26+ storage . getLibraryEntry ( type , path ) . then ( function ( result ) {
27+ if ( typeof result === "string" ) {
28+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
29+ res . write ( result ) ;
30+ res . end ( ) ;
31+ } else {
32+ res . json ( result ) ;
4033 }
41- }
42- res . send ( 404 ) ;
43- } ) ;
44- } ) ;
45-
46- redApp . post ( new RegExp ( "/library/" + type + "\/(.*)" ) , function ( req , res ) {
47- var path = req . params [ 0 ] ;
48- var fullBody = '' ;
49- req . on ( 'data' , function ( chunk ) {
50- fullBody += chunk . toString ( ) ;
51- } ) ;
52- req . on ( 'end' , function ( ) {
53- storage . saveLibraryEntry ( type , path , req . query , fullBody ) . then ( function ( ) {
54- res . send ( 204 ) ;
55- } ) . otherwise ( function ( err ) {
56- util . log ( "[red] Error saving library entry '" + path + "' : " + err ) ;
34+ } ) . otherwise ( function ( err ) {
35+ if ( err ) {
36+ util . log ( "[red] Error loading library entry '" + path + "' : " + err ) ;
5737 if ( err . message . indexOf ( 'forbidden' ) === 0 ) {
5838 res . send ( 403 ) ;
5939 return ;
6040 }
61- res . send ( 500 ) ;
62- } ) ;
41+ }
42+ res . send ( 404 ) ;
43+ } ) ;
6344 } ) ;
64- } ) ;
45+
46+ redApp . post ( new RegExp ( "/library/" + type + "\/(.*)" ) , function ( req , res ) {
47+ var path = req . params [ 0 ] ;
48+ var fullBody = '' ;
49+ req . on ( 'data' , function ( chunk ) {
50+ fullBody += chunk . toString ( ) ;
51+ } ) ;
52+ req . on ( 'end' , function ( ) {
53+ storage . saveLibraryEntry ( type , path , req . query , fullBody ) . then ( function ( ) {
54+ res . send ( 204 ) ;
55+ } ) . otherwise ( function ( err ) {
56+ util . log ( "[red] Error saving library entry '" + path + "' : " + err ) ;
57+ if ( err . message . indexOf ( 'forbidden' ) === 0 ) {
58+ res . send ( 403 ) ;
59+ return ;
60+ }
61+ res . send ( 500 ) ;
62+ } ) ;
63+ } ) ;
64+ } ) ;
65+ }
6566}
6667module . exports = {
6768 init : function ( app ) {
0 commit comments