@@ -132,19 +132,26 @@ async function handler (req, res, next) {
132132}
133133
134134async function globHandler ( req , res , next ) {
135- const ldp = req . app . locals . ldp
136- // TODO: This is a hack, that does not check if the target file exists, as this is quite complex with globbing.
137- // TODO: Proper support for this is not implemented, as globbing support might be removed in the future.
138- const filename = ldp . resourceMapper . getFullPath ( req )
139- const requestUri = ( await ldp . resourceMapper . mapFileToUrl ( { path : filename , hostname : req . hostname } ) ) . url
135+ const { ldp } = req . app . locals
136+
137+ // Ensure this is a glob for all files in a single folder
138+ // https://github.com/solid/solid-spec/pull/148
139+ const requestUrl = await ldp . resourceMapper . getRequestUrl ( req )
140+ if ( ! / ^ [ ^ * ] + \/ \* $ / . test ( requestUrl ) ) {
141+ return next ( error ( 404 , 'Unsupported glob pattern' ) )
142+ }
143+
144+ // Extract the folder on the file system from the URL glob
145+ const folderUrl = requestUrl . substr ( 0 , requestUrl . length - 1 )
146+ const folderPath = ( await ldp . resourceMapper . mapUrlToFile ( { url : folderUrl , searchIndex : false } ) ) . path
140147
141148 const globOptions = {
142149 noext : true ,
143150 nobrace : true ,
144151 nodir : true
145152 }
146153
147- glob ( filename , globOptions , function ( err , matches ) {
154+ glob ( ` ${ folderPath } *` , globOptions , async ( err , matches ) => {
148155 if ( err || matches . length === 0 ) {
149156 debugGlob ( 'No files matching the pattern' )
150157 return next ( error ( 404 , 'No files matching glob pattern' ) )
@@ -154,7 +161,7 @@ async function globHandler (req, res, next) {
154161 const globGraph = $rdf . graph ( )
155162
156163 debugGlob ( 'found matches ' + matches )
157- Promise . all ( matches . map ( match => new Promise ( async ( resolve , reject ) => {
164+ await Promise . all ( matches . map ( match => new Promise ( async ( resolve , reject ) => {
158165 const urlData = await ldp . resourceMapper . mapFileToUrl ( { path : match , hostname : req . hostname } )
159166 fs . readFile ( match , { encoding : 'utf8' } , function ( err , fileData ) {
160167 if ( err ) {
@@ -178,15 +185,14 @@ async function globHandler (req, res, next) {
178185 } )
179186 } )
180187 } ) ) )
181- . then ( ( ) => {
182- const data = $rdf . serialize ( undefined , globGraph , requestUri , 'text/turtle' )
183- // TODO this should be added as a middleware in the routes
184- res . setHeader ( 'Content-Type' , 'text/turtle' )
185- debugGlob ( 'returning turtle' )
186-
187- res . send ( data )
188- return next ( )
189- } )
188+
189+ const data = $rdf . serialize ( undefined , globGraph , requestUrl , 'text/turtle' )
190+ // TODO this should be added as a middleware in the routes
191+ res . setHeader ( 'Content-Type' , 'text/turtle' )
192+ debugGlob ( 'returning turtle' )
193+
194+ res . send ( data )
195+ next ( )
190196 } )
191197}
192198
@@ -198,7 +204,7 @@ function hasReadPermissions (file, req, res, callback) {
198204 return callback ( true )
199205 }
200206
201- const root = ldp . resourceMapper . getBasePath ( req . hostname )
207+ const root = ldp . resourceMapper . resolveFilePath ( req . hostname )
202208 const relativePath = '/' + _path . relative ( root , file )
203209 res . locals . path = relativePath
204210 allow ( 'Read' ) ( req , res , err => callback ( ! err ) )
0 commit comments