(req, res, next)
| 169 | } |
| 170 | |
| 171 | async function globHandler (req, res, next) { |
| 172 | const { ldp } = req.app.locals |
| 173 | |
| 174 | // Ensure this is a glob for all files in a single folder |
| 175 | // https://github.com/solid/solid-spec/pull/148 |
| 176 | const requestUrl = await ldp.resourceMapper.getRequestUrl(req) |
| 177 | if (!/^[^*]+\/\*$/.test(requestUrl)) { |
| 178 | return next(HTTPError(404, 'Unsupported glob pattern')) |
| 179 | } |
| 180 | |
| 181 | // Extract the folder on the file system from the URL glob |
| 182 | const folderUrl = requestUrl.substr(0, requestUrl.length - 1) |
| 183 | const folderPath = (await ldp.resourceMapper.mapUrlToFile({ url: folderUrl, searchIndex: false })).path |
| 184 | |
| 185 | const globOptions = { |
| 186 | noext: true, |
| 187 | nobrace: true, |
| 188 | nodir: true |
| 189 | } |
| 190 | |
| 191 | try { |
| 192 | const matches = await glob(`${folderPath}*`, globOptions) |
| 193 | if (matches.length === 0) { |
| 194 | debugGlob('No files matching the pattern') |
| 195 | return next(HTTPError(404, 'No files matching glob pattern')) |
| 196 | } |
| 197 | |
| 198 | // Matches found |
| 199 | const globGraph = $rdf.graph() |
| 200 | |
| 201 | debugGlob('found matches ' + matches) |
| 202 | await Promise.all(matches.map(match => new Promise(async (resolve, reject) => { |
| 203 | const urlData = await ldp.resourceMapper.mapFileToUrl({ path: match, hostname: req.hostname }) |
| 204 | fs.readFile(match, { encoding: 'utf8' }, function (err, fileData) { |
| 205 | if (err) { |
| 206 | debugGlob('error ' + err) |
| 207 | return resolve() |
| 208 | } |
| 209 | // Files should be Turtle |
| 210 | if (urlData.contentType !== 'text/turtle') { |
| 211 | return resolve() |
| 212 | } |
| 213 | // The agent should have Read access to the file |
| 214 | hasReadPermissions(match, req, res, function (allowed) { |
| 215 | if (allowed) { |
| 216 | try { |
| 217 | $rdf.parse(fileData, globGraph, urlData.url, 'text/turtle') |
| 218 | } catch (parseErr) { |
| 219 | debugGlob(`error parsing ${match}: ${parseErr}`) |
| 220 | } |
| 221 | } |
| 222 | return resolve() |
| 223 | }) |
| 224 | }) |
| 225 | }))) |
| 226 | |
| 227 | const data = $rdf.serialize(undefined, globGraph, requestUrl, 'text/turtle') |
| 228 | // TODO this should be added as a middleware in the routes |
no test coverage detected