Skip to content

Commit b809444

Browse files
RubenVerborghrubensworks
authored andcommitted
Use ResourceMapper in ldp#readContainerMeta.
1 parent c185b67 commit b809444

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

lib/ldp.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,18 @@ class LDP {
121121
})
122122
}
123123

124-
readContainerMeta (directory, callback) {
125-
const ldp = this
126-
127-
if (directory[ directory.length - 1 ] !== '/') {
128-
directory += '/'
124+
async readContainerMeta (url, callback) {
125+
if (url[ url.length - 1 ] !== '/') {
126+
url += '/'
129127
}
128+
url += this.suffixMeta
130129

131-
ldp.readFile(directory + ldp.suffixMeta, function (err, data) {
132-
if (err) {
133-
return callback(error(err, "Can't read meta file"))
134-
}
135-
136-
return callback(null, data)
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+
})
137136
})
138137
}
139138

@@ -409,10 +408,9 @@ class LDP {
409408

410409
// Found a container
411410
if (stats.isDirectory()) {
412-
return ldp.readContainerMeta(filename, function (err, metaFile) {
413-
if (err) {
414-
metaFile = ''
415-
}
411+
return ldp.readContainerMeta(reqPath)
412+
.catch(() => '')
413+
.then(metaFile => {
416414
const absContainerUri = baseUri + reqPath
417415
ldp.listContainer(filename, absContainerUri, baseUri, metaFile, contentType,
418416
function (err, data) {

test/integration/ldp-test.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,28 @@ describe('LDP', function () {
4949
})
5050
})
5151

52-
describe('readContainerMeta', function () {
53-
it('should return 404 if .meta is not found', function (done) {
54-
ldp.readContainerMeta('resources/', function (err) {
52+
describe('readContainerMeta', () => {
53+
it('should return 404 if .meta is not found', () => {
54+
return ldp.readContainerMeta('/resources/').catch(err => {
5555
assert.equal(err.status, 404)
56-
done()
5756
})
5857
})
5958

60-
it('should return content if metaFile exists', function (done) {
59+
it('should return content if metaFile exists', () => {
6160
// file can be empty as well
6261
write('This function just reads this, does not parse it', '.meta')
63-
ldp.readContainerMeta(path.join(__dirname, '../resources/'), function (err, metaFile) {
62+
return ldp.readContainerMeta('/resources/').then(metaFile => {
6463
rm('.meta')
65-
assert.notOk(err)
6664
assert.equal(metaFile, 'This function just reads this, does not parse it')
67-
done()
6865
})
6966
})
7067

71-
it('should work also if trailing `/` is not passed', function (done) {
68+
it('should work also if trailing `/` is not passed', () => {
7269
// file can be empty as well
7370
write('This function just reads this, does not parse it', '.meta')
74-
ldp.readContainerMeta(path.join(__dirname, '../resources'), function (err, metaFile) {
71+
return ldp.readContainerMeta('/resources').then(metaFile => {
7572
rm('.meta')
76-
assert.notOk(err)
7773
assert.equal(metaFile, 'This function just reads this, does not parse it')
78-
done()
7974
})
8075
})
8176
})

0 commit comments

Comments
 (0)