@@ -3,7 +3,6 @@ module.exports = allow
33var ACL = require ( '../acl-checker' )
44var $rdf = require ( 'rdflib' )
55var url = require ( 'url' )
6- var async = require ( 'async' )
76var utils = require ( '../utils' )
87
98function allow ( mode ) {
@@ -59,31 +58,30 @@ function allow (mode) {
5958 */
6059function fetchDocument ( host , ldp , baseUri ) {
6160 return function fetch ( uri , callback ) {
62- var graph = $rdf . graph ( )
63- async . waterfall ( [
64- function readFile ( cb ) {
65- // If local request, slice off the initial baseUri
66- // S(uri).chompLeft(baseUri).s
67- var newPath = uri . startsWith ( baseUri )
68- ? uri . slice ( baseUri . length )
69- : uri
70- // Determine the root file system folder to look in
71- // TODO prettify this
72- var root = ! ldp . idp ? ldp . root : ldp . root + host + '/'
73- // Derive the file path for the resource
74- var documentPath = utils . uriToFilename ( newPath , root )
75- var documentUri = url . parse ( documentPath )
76- documentPath = documentUri . pathname
77- return ldp . readFile ( documentPath , cb )
78- } ,
79- function parseFile ( body , cb ) {
80- try {
81- $rdf . parse ( body , graph , uri , 'text/turtle' )
82- } catch ( err ) {
83- return cb ( err , graph )
84- }
85- return cb ( null , graph )
86- }
87- ] , callback )
61+ readFile ( uri , host , ldp , baseUri ) . then ( body => {
62+ const graph = $rdf . graph ( )
63+ $rdf . parse ( body , graph , uri , 'text/turtle' )
64+ return graph
65+ } )
66+ . then ( graph => callback ( null , graph ) , callback )
8867 }
8968}
69+
70+ // Reads the given file, returning its contents
71+ function readFile ( uri , host , ldp , baseUri ) {
72+ return new Promise ( ( resolve , reject ) => {
73+ // If local request, slice off the initial baseUri
74+ // S(uri).chompLeft(baseUri).s
75+ var newPath = uri . startsWith ( baseUri )
76+ ? uri . slice ( baseUri . length )
77+ : uri
78+ // Determine the root file system folder to look in
79+ // TODO prettify this
80+ var root = ! ldp . idp ? ldp . root : ldp . root + host + '/'
81+ // Derive the file path for the resource
82+ var documentPath = utils . uriToFilename ( newPath , root )
83+ var documentUri = url . parse ( documentPath )
84+ documentPath = documentUri . pathname
85+ ldp . readFile ( documentPath , ( e , c ) => e ? reject ( e ) : resolve ( c ) )
86+ } )
87+ }
0 commit comments