@@ -67,84 +67,52 @@ function get(req, res, includeBody) {
6767 header . addLink ( res , aclLink , 'acl' ) ;
6868 header . addLink ( res , metaLink , 'describedBy' ) ;
6969
70- ldp . stat ( filename , function ( err , stats ) {
71- // File does not exist
70+ ldp . get ( filename , uri , includeBody , function ( err , data , container ) {
71+
72+ // This should be implemented in LDP.prototype.get
73+ if ( err && err . status === 404 && glob . hasMagic ( filename ) ) {
74+ debug ( "GET/HEAD -- Glob request" ) ;
75+ return globHandler ( req , res ) ;
76+ }
77+
7278 if ( err ) {
73- // Check in case it is a pattern, e.g. /*.js
74- if ( glob . hasMagic ( filename ) ) {
75- debug ( "GET/HEAD -- Glob request" ) ;
76- return globHandler ( req , res ) ;
77- }
7879 debug ( 'GET/HEAD -- Read error: ' + err ) ;
7980 return res
80- . status ( 404 )
81- . send ( "Can't read file: " + err ) ;
81+ . status ( err . status )
82+ . send ( err . message ) ;
8283 }
8384
84- // Just return resource exists
85- if ( ! includeBody ) {
85+ // Just return that file exists
86+ // data is of `typeof 'string'` if file is empty, but exists!
87+ if ( data === undefined ) {
8688 return res . sendStatus ( 200 ) ;
8789 }
8890
89- // Found a container
90- if ( stats . isDirectory ( ) ) {
91- return ldp . readContainerMeta ( filename , function ( err , data ) {
92- if ( err ) {
93- debug ( 'GET/HEAD -- Read error:' + err ) ;
94- return res
95- . status ( err . status )
96- . send ( err . message ) ;
97- }
98- ldp . listContainer ( filename , uri , data , function ( err , data ) {
99- if ( err ) {
100- debug ( 'GET/HEAD -- Read error:' + err ) ;
101- return res
102- . status ( err . status )
103- . send ( err . message ) ;
104- }
105- // TODO this should be added as a middleware in the routes
106- res . locals . turtleData = data ;
107- return parseLinkedData ( req , res ) ;
108- } ) ;
109- } ) ;
110- }
111- else {
112- return ldp . readFile ( filename , function ( err , data ) {
113- // Error when reading
114- if ( err ) {
115- debug ( 'GET/HEAD -- Read error:' + err ) ;
116- return res
117- . status ( err . status )
118- . send ( err . message ) ;
119- }
120-
121- // File retrieved
122- debug ( 'GET/HEAD -- Read Ok. Bytes read: ' + data . length ) ;
123-
124- var contentType = mime . lookup ( filename ) ;
125- res . set ( 'content-type' , contentType ) ;
126- debug ( 'GET/HEAD -- content-type: ' + contentType ) ;
127-
128- // Consider acl and meta files text/turtle
129- if ( path . extname ( filename ) === ldp . suffixAcl ||
130- path . basename ( filename ) === turtleExtension ||
131- path . basename ( filename ) === metaExtension ) {
132- contentType = 'text/turtle' ;
133- }
134-
135- // if data is text/turtle, parse it
136- if ( contentType === 'text/turtle' ) {
137- // TODO this should be added as a middleware in the routes
138- res . locals . turtleData = data ;
139- return parseLinkedData ( req , res ) ;
140- }
91+ // Container/resource retrieved
92+ if ( ! container ) {
93+ var contentType = mime . lookup ( filename ) ;
94+ res . set ( 'content-type' , contentType ) ;
95+ debug ( 'GET/HEAD -- content-type: ' + contentType ) ;
96+
97+ // Consider acl and meta files text/turtle
98+ if ( path . extname ( filename ) === ldp . suffixAcl ||
99+ path . basename ( filename ) === turtleExtension ||
100+ path . basename ( filename ) === metaExtension ) {
101+ contentType = 'text/turtle' ;
102+ }
141103
142- // Otherwise, just send the data
104+ // if data is not text/turtle, just send it
105+ if ( contentType !== 'text/turtle' ) {
143106 return res
144107 . status ( 200 )
145108 . send ( data ) ;
146- } ) ;
109+ }
147110 }
111+
112+ // TODO this should be added as a middleware in the routes
113+ res . locals . turtleData = data ;
114+ return parseLinkedData ( req , res ) ;
115+
148116 } ) ;
149117
150118}
0 commit comments