Skip to content

Commit 1e59cba

Browse files
committed
Use ResourceMapper in ldp#delete
1 parent 98289a6 commit 1e59cba

File tree

3 files changed

+113
-64
lines changed

3 files changed

+113
-64
lines changed

lib/handlers/delete.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ function handler (req, res, next) {
66
debug('DELETE -- Request on' + req.originalUrl)
77

88
const ldp = req.app.locals.ldp
9-
ldp.delete(req.hostname, req.path, function (err) {
10-
if (err) {
9+
ldp.delete(req)
10+
.then(() => {
11+
debug('DELETE -- Ok.')
12+
res.sendStatus(200)
13+
next()
14+
})
15+
.catch(err => {
1116
debug('DELETE -- Failed to delete: ' + err)
12-
return next(err)
13-
}
14-
debug('DELETE -- Ok.')
15-
res.sendStatus(200)
16-
return next()
17-
})
17+
next(err)
18+
})
1819
}

lib/ldp.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -432,58 +432,58 @@ class LDP {
432432
)
433433
}
434434

435-
delete (host, resourcePath, callback) {
436-
const ldp = this
437-
const root = !ldp.multiuser ? ldp.root : ldp.root + host + '/'
438-
const filename = utils.uriToFilename(resourcePath, root)
439-
ldp.stat(filename).then(stats => {
440-
if (stats.isDirectory()) {
441-
return ldp.deleteContainer(filename, callback)
442-
} else {
443-
return ldp.deleteResource(filename, callback)
444-
}
445-
},
446-
err => callback(error(404, "Can't find " + err))
447-
)
435+
async delete (url) {
436+
const { path } = await this.resourceMapper.mapUrlToFile({ url })
437+
438+
// First check if the path points to a valid file
439+
let stats
440+
try {
441+
stats = await this.stat(path)
442+
} catch (err) {
443+
throw error(404, "Can't find " + err)
444+
}
445+
446+
// If so, delete the directory or file
447+
if (stats.isDirectory()) {
448+
return this.deleteContainer(path)
449+
} else {
450+
return this.deleteResource(path)
451+
}
448452
}
449453

450-
deleteContainer (directory, callback) {
451-
const self = this
454+
async deleteContainer (directory) {
452455
if (directory[ directory.length - 1 ] !== '/') {
453456
directory += '/'
454457
}
455458

456-
let countValid = 0
457-
fs.readdir(directory, function (err, list) {
458-
if (err) return callback(error(404, 'The container does not exist'))
459-
460-
if (list.indexOf(self.suffixMeta) > -1) {
461-
countValid++
462-
}
463-
464-
if (list.indexOf(self.suffixAcl) > -1) {
465-
countValid++
466-
}
459+
// Ensure the container exists
460+
let list
461+
try {
462+
list = await promisify(fs.readdir)(directory)
463+
} catch (err) {
464+
throw error(404, 'The container does not exist')
465+
}
467466

468-
if (list.length !== countValid) {
469-
return callback(error(409, 'Container is not empty'))
470-
}
467+
// Ensure the container is empty (we ignore .meta and .acl)
468+
if (list.some(file => file !== this.suffixMeta && file !== this.suffixAcl)) {
469+
throw error(409, 'Container is not empty')
470+
}
471471

472-
return rimraf(directory, function (err) {
473-
if (err) return callback(error(err, 'Failed to delete the container'))
474-
return callback(null)
475-
})
476-
})
472+
// Delete the directory recursively
473+
try {
474+
await promisify(rimraf)(directory)
475+
} catch (err) {
476+
throw error(err, 'Failed to delete the container')
477+
}
477478
}
478479

479-
deleteResource (filename, callback) {
480-
return fs.unlink(filename, function (err, data) {
481-
if (err) {
482-
debug.container('DELETE -- unlink() error: ' + err)
483-
return callback(error(err, 'Failed to delete resource'))
484-
}
485-
return callback(null, data)
486-
})
480+
async deleteResource (filename) {
481+
try {
482+
return promisify(fs.unlink)(filename)
483+
} catch (err) {
484+
debug.container('DELETE -- unlink() error: ' + err)
485+
throw error(err, 'Failed to delete resource')
486+
}
487487
}
488488

489489
getAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) {

test/integration/ldp-test.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var assert = require('chai').assert
1+
var chai = require('chai')
2+
var assert = chai.assert
3+
chai.use(require('chai-as-promised'))
24
var $rdf = require('rdflib')
35
var ns = require('solid-namespace')($rdf)
46
var LDP = require('../../lib/ldp')
@@ -153,21 +155,67 @@ describe('LDP', function () {
153155
})
154156

155157
describe('delete', function () {
156-
it.skip('should delete a file in an existing dir', function (done) {
158+
it('should error when deleting a non-existing file', () => {
159+
return assert.isRejected(ldp.delete('/resources/testPut.txt'))
160+
})
161+
162+
it.skip('should delete a file in an existing dir', async () => {
163+
// First create a dummy file
157164
var stream = stringToStream('hello world')
158-
ldp.put('/resources/testPut.txt', stream).then(() => {
159-
fs.stat(ldp.root + '/resources/testPut.txt', function (err) {
160-
if (err) {
161-
return done(err)
162-
}
163-
ldp.delete('localhost', '/resources/testPut.txt', function (err) {
164-
if (err) done(err)
165-
fs.stat(ldp.root + '/resources/testPut.txt', function (err) {
166-
return done(err ? null : new Error('file still exists'))
167-
})
168-
})
169-
})
165+
await ldp.put('/resources/testPut.txt', stream)
166+
// Make sure it exists
167+
fs.stat(ldp.root + '/resources/testPut.txt', function (err) {
168+
if (err) {
169+
throw err
170+
}
171+
})
172+
173+
// Now delete the dummy file
174+
await ldp.delete('/resources/testPut.txt')
175+
// Make sure it does not exist anymore
176+
fs.stat(ldp.root + '/resources/testPut.txt', function (err, s) {
177+
if (!err) {
178+
throw new Error('file still exists')
179+
}
180+
})
181+
})
182+
183+
it.skip('should fail to delete a non-empty folder', async () => {
184+
// First create a dummy file
185+
var stream = stringToStream('hello world')
186+
await ldp.put('/resources/dummy/testPutBlocking.txt', stream)
187+
// Make sure it exists
188+
fs.stat(ldp.root + '/resources/dummy/testPutBlocking.txt', function (err) {
189+
if (err) {
190+
throw err
191+
}
170192
})
193+
194+
// Now try to delete its folder
195+
return assert.isRejected(ldp.delete('/resources/dummy/'))
196+
})
197+
198+
it.skip('should fail to delete nested non-empty folders', async () => {
199+
// First create a dummy file
200+
var stream = stringToStream('hello world')
201+
await ldp.put('/resources/dummy/dummy2/testPutBlocking.txt', stream)
202+
// Make sure it exists
203+
fs.stat(ldp.root + '/resources/dummy/dummy2/testPutBlocking.txt', function (err) {
204+
if (err) {
205+
throw err
206+
}
207+
})
208+
209+
// Now try to delete its parent folder
210+
return assert.isRejected(ldp.delete('/resources/dummy/'))
211+
})
212+
213+
after(async function () {
214+
// Clean up after delete tests
215+
await ldp.delete('/resources/dummy/testPutBlocking.txt')
216+
await ldp.delete('/resources/dummy/dummy2/testPutBlocking.txt')
217+
await ldp.delete('/resources/dummy/dummy2/')
218+
await ldp.delete('/resources/dummy/')
171219
})
172220
})
173221
describe('listContainer', function () {

0 commit comments

Comments
 (0)