Skip to content

Commit f1a62f3

Browse files
committed
Merge pull request #322 from linkeddata/dz_refactor
Minor layout refactoring
2 parents 1125227 + 25df835 commit f1a62f3

File tree

5 files changed

+264
-222
lines changed

5 files changed

+264
-222
lines changed

lib/acl.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ ACL.prototype.can = function (user, mode, resource, callback, options) {
105105
})
106106
}
107107

108-
ACL.prototype.findAgentClass = function (graph, user, mode, resource, acl, callback) {
108+
ACL.prototype.findAgentClass = function (graph, user, mode, resource, acl,
109+
callback) {
109110
var self = this
110111

111112
// Agent class statement
@@ -131,7 +132,8 @@ ACL.prototype.findAgentClass = function (graph, user, mode, resource, acl, callb
131132
}, callback)
132133
}
133134

134-
ACL.prototype.findRule = function (graph, user, mode, resource, accessType, acl, callback, options) {
135+
ACL.prototype.findRule = function (graph, user, mode, resource, accessType, acl,
136+
callback, options) {
135137
var self = this
136138

137139
// TODO check if this is necessary
@@ -155,7 +157,8 @@ ACL.prototype.findRule = function (graph, user, mode, resource, accessType, acl,
155157
var statementSubject = statement.subject.uri
156158

157159
// Check for origin
158-
var matchOrigin = self.matchOrigin(graph, statementSubject, options.origin, options.host)
160+
var matchOrigin = self.matchOrigin(graph, statementSubject,
161+
options.origin, options.host)
159162
if (!matchOrigin) {
160163
debug('The request does not match the origin')
161164
return done(false)
@@ -164,7 +167,8 @@ ACL.prototype.findRule = function (graph, user, mode, resource, accessType, acl,
164167
// Check for accessTo/defaultForNew
165168
if (!self.isAcl(resource) || accessType === 'defaultForNew') {
166169
debug('Checking for accessType:' + accessType)
167-
var accesses = self.matchAccessType(graph, statementSubject, accessType, resource)
170+
var accesses = self.matchAccessType(graph, statementSubject, accessType,
171+
resource)
168172
if (!accesses) {
169173
debug('Cannot find accessType ' + accessType)
170174
return done(false)
@@ -189,7 +193,8 @@ ACL.prototype.findRule = function (graph, user, mode, resource, accessType, acl,
189193

190194
debug('Inspect agentClass')
191195
// Check for AgentClass
192-
return self.findAgentClass(graph, user, mode, resource, statementSubject, done)
196+
return self.findAgentClass(graph, user, mode, resource, statementSubject,
197+
done)
193198
},
194199
function (found) {
195200
if (!found) {
@@ -209,7 +214,8 @@ ACL.prototype.getMode = function getMode (graph, mode) {
209214
'http://www.w3.org/ns/auth/acl#' + mode)
210215
}
211216

212-
ACL.prototype.matchAccessType = function matchAccessType (graph, rule, accessType, uri) {
217+
ACL.prototype.matchAccessType = function matchAccessType (graph, rule,
218+
accessType, uri) {
213219
var self = this
214220
var matches = self.match(
215221
graph,
@@ -250,7 +256,8 @@ function possibleACLs (uri, suffix) {
250256
var first = S(uri).endsWith(suffix) ? uri : uri + suffix
251257
var urls = [first]
252258
var parsedUri = url.parse(uri)
253-
var baseUrl = (parsedUri.protocol ? parsedUri.protocol + '//' : '') + (parsedUri.host || '')
259+
var baseUrl = (parsedUri.protocol ? parsedUri.protocol + '//' : '') +
260+
(parsedUri.host || '')
254261
if (baseUrl + '/' === uri) {
255262
return urls
256263
}
@@ -301,18 +308,19 @@ function getUserId (req, callback) {
301308
}
302309

303310
var delegator = rdfVocab.debrack(onBehalfOf)
304-
verifyDelegator(req.hostname, delegator, req.session.userId, function (err, res) {
305-
if (err) {
306-
err.status = 500
307-
return callback(err)
308-
}
311+
verifyDelegator(req.hostname, delegator, req.session.userId,
312+
function (err, res) {
313+
if (err) {
314+
err.status = 500
315+
return callback(err)
316+
}
309317

310-
if (res) {
311-
debug('Request User ID (delegation) :' + delegator)
312-
return callback(null, delegator)
313-
}
314-
return callback(null, req.session.userId)
315-
})
318+
if (res) {
319+
debug('Request User ID (delegation) :' + delegator)
320+
return callback(null, delegator)
321+
}
322+
return callback(null, req.session.userId)
323+
})
316324
}
317325

318326
function verifyDelegator (host, ldp, baseUri, delegator, delegatee, callback) {
@@ -365,7 +373,9 @@ function allow (mode) {
365373
getUserId(req, function (err, userId) {
366374
if (err) return next(err)
367375

368-
var reqPath = res && res.locals && res.locals.path ? res.locals.path : req.path
376+
var reqPath = res && res.locals && res.locals.path
377+
? res.locals.path
378+
: req.path
369379
ldp.exists(req.hostname, reqPath, function (err, stat) {
370380
if (reqPath[reqPath.length - 1] !== '/' &&
371381
!err &&

lib/identity-provider.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
102102
graphs.push(self.putGraph(options.preferences, createPreferences(options)))
103103
}
104104
if (options.inbox) {
105-
graphs.push(self.putGraph(options.inbox + self.suffixAcl, createInboxAcl(options)))
105+
graphs.push(self.putGraph(options.inbox + self.suffixAcl,
106+
createInboxAcl(options)))
106107
}
107108
if (options.defaultContainers && options.defaultContainers.length > 0) {
108109
throw new Error('default containers is not supported yet')
@@ -382,10 +383,12 @@ IdentityProvider.prototype.get = function (req, res, next) {
382383
if (err && err.status === 404) {
383384
// TODO maybe a personalized page
384385
// or a redirect
385-
debug('Account on ' + req.hostname + ' is available from ' + req.originalUrl)
386+
debug('Account on ' + req.hostname + ' is available from ' +
387+
req.originalUrl)
386388
return res.sendStatus(404)
387389
} else {
388-
debug('Account on ' + req.hostname + ' is not available ' + req.originalUrl)
390+
debug('Account on ' + req.hostname + ' is not available ' +
391+
req.originalUrl)
389392
return next()
390393
}
391394
})
@@ -410,7 +413,8 @@ IdentityProvider.prototype.newCert = function (req, res, next) {
410413
debug('Requesting new cert for ' + options.webid)
411414

412415
if (req.session.userId !== options.webid) {
413-
debug('user is not logged in: ' + req.session.userId + ' is not ' + options.webid)
416+
debug('user is not logged in: ' + req.session.userId + ' is not ' +
417+
options.webid)
414418
var error = new Error('You are not logged in, so you can\'t create a certificate')
415419
error.status = 401
416420
return next(error)
@@ -423,7 +427,8 @@ IdentityProvider.prototype.newCert = function (req, res, next) {
423427
// Get a new cert
424428
webid('tls').generate({
425429
spkac: spkac,
426-
commonName: options.name + ' [on ' + options.host + ' created at ' + options.date.toDateString() + ']',
430+
commonName: options.name + ' [on ' + options.host + ' created at ' +
431+
options.date.toDateString() + ']',
427432
issuer: { commonName: options.host },
428433
duration: 10,
429434
agent: options.webid
@@ -465,32 +470,33 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
465470
var hostname = parsed.hostname
466471
var reqPath = parsed.path
467472

468-
self.store.get(hostname, reqPath, null, true, 'text/turtle', function (err, stream) {
469-
if (err) {
470-
debug('Cannot find WebID card')
471-
var notexists = new Error('Cannot find WebID card')
472-
notexists.status = 500
473-
return callback(notexists)
474-
}
475-
var data = ''
476-
stream
477-
.on('data', function (chunk) {
478-
data += chunk
479-
})
480-
.on('end', function () {
481-
// TODO make sure that uri is correct
482-
parse(data, uri, 'text/turtle', function (err, card) {
483-
if (err) {
484-
debug('WebId can\'t be parsed: ' + err.message)
485-
var invalid = new Error('You have an invalid WebID card')
486-
invalid.status = 500
487-
return callback(invalid)
488-
}
489-
490-
return callback(null, card)
473+
self.store.get(hostname, reqPath, null, true, 'text/turtle',
474+
function (err, stream) {
475+
if (err) {
476+
debug('Cannot find WebID card')
477+
var notexists = new Error('Cannot find WebID card')
478+
notexists.status = 500
479+
return callback(notexists)
480+
}
481+
var data = ''
482+
stream
483+
.on('data', function (chunk) {
484+
data += chunk
491485
})
492-
})
493-
})
486+
.on('end', function () {
487+
// TODO make sure that uri is correct
488+
parse(data, uri, 'text/turtle', function (err, card) {
489+
if (err) {
490+
debug('WebId can\'t be parsed: ' + err.message)
491+
var invalid = new Error('You have an invalid WebID card')
492+
invalid.status = 500
493+
return callback(invalid)
494+
}
495+
496+
return callback(null, card)
497+
})
498+
})
499+
})
494500
}
495501

496502
// Handle POST requests on account creation

lib/ldp-container.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
module.exports.addContainerStats = addContainerStats
2+
module.exports.addFile = addFile
3+
module.exports.addStats = addStats
4+
module.exports.getMetadataGraph = getMetadataGraph
5+
module.exports.readdir = readdir
6+
7+
var $rdf = require('rdflib')
8+
var debug = require('./debug')
9+
var error = require('./http-error')
10+
var fs = require('fs')
11+
var ns = require('./vocab/ns').ns
12+
var S = require('string')
13+
var turtleExtension = '.ttl'
14+
15+
function addContainerStats (ldp, filename, resourceGraph, next) {
16+
ldp.stat(filename, function (err, containerStats) {
17+
if (!err) {
18+
addStats(resourceGraph, '', containerStats)
19+
resourceGraph.add(
20+
resourceGraph.sym(''),
21+
ns.rdf('type'),
22+
ns.ldp('BasicContainer'))
23+
24+
resourceGraph.add(
25+
resourceGraph.sym(''),
26+
ns.rdf('type'),
27+
ns.ldp('Container'))
28+
}
29+
next()
30+
})
31+
}
32+
33+
function addFile (ldp, resourceGraph, baseUri, uri, container, file, callback) {
34+
// Skip .meta and .acl
35+
if (S(file).endsWith(ldp.suffixMeta) || S(file).endsWith(ldp.suffixAcl)) {
36+
return callback(null)
37+
}
38+
39+
// Get file stats
40+
ldp.stat(container + file, function (err, stats) {
41+
if (err) {
42+
// File does not exist, skip
43+
return callback(null)
44+
}
45+
46+
var fileSubject = file + (stats.isDirectory() ? '/' : '')
47+
// var fileBaseUri = utils.filenameToBaseUri(fileSubject, uri, root)
48+
49+
// Add fileStats to resource Graph
50+
addStats(resourceGraph, fileSubject, stats)
51+
52+
// Add to `contains` list
53+
resourceGraph.add(
54+
resourceGraph.sym(''),
55+
ns.ldp('contains'),
56+
resourceGraph.sym(fileSubject))
57+
58+
// Set up a metaFile path
59+
var metaFile = container + file +
60+
(stats.isDirectory() ? '/' : '') +
61+
(S(file).endsWith(turtleExtension) ? '' : ldp.suffixMeta)
62+
63+
getMetadataGraph(ldp, metaFile, baseUri, function (err, metadataGraph) {
64+
if (err) {
65+
metadataGraph = $rdf.graph()
66+
}
67+
68+
// Add Container or BasicContainer types
69+
if (stats.isDirectory()) {
70+
resourceGraph.add(
71+
metadataGraph.sym(fileSubject),
72+
ns.rdf('type'),
73+
ns.ldp('BasicContainer'))
74+
75+
resourceGraph.add(
76+
metadataGraph.sym(fileSubject),
77+
ns.rdf('type'),
78+
ns.ldp('Container'))
79+
}
80+
// Add generic LDP type
81+
resourceGraph.add(
82+
metadataGraph.sym(fileSubject),
83+
ns.rdf('type'),
84+
ns.ldp('Resource'))
85+
86+
// Add type from metadataGraph
87+
metadataGraph
88+
.statementsMatching(
89+
metadataGraph.sym(baseUri),
90+
ns.rdf('type'),
91+
undefined)
92+
.forEach(function (typeStatement) {
93+
// If the current is a file and its type is BasicContainer,
94+
// This is not possible, so do not infer its type!
95+
if (
96+
(
97+
typeStatement.object.uri !== ns.ldp('BasicContainer').uri &&
98+
typeStatement.object.uri !== ns.ldp('Container').uri
99+
) ||
100+
!stats.isFile()
101+
) {
102+
resourceGraph.add(
103+
resourceGraph.sym(fileSubject),
104+
typeStatement.predicate,
105+
typeStatement.object)
106+
}
107+
})
108+
109+
return callback(null)
110+
})
111+
})
112+
}
113+
114+
function addStats (resourceGraph, baseUri, stats) {
115+
resourceGraph.add(
116+
resourceGraph.sym(baseUri),
117+
ns.stat('mtime'),
118+
stats.mtime.getTime() / 1000)
119+
120+
resourceGraph.add(
121+
resourceGraph.sym(baseUri),
122+
ns.stat('size'),
123+
stats.size)
124+
}
125+
126+
function readdir (filename, callback) {
127+
debug.handlers('GET -- Reading directory')
128+
fs.readdir(filename, function (err, files) {
129+
if (err) {
130+
debug.handlers('GET -- Error reading files: ' + err)
131+
return callback(error(err, 'Can\'t read container'))
132+
}
133+
134+
debug.handlers('Files in directory: ' + files)
135+
return callback(null, files)
136+
})
137+
}
138+
139+
function getMetadataGraph (ldp, metaFile, fileBaseUri, callback) {
140+
ldp.stat(metaFile, function (err, metaStats) {
141+
if (err) {
142+
return callback(err)
143+
}
144+
145+
if (metaStats && metaStats.isFile()) {
146+
ldp.readFile(metaFile, function (err, rawMetadata) {
147+
if (err) {
148+
return callback(err)
149+
}
150+
151+
var metadataGraph = $rdf.graph()
152+
try {
153+
$rdf.parse(
154+
rawMetadata,
155+
metadataGraph,
156+
fileBaseUri,
157+
'text/turtle')
158+
} catch (dirErr) {
159+
return callback(error(err, 'Can\'t parse container metadata'))
160+
}
161+
return callback(null, metadataGraph)
162+
})
163+
} else {
164+
return callback(null, $rdf.graph())
165+
}
166+
})
167+
}

0 commit comments

Comments
 (0)