|
1 | 1 | module.exports = allow |
2 | 2 |
|
| 3 | +const $rdf = require('rdflib') |
3 | 4 | const ACL = require('../acl-checker') |
4 | 5 | const debug = require('../debug.js').ACL |
| 6 | +const fs = require('fs') |
| 7 | +const { promisify } = require('util') |
| 8 | +const HTTPError = require('../http-error') |
5 | 9 |
|
6 | 10 | function allow (mode) { |
7 | 11 | return async function allowHandler (req, res, next) { |
@@ -35,9 +39,9 @@ function allow (mode) { |
35 | 39 |
|
36 | 40 | // Obtain and store the ACL of the requested resource |
37 | 41 | req.acl = new ACL(rootUrl + reqPath, { |
38 | | - origin: req.get('origin'), |
| 42 | + agentOrigin: req.get('origin'), |
39 | 43 | // host: req.get('host'), |
40 | | - fetch: fetchFromLdp(ldp.resourceMapper, ldp), |
| 44 | + fetch: fetchFromLdp(ldp.resourceMapper), |
41 | 45 | fetchGraph: (uri, options) => { |
42 | 46 | // first try loading from local fs |
43 | 47 | return ldp.getGraph(uri, options.contentType) |
@@ -69,14 +73,17 @@ function allow (mode) { |
69 | 73 | * - `callback(null, graph)` with the parsed RDF graph of the fetched resource |
70 | 74 | * @return {Function} Returns a `fetch(uri, callback)` handler |
71 | 75 | */ |
72 | | -function fetchFromLdp (mapper, ldp) { |
| 76 | +function fetchFromLdp (mapper) { |
73 | 77 | return async function fetch (url, graph = $rdf.graph()) { |
74 | 78 | // Convert the URL into a filename |
75 | | - const { path, contentType } = await mapper.mapUrlToFile({ url }) |
| 79 | + let path, contentType |
| 80 | + try { |
| 81 | + ({ path, contentType } = await mapper.mapUrlToFile({ url })) |
| 82 | + } catch (err) { |
| 83 | + throw new HTTPError(404, err) |
| 84 | + } |
76 | 85 | // Read the file from disk |
77 | | - const body = await new Promise((resolve, reject) => { |
78 | | - ldp.readFile(path, (e, c) => e ? reject(e) : resolve(c)) |
79 | | - }) |
| 86 | + const body = await promisify(fs.readFile)(path, {'encoding': 'utf8'}) |
80 | 87 | // Parse the file as Turtle |
81 | 88 | $rdf.parse(body, graph, url, contentType) |
82 | 89 | return graph |
|
0 commit comments