Skip to content

Commit b2a6435

Browse files
author
nicola
committed
removing sync from globHandler
1 parent 5fd9c9c commit b2a6435

2 files changed

Lines changed: 39 additions & 30 deletions

File tree

‎acl.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ var rdfVocab = require('./vocab/rdf.js');
1717

1818
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
1919

20-
function allow(mode, req) {
20+
function allow(mode, req, res) {
2121
var options = req.app.locals.ldp;
2222
var origin = req.get('origin');
2323
origin = origin ? origin : '';
2424

2525
var accessType = "accessTo";
2626

27-
var filepath = file.uriToFilename(req.path, options.root);
28-
var relativePath = file.uriToRelativeFilename(req.path, options.root);
27+
var reqPath = res && res.locals && res.locals.path ? res.locals.path : req.path;
28+
var filepath = file.uriToFilename(reqPath, options.root);
29+
var relativePath = file.uriToRelativeFilename(reqPath, options.root);
2930
var depth = relativePath.split('/');
3031

3132
//Get user from request

‎handlers/get.js‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var glob = require('glob');
77
var path = require('path');
88
var $rdf = require('rdflib');
99
var S = require('string');
10+
var async = require('async');
1011

1112
var debug = require('../logging').handlers;
1213
var acl = require('../acl.js');
@@ -67,8 +68,9 @@ function get(req, res, includeBody) {
6768
header.addLink(res, metaLink, 'describedBy');
6869

6970
ldp.stat(filename, function(err, stats) {
71+
// File does not exist
7072
if (err) {
71-
// File does not exist
73+
// Check in case it is a pattern, e.g. /*.js
7274
if (glob.hasMagic(filename)) {
7375
debug("GET/HEAD -- Glob request");
7476
return globHandler();
@@ -95,6 +97,7 @@ function get(req, res, includeBody) {
9597
}
9698
ldp.listContainer(filename, uri, data, function (err, data) {
9799
if (err) {
100+
debug('GET/HEAD -- Read error:' + err);
98101
return res
99102
.status(err.status)
100103
.send(err.message);
@@ -137,7 +140,9 @@ function get(req, res, includeBody) {
137140
}
138141

139142
// Otherwise, just send the data
140-
res.status(200).send(data);
143+
return res
144+
.status(200)
145+
.send(data);
141146
});
142147
}
143148
});
@@ -151,34 +156,37 @@ function get(req, res, includeBody) {
151156

152157
// Matches found
153158
var globGraph = $rdf.graph();
154-
matches.forEach(function(match) {
155-
try {
156-
var baseUri = file.filenameToBaseUri(match, uri, ldp.root);
157-
var fileData = fs.readFileSync(
158-
match,
159-
{ encoding: "utf8" });
160159

160+
async.each(matches, function(match, done) {
161+
var baseUri = file.filenameToBaseUri(match, uri, ldp.root);
162+
fs.readFile(match, {encoding: "utf8"}, function(err, fileData) {
163+
if (err) {
164+
debug('GET -- Error in globHandler' + err);
165+
return done(null);
166+
}
161167
if (S(match).endsWith(".ttl") && aclAllow(match)) {
162-
$rdf.parse(
163-
fileData,
164-
globGraph,
165-
baseUri,
166-
'text/turtle');
168+
try {
169+
$rdf.parse(
170+
fileData,
171+
globGraph,
172+
baseUri,
173+
'text/turtle');
174+
} catch(parseErr) {
175+
debug('GET -- Error in globHandler' + parseErr);
176+
}
167177
}
168-
} catch (readErr) {
169-
debug('GET -- Error in globHandler' + readErr);
170-
return;
171-
}
178+
return done(null);
179+
});
180+
}, function () {
181+
var data = $rdf.serialize(
182+
undefined,
183+
globGraph,
184+
null,
185+
'text/turtle');
186+
// TODO this should be added as a middleware in the routes
187+
res.locals.turtleData = data;
188+
return parseLinkedData(req, res);
172189
});
173-
174-
var data = $rdf.serialize(
175-
undefined,
176-
globGraph,
177-
null,
178-
'text/turtle');
179-
// TODO this should be added as a middleware in the routes
180-
res.locals.turtleData = data;
181-
return parseLinkedData(req, res);
182190
});
183191
}
184192

@@ -190,7 +198,7 @@ function get(req, res, includeBody) {
190198
var relativePath = '/' +
191199
path.relative(ldp.root, match);
192200

193-
req.path = relativePath;
201+
res.locals.path = relativePath;
194202
var allow = acl.allow("Read", req, res);
195203

196204
if (allow.status === 200) {

0 commit comments

Comments
 (0)