Skip to content

Commit 98289a6

Browse files
committed
Use ResourceMapper in ldp#put
1 parent 9c81fdc commit 98289a6

File tree

6 files changed

+116
-125
lines changed

6 files changed

+116
-125
lines changed

lib/handlers/post.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ function handler (req, res, next) {
5454
debug('receving multiple files')
5555

5656
const busboy = new Busboy({ headers: req.headers })
57-
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
57+
busboy.on('file', async function (fieldname, file, filename, encoding, mimetype) {
5858
debug('One file received via multipart: ' + filename)
59-
ldp.put(
60-
req.hostname,
61-
path.join(containerPath, filename),
62-
file,
63-
function (err) {
64-
if (err) {
65-
busboy.emit('error', err)
66-
}
67-
})
59+
const { url: putUrl } = await ldp.resourceMapper.mapFileToUrl(
60+
{ path: ldp.resourceMapper._rootPath + path.join(containerPath, filename), hostname: req.hostname })
61+
try {
62+
await ldp.put(putUrl, file)
63+
} catch (err) {
64+
busboy.emit('error', err)
65+
}
6866
})
6967
busboy.on('error', function (err) {
7068
debug('Error receiving the file: ' + err.message)

lib/handlers/put.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ function handler (req, res, next) {
77
debug(req.originalUrl)
88
res.header('MS-Author-Via', 'SPARQL')
99

10-
ldp.put(req.hostname, req.path, req, function (err) {
11-
if (err) {
10+
ldp.put(req, req)
11+
.then(() => {
12+
debug('succeded putting the file')
13+
14+
res.sendStatus(201)
15+
return next()
16+
})
17+
.catch(err => {
1218
debug('error putting the file:' + err.message)
1319
err.message = 'Can\'t write file: ' + err.message
1420
return next(err)
15-
}
16-
17-
debug('succeded putting the file')
18-
19-
res.sendStatus(201)
20-
return next()
21-
})
21+
})
2222
}

lib/ldp.js

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,23 @@ class LDP {
191191
extension = ''
192192
}
193193
// TODO: possibly package this in ldp.post
194-
ldp.getAvailablePath(host, containerPath, { slug, extension }).then(resourcePath => {
195-
debug.handlers('POST -- Will create at: ' + resourcePath)
196-
let originalPath = resourcePath
197-
if (container) {
198-
// Create directory by an LDP PUT to the container's .meta resource
199-
resourcePath = path.join(originalPath, ldp.suffixMeta)
200-
if (originalPath && !originalPath.endsWith('/')) {
201-
originalPath += '/'
194+
ldp.getAvailablePath(host, containerPath, { slug, extension })
195+
.then(async resourcePath => {
196+
debug.handlers('POST -- Will create at: ' + resourcePath)
197+
let originalPath = resourcePath
198+
if (container) {
199+
// Create directory by an LDP PUT to the container's .meta resource
200+
resourcePath = path.join(originalPath, ldp.suffixMeta)
201+
if (originalPath && !originalPath.endsWith('/')) {
202+
originalPath += '/'
203+
}
202204
}
203-
}
204-
ldp.put(host, resourcePath, stream, function (err) {
205-
if (err) callback(err)
205+
const { url: putUrl } = await this.resourceMapper.mapFileToUrl(
206+
{ path: this.resourceMapper._rootPath + resourcePath, hostname: host })
207+
await ldp.put(putUrl, stream)
206208
callback(null, originalPath)
207209
})
208-
})
210+
.catch((err) => callback(err))
209211
}
210212

211213
/**
@@ -226,63 +228,54 @@ class LDP {
226228
*
227229
* @return {Promise<Graph>}
228230
*/
229-
putGraph (graph, uri, contentType = DEFAULT_CONTENT_TYPE) {
230-
return new Promise((resolve, reject) => {
231-
let parsedUri = url.parse(uri)
232-
let hostname = parsedUri.hostname
233-
let path = parsedUri.pathname
234-
235-
serialize(graph, uri, contentType)
236-
.then((content) => {
237-
let stream = stringToStream(content)
238-
239-
this.put(hostname, path, stream, (error) => {
240-
if (error) { return reject(error) }
241-
242-
resolve(graph)
243-
})
244-
})
245-
.catch(reject)
246-
})
231+
async putGraph (graph, uri, contentType = DEFAULT_CONTENT_TYPE) {
232+
const { path } = url.parse(uri)
233+
const content = await serialize(graph, uri, contentType)
234+
let stream = stringToStream(content)
235+
return await this.put(path, stream)
247236
}
248237

249-
put (host, resourcePath, stream, callback) {
250-
const ldp = this
251-
const root = !ldp.multiuser ? ldp.root : ldp.root + host + '/'
252-
const filePath = utils.uriToFilename(resourcePath, root, host)
238+
async put (url, stream) {
239+
const { path: filePath } = await this.resourceMapper.mapUrlToFile({ url })
253240

254241
// PUT requests not supported on containers. Use POST instead
255242
if (filePath.endsWith('/')) {
256-
return callback(error(409,
257-
'PUT not supported on containers, use POST instead'))
243+
throw error(409,
244+
'PUT not supported on containers, use POST instead')
258245
}
259246

260247
// First check if we are above quota
261-
overQuota(root, this.serverUri).then((isOverQuota) => {
262-
if (isOverQuota) {
263-
return callback(error(413,
264-
'User has exceeded their storage quota'))
265-
}
248+
let isOverQuota
249+
try {
250+
isOverQuota = await overQuota(this.resourceMapper.rootPath, this.serverUri)
251+
} catch (err) {
252+
throw error(500, 'Error finding user quota')
253+
}
254+
if (isOverQuota) {
255+
throw error(413, 'User has exceeded their storage quota')
256+
}
266257

267-
// Second, create the enclosing directory, if necessary
268-
const dirName = path.dirname(filePath)
269-
mkdirp(dirName, (err) => {
270-
if (err) {
271-
debug.handlers('PUT -- Error creating directory: ' + err)
272-
return callback(error(err,
273-
'Failed to create the path to the new resource'))
274-
}
275-
// Directory created, now write the file
276-
const file = stream.pipe(fs.createWriteStream(filePath))
277-
file.on('error', function () {
278-
callback(error(500, 'Error writing data'))
279-
})
280-
file.on('finish', function () {
281-
debug.handlers('PUT -- Wrote data to: ' + filePath)
282-
callback(null)
283-
})
258+
// Second, create the enclosing directory, if necessary
259+
const dirName = path.dirname(filePath)
260+
try {
261+
await promisify(mkdirp)(dirName)
262+
} catch (err) {
263+
debug.handlers('PUT -- Error creating directory: ' + err)
264+
throw error(err,
265+
'Failed to create the path to the new resource')
266+
}
267+
268+
// Directory created, now write the file
269+
return await new Promise((resolve, reject) => {
270+
const file = stream.pipe(fs.createWriteStream(filePath))
271+
file.on('error', function () {
272+
reject(error(500, 'Error writing data'))
273+
})
274+
file.on('finish', function () {
275+
debug.handlers('PUT -- Wrote data to: ' + filePath)
276+
resolve(null)
284277
})
285-
}).catch(() => callback(error(500, 'Error finding user quota')))
278+
})
286279
}
287280

288281
exists (hostname, path, callback) {

lib/resource-mapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ResourceMapper {
2929
}
3030
}
3131

32+
get rootPath() {
33+
return this._rootPath
34+
}
35+
3236
// Maps the request for a given resource and representation format to a server file
3337
async mapUrlToFile ({ url, contentType, createIfNotExists }) {
3438
const fullPath = this._getFullPath(url)

0 commit comments

Comments
 (0)