Skip to content

Commit 2ad05e8

Browse files
committed
Promisify ldp#post
1 parent 3a7b199 commit 2ad05e8

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

lib/handlers/post.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ function handler (req, res, next) {
8686
const extension = mimeType in extensions ? `.${extensions[mimeType][0]}` : ''
8787

8888
ldp.post(req.hostname, containerPath, req,
89-
{ slug, extension, container: links.isBasicContainer },
90-
function (err, resourcePath) {
91-
if (err) {
92-
return next(err)
93-
}
89+
{ slug, extension, container: links.isBasicContainer }).then(
90+
resourcePath => {
9491
debug('File stored in ' + resourcePath)
9592
header.addLinks(res, links)
9693
res.set('Location', resourcePath)
9794
res.sendStatus(201)
9895
next()
99-
})
96+
},
97+
err => next(err))
10098
}
10199
}

lib/ldp.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -163,39 +163,35 @@ class LDP {
163163
}
164164
}
165165

166-
post (host, containerPath, stream, { container, slug, extension }, callback) {
166+
async post (host, containerPath, stream, { container, slug, extension }) {
167167
const ldp = this
168168
debug.handlers('POST -- On parent: ' + containerPath)
169169
// prepare slug
170170
if (slug) {
171171
slug = decodeURIComponent(slug)
172172
if (slug.match(/\/|\||:/)) {
173-
callback(error(400, 'The name of new file POSTed may not contain : | or /'))
174-
return
173+
throw error(400, 'The name of new file POSTed may not contain : | or /')
175174
}
176175
}
177176
// Containers should not receive an extension
178177
if (container) {
179178
extension = ''
180179
}
181180
// TODO: possibly package this in ldp.post
182-
ldp.getAvailablePath(host, containerPath, { slug, extension })
183-
.then(async resourcePath => {
184-
debug.handlers('POST -- Will create at: ' + resourcePath)
185-
let originalPath = resourcePath
186-
if (container) {
187-
// Create directory by an LDP PUT to the container's .meta resource
188-
resourcePath = path.join(originalPath, ldp.suffixMeta)
189-
if (originalPath && !originalPath.endsWith('/')) {
190-
originalPath += '/'
191-
}
192-
}
193-
const { url: putUrl } = await this.resourceMapper.mapFileToUrl(
194-
{ path: this.resourceMapper._rootPath + resourcePath, hostname: host })
195-
await ldp.put(putUrl, stream)
196-
callback(null, originalPath)
197-
})
198-
.catch((err) => callback(err))
181+
let resourcePath = await ldp.getAvailablePath(host, containerPath, { slug, extension })
182+
debug.handlers('POST -- Will create at: ' + resourcePath)
183+
let originalPath = resourcePath
184+
if (container) {
185+
// Create directory by an LDP PUT to the container's .meta resource
186+
resourcePath = path.join(originalPath, ldp.suffixMeta)
187+
if (originalPath && !originalPath.endsWith('/')) {
188+
originalPath += '/'
189+
}
190+
}
191+
const { url: putUrl } = await this.resourceMapper.mapFileToUrl(
192+
{ path: this.resourceMapper._rootPath + resourcePath, hostname: host })
193+
await ldp.put(putUrl, stream)
194+
return originalPath
199195
}
200196

201197
/**

0 commit comments

Comments
 (0)