@@ -132,19 +132,26 @@ async function handler (req, res, next) {
132
132
}
133
133
134
134
async 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
140
147
141
148
const globOptions = {
142
149
noext : true ,
143
150
nobrace : true ,
144
151
nodir : true
145
152
}
146
153
147
- glob ( filename , globOptions , function ( err , matches ) {
154
+ glob ( ` ${ folderPath } *` , globOptions , async ( err , matches ) => {
148
155
if ( err || matches . length === 0 ) {
149
156
debugGlob ( 'No files matching the pattern' )
150
157
return next ( error ( 404 , 'No files matching glob pattern' ) )
@@ -154,7 +161,7 @@ async function globHandler (req, res, next) {
154
161
const globGraph = $rdf . graph ( )
155
162
156
163
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 ) => {
158
165
const urlData = await ldp . resourceMapper . mapFileToUrl ( { path : match , hostname : req . hostname } )
159
166
fs . readFile ( match , { encoding : 'utf8' } , function ( err , fileData ) {
160
167
if ( err ) {
@@ -178,15 +185,14 @@ async function globHandler (req, res, next) {
178
185
} )
179
186
} )
180
187
} ) ) )
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 ( )
190
196
} )
191
197
}
192
198
@@ -198,7 +204,7 @@ function hasReadPermissions (file, req, res, callback) {
198
204
return callback ( true )
199
205
}
200
206
201
- const root = ldp . resourceMapper . getBasePath ( req . hostname )
207
+ const root = ldp . resourceMapper . resolveFilePath ( req . hostname )
202
208
const relativePath = '/' + _path . relative ( root , file )
203
209
res . locals . path = relativePath
204
210
allow ( 'Read' ) ( req , res , err => callback ( ! err ) )
0 commit comments