Skip to content

Commit 2af9560

Browse files
committed
Allow patch handler to map resources that do no exist yet
1 parent f996761 commit 2af9560

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/handlers/patch.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ const PATCH_PARSERS = {
1616
'text/n3': require('./patch/n3-patch-parser.js')
1717
}
1818

19+
const DEFAULT_CONTENT_TYPE = 'text/turtle'
20+
1921
// Handles a PATCH request
2022
async function patchHandler (req, res, next) {
2123
debug(`PATCH -- ${req.originalUrl}`)
2224
res.header('MS-Author-Via', 'SPARQL')
2325
try {
2426
// Obtain details of the target resource
2527
const ldp = req.app.locals.ldp
26-
const { path, contentType } = await ldp.resourceMapper.mapUrlToFile({ url: req })
28+
let path, contentType;
29+
try {
30+
// First check if the file already exists
31+
({ path, contentType } = await ldp.resourceMapper.mapUrlToFile({ url: req }))
32+
} catch (err) {
33+
// If the file doesn't exist, request one to be created with the default content type
34+
({ path, contentType } = await ldp.resourceMapper.mapUrlToFile(
35+
{ url: req, createIfNotExists: true, contentType: DEFAULT_CONTENT_TYPE }))
36+
}
2737
const { url } = await ldp.resourceMapper.mapFileToUrl({ path, hostname: req.hostname })
2838
const resource = { path, contentType, url }
2939
debug('PATCH -- Target <%s> (%s)', url, contentType)

0 commit comments

Comments
 (0)