Skip to content

Commit d978f2c

Browse files
RubenVerborghrubensworks
authored andcommitted
Use ResourceMapper in ldp#readResource.
1 parent b809444 commit d978f2c

File tree

4 files changed

+28
-71
lines changed

4 files changed

+28
-71
lines changed

lib/handlers/allow.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = allow
22

33
const ACL = require('../acl-checker')
4-
const $rdf = require('rdflib')
54
const utils = require('../utils')
65
const debug = require('../debug.js').ACL
76
const LegacyResourceMapper = require('../legacy-resource-mapper')
@@ -72,19 +71,6 @@ function allow (mode) {
7271
*/
7372
function fetchFromLdp (mapper, ldp) {
7473
return function fetch (url, callback) {
75-
// Convert the URL into a filename
76-
mapper.mapUrlToFile({ url })
77-
// Read the file from disk
78-
.then(({ path }) => new Promise((resolve, reject) => {
79-
ldp.readFile(path, (e, c) => e ? reject(e) : resolve(c))
80-
}))
81-
// Parse the file as Turtle
82-
.then(body => {
83-
const graph = $rdf.graph()
84-
$rdf.parse(body, graph, url, 'text/turtle')
85-
return graph
86-
})
87-
// Return the ACL graph
88-
.then(graph => callback(null, graph), callback)
74+
ldp.getGraph(url).then(g => callback(null, g), callback)
8975
}
9076
}

lib/ldp-container.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports.addContainerStats = addContainerStats
22
module.exports.addFile = addFile
33
module.exports.addStats = addStats
4-
module.exports.getMetadataGraph = getMetadataGraph
54
module.exports.readdir = readdir
65

76
const $rdf = require('rdflib')
@@ -61,7 +60,7 @@ function addFile (ldp, resourceGraph, containerUri, reqUri, uri, container, file
6160
// Set up a metaFile path
6261
// Earlier code used a .ttl file as its own meta file, which
6362
// caused massive data files to parsed as part of deirectory listings just looking for type triples
64-
const metaFile = container + file + ldp.suffixMeta
63+
const metaFile = containerUri + file + ldp.suffixMeta
6564

6665
getMetadataGraph(ldp, metaFile, memberUri, function (err, metadataGraph) {
6766
if (err) {
@@ -160,25 +159,11 @@ function getMetadataGraph (ldp, metaFile, fileBaseUri, callback) {
160159
}
161160

162161
if (metaStats && metaStats.isFile()) {
163-
ldp.readFile(metaFile, function (err, rawMetadata) {
164-
if (err) {
165-
return callback(err)
166-
}
167-
168-
const metadataGraph = $rdf.graph()
169-
try {
170-
$rdf.parse(
171-
rawMetadata,
172-
metadataGraph,
173-
fileBaseUri,
174-
'text/turtle')
175-
} catch (dirErr) {
176-
return callback(error(err, 'Can\'t parse container metadata'))
177-
}
178-
return callback(null, metadataGraph)
179-
})
162+
ldp.getGraph(metaFile)
163+
.then(graph => callback(null, graph),
164+
err => callback(error(err, 'Can\'t parse container metadata')))
180165
} else {
181-
return callback(null, $rdf.graph())
166+
callback(null, $rdf.graph())
182167
}
183168
})
184169
}

lib/ldp.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,24 @@ class LDP {
109109
}
110110
}
111111

112-
readFile (filename, callback) {
113-
fs.readFile(
114-
filename,
115-
{ 'encoding': 'utf8' },
116-
function (err, data) {
117-
if (err) {
118-
return callback(error(err, "Can't read file"))
119-
}
120-
return callback(null, data)
121-
})
112+
async readResource (url) {
113+
const { path } = await this.resourceMapper.mapUrlToFile({ url })
114+
return new Promise((resolve, reject) => {
115+
fs.readFile(
116+
path,
117+
{ 'encoding': 'utf8' },
118+
(err, data) => {
119+
if (err) return reject(error(err, "Can't read file"))
120+
resolve(data)
121+
})
122+
})
122123
}
123124

124125
async readContainerMeta (url, callback) {
125126
if (url[ url.length - 1 ] !== '/') {
126127
url += '/'
127128
}
128-
url += this.suffixMeta
129-
130-
const { path } = await this.resourceMapper.mapUrlToFile({ url })
131-
return new Promise((resolve, reject) => {
132-
this.readFile(path, function (err, data) {
133-
if (err) return reject(error(err, "Can't read meta file"))
134-
resolve(data)
135-
})
136-
})
129+
return this.readResource(url + this.suffixMeta)
137130
}
138131

139132
listContainer (filename, reqUri, uri, containerData, contentType, callback) {
@@ -360,19 +353,15 @@ class LDP {
360353
* @return {Promise<Graph>}
361354
*/
362355
getGraph (uri, contentType = DEFAULT_CONTENT_TYPE) {
363-
const { path } = url.parse(uri)
364-
return this.graph(path, uri, contentType)
356+
return this.graph(uri, uri, contentType)
365357
}
366358

367359
async graph (url, baseUri, contentType = 'text/turtle') {
368-
const { path } = await this.resourceMapper.mapUrlToFile({ url })
360+
const body = await this.readResource(url)
369361
return new Promise((resolve, reject) => {
370-
this.readFile(path, (err, body) => {
371-
if (err) return reject(err)
372-
const graph = $rdf.graph()
373-
$rdf.parse(body, graph, baseUri, contentType,
374-
err => err ? reject(err) : resolve(graph))
375-
})
362+
const graph = $rdf.graph()
363+
$rdf.parse(body, graph, baseUri, contentType,
364+
err => err ? reject(err) : resolve(graph))
376365
})
377366
}
378367

test/integration/ldp-test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@ describe('LDP', function () {
2929
webid: false
3030
})
3131

32-
describe('readFile', function () {
33-
it('return 404 if file does not exist', function (done) {
34-
ldp.readFile('resources/unexistent.ttl', function (err) {
32+
describe('readResource', function () {
33+
it('return 404 if file does not exist', () => {
34+
return ldp.readResource('/resources/unexistent.ttl').catch(err => {
3535
assert.equal(err.status, 404)
36-
done()
3736
})
3837
})
3938

40-
it('return file if file exists', function (done) {
39+
it('return file if file exists', () => {
4140
// file can be empty as well
4241
write('hello world', 'fileExists.txt')
43-
ldp.readFile(path.join(__dirname, '../resources/fileExists.txt'), function (err, file) {
42+
return ldp.readResource('/resources/fileExists.txt').then(file => {
4443
rm('fileExists.txt')
45-
assert.notOk(err)
4644
assert.equal(file, 'hello world')
47-
done()
4845
})
4946
})
5047
})

0 commit comments

Comments
 (0)