@@ -85,8 +85,9 @@ function get(req, res, includeBody) {
8585 . status ( err . status )
8686 . send ( err . message ) ;
8787 }
88-
89- return parseLinkedData ( data ) ;
88+ // TODO this should be added as a middleware in the routes
89+ res . locals . turtleData = data ;
90+ return parseLinkedData ( req , res ) ;
9091 } ) ;
9192 } ) ;
9293 }
@@ -116,7 +117,9 @@ function get(req, res, includeBody) {
116117
117118 // if data is text/turtle, parse it
118119 if ( contentType === 'text/turtle' ) {
119- return parseLinkedData ( data ) ;
120+ // TODO this should be added as a middleware in the routes
121+ res . locals . turtleData = data ;
122+ return parseLinkedData ( req , res ) ;
120123 }
121124
122125 // Otherwise, just send the data
@@ -154,12 +157,14 @@ function get(req, res, includeBody) {
154157 }
155158 } ) ;
156159
157- var turtleData = $rdf . serialize (
160+ var data = $rdf . serialize (
158161 undefined ,
159162 globGraph ,
160163 null ,
161164 'text/turtle' ) ;
162- parseLinkedData ( turtleData ) ;
165+ // TODO this should be added as a middleware in the routes
166+ res . locals . turtleData = data ;
167+ return parseLinkedData ( req , res ) ;
163168 } ) ;
164169 }
165170
@@ -180,48 +185,49 @@ function get(req, res, includeBody) {
180185 return false ;
181186 }
182187 }
188+ }
183189
184- function parseLinkedData ( turtleData ) {
185- var accept = header . parseAcceptHeader ( req ) ;
186- var baseUri = file . filenameToBaseUri ( filename , uri , ldp . root ) ;
190+ function parseLinkedData ( req , res ) {
191+ var ldp = req . app . locals . ldp ;
192+ var filename = file . uriToFilename ( req . path , ldp . root ) ;
193+ var uri = file . uriBase ( req ) ;
194+ var turtleData = res . locals . turtleData ;
195+
196+ var accept = header . parseAcceptHeader ( req ) || 'text/turtle' ;
197+ var baseUri = file . filenameToBaseUri ( filename , uri , ldp . root ) ;
198+
199+ // Handle Turtle Accept header
200+ if ( accept === 'text/turtle' ||
201+ accept === 'text/n3' ||
202+ accept === 'application/turtle' ||
203+ accept === 'application/n3' ) {
204+ return res . status ( 200 )
205+ . set ( 'content-type' , accept )
206+ . send ( turtleData ) ;
207+ }
187208
188- // Handle Turtle Accept header
189- if ( accept === undefined || accept === null ) {
190- accept = 'text/turtle' ;
191- }
192- if ( accept === 'text/turtle' ||
193- accept === 'text/n3' ||
194- accept === 'application/turtle' ||
195- accept === 'application/n3' ) {
196- return res . status ( 200 )
197- . set ( 'content-type' , accept )
198- . send ( turtleData ) ;
199- }
209+ //Handle other file types
210+ var resourceGraph = $rdf . graph ( ) ;
211+ try {
212+ $rdf . parse ( turtleData , resourceGraph , baseUri , 'text/turtle' ) ;
213+ } catch ( err ) {
214+ debug ( "GET/HEAD -- Error parsing data: " + err ) ;
215+ return res
216+ . status ( 500 )
217+ . send ( err ) ;
218+ }
200219
201- //Handle other file types
202- var resourceGraph = $rdf . graph ( ) ;
203- try {
204- $rdf . parse ( turtleData , resourceGraph , baseUri , 'text/turtle' ) ;
205- } catch ( err ) {
206- debug ( "GET/HEAD -- Error parsing data: " + err ) ;
207- return res . status ( 500 ) . send ( err ) ;
220+ $rdf . serialize ( undefined , resourceGraph , null , accept , function ( err , result ) {
221+ if ( result === undefined || err ) {
222+ debug ( "GET/HEAD -- Serialization error: " + err ) ;
223+ return res . sendStatus ( 500 ) ;
208224 }
209225
210- $rdf . serialize (
211- undefined ,
212- resourceGraph ,
213- null ,
214- accept ,
215- function ( err , result ) {
216- if ( result === undefined || err ) {
217- debug ( "GET/HEAD -- Serialization error: " + err ) ;
218- return res . sendStatus ( 500 ) ;
219- } else {
220- res . set ( 'content-type' , accept ) ;
221- return res . status ( 200 ) . send ( result ) ;
222- }
223- } ) ;
224- }
226+ return res
227+ . status ( 200 )
228+ . set ( 'content-type' , accept )
229+ . send ( result ) ;
230+ } ) ;
225231}
226232
227233var globOptions = {
0 commit comments