Skip to content

Commit 2f1c74f

Browse files
author
nicola
committed
globhandler now async
1 parent b2a6435 commit 2f1c74f

1 file changed

Lines changed: 64 additions & 55 deletions

File tree

‎handlers/get.js‎

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function get(req, res, includeBody) {
7373
// Check in case it is a pattern, e.g. /*.js
7474
if (glob.hasMagic(filename)) {
7575
debug("GET/HEAD -- Glob request");
76-
return globHandler();
76+
return globHandler(req, res);
7777
}
7878
debug('GET/HEAD -- Read error: ' + err);
7979
return res
@@ -147,65 +147,78 @@ function get(req, res, includeBody) {
147147
}
148148
});
149149

150-
function globHandler() {
151-
glob(filename, globOptions, function(err, matches) {
152-
if (err || matches.length === 0) {
153-
debug("GET/HEAD -- No files matching the pattern");
154-
return res.sendStatus(404);
155-
}
150+
}
156151

157-
// Matches found
158-
var globGraph = $rdf.graph();
152+
function globHandler(req, res) {
153+
var ldp = req.app.locals.ldp;
154+
var filename = file.uriToFilename(req.path, ldp.root);
155+
var uri = file.uriBase(req);
159156

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-
}
167-
if (S(match).endsWith(".ttl") && aclAllow(match)) {
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-
}
177-
}
157+
var globOptions = {
158+
noext: true,
159+
nobrace: true
160+
};
161+
162+
glob(filename, globOptions, function(err, matches) {
163+
if (err || matches.length === 0) {
164+
debug("GET/HEAD -- No files matching the pattern");
165+
return res.sendStatus(404);
166+
}
167+
168+
// Matches found
169+
var globGraph = $rdf.graph();
170+
171+
async.each(matches, function(match, done) {
172+
var baseUri = file.filenameToBaseUri(match, uri, ldp.root);
173+
fs.readFile(match, {encoding: "utf8"}, function(err, fileData) {
174+
if (err) {
175+
debug('GET -- Error in globHandler' + err);
178176
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);
177+
}
178+
if (!S(match).endsWith(".ttl") || !aclAllow(match, req, res)) {
179+
return done(null);
180+
}
181+
try {
182+
$rdf.parse(
183+
fileData,
184+
globGraph,
185+
baseUri,
186+
'text/turtle');
187+
} catch(parseErr) {
188+
debug('GET -- Error in globHandler' + parseErr);
189+
}
190+
return done(null);
189191
});
192+
}, function () {
193+
var data = $rdf.serialize(
194+
undefined,
195+
globGraph,
196+
null,
197+
'text/turtle');
198+
// TODO this should be added as a middleware in the routes
199+
res.locals.turtleData = data;
200+
return parseLinkedData(req, res);
190201
});
191-
}
202+
});
203+
}
192204

193-
function aclAllow(match) {
194-
if (!ldp.webid) {
195-
return true;
196-
}
205+
function aclAllow(match, req, res) {
206+
var ldp = req.app.locals.ldp;
207+
208+
if (!ldp.webid) {
209+
return true;
210+
}
197211

198-
var relativePath = '/' +
199-
path.relative(ldp.root, match);
212+
var relativePath = '/' +
213+
path.relative(ldp.root, match);
200214

201-
res.locals.path = relativePath;
202-
var allow = acl.allow("Read", req, res);
215+
res.locals.path = relativePath;
216+
var allow = acl.allow("Read", req, res);
203217

204-
if (allow.status === 200) {
205-
return true;
206-
} else {
207-
return false;
208-
}
218+
if (allow.status === 200) {
219+
return true;
220+
} else {
221+
return false;
209222
}
210223
}
211224

@@ -252,10 +265,6 @@ function parseLinkedData(req, res) {
252265
});
253266
}
254267

255-
var globOptions = {
256-
noext: true,
257-
nobrace: true
258-
};
259268

260269
function getHandler(req, res) {
261270
get(req, res, true);

0 commit comments

Comments
 (0)