Skip to content

Commit 90c218a

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Move patch writing to generic PATCH handler.
This reduces the task of individual patch handlers to parsing the patch and applying it.
1 parent 3d05378 commit 90c218a

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

lib/handlers/patch.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function patchHandler (req, res, next) {
3232
res.header('MS-Author-Via', 'SPARQL')
3333

3434
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'
35-
var filename = utils.uriToFilename(req.path, root)
36-
var targetContentType = mime.lookup(filename) || DEFAULT_CONTENT_TYPE
35+
var targetFile = utils.uriToFilename(req.path, root)
36+
var targetContentType = mime.lookup(targetFile) || DEFAULT_CONTENT_TYPE
3737
var patchContentType = req.get('content-type')
3838
? req.get('content-type').split(';')[0].trim() // Ignore parameters
3939
: ''
@@ -42,29 +42,25 @@ function patchHandler (req, res, next) {
4242
debug('PATCH -- Content-type ' + patchContentType + ' patching target ' + targetContentType + ' <' + targetURI + '>')
4343

4444
// Obtain a patcher for the given patch type
45-
const patch = PATCHERS[patchContentType]
46-
if (!patch) {
45+
const patchGraph = PATCHERS[patchContentType]
46+
if (!patchGraph) {
4747
return next(error(415, 'Unknown patch content type: ' + patchContentType))
4848
}
4949

50-
// Read the RDF graph to be patched
51-
readGraph(filename, targetURI).then((targetKB) => {
52-
// Patch the target graph
53-
patch(targetKB, filename, targetURI, req.text, function (err, result) {
54-
if (err) {
55-
throw err
56-
}
57-
res.send(result)
58-
next()
59-
})
60-
})
61-
.catch(next)
50+
// Read the RDF graph to be patched from the file
51+
readGraph(targetFile, targetURI, targetContentType)
52+
// Patch the graph and write it back to the file
53+
.then(targetKB => patchGraph(targetKB, targetFile, targetURI, req.text))
54+
.then(targetKB => writeGraph(targetKB, targetFile, targetURI, targetContentType))
55+
// Send the result to the client
56+
.then(result => { res.send(result) })
57+
.then(next, next)
6258
}
6359

6460
// Reads the RDF graph in the given file with the corresponding URI
65-
function readGraph (resourceFile, resourceURI) {
61+
function readGraph (resourceFile, resourceURI, contentType) {
6662
// Read the file
67-
return new Promise((resolve, reject) => {
63+
return new Promise((resolve, reject) =>
6864
fs.readFile(resourceFile, {encoding: 'utf8'}, function (err, fileContents) {
6965
if (err) {
7066
// If the file does not exist, assume empty contents
@@ -79,11 +75,10 @@ function readGraph (resourceFile, resourceURI) {
7975
debug('PATCH -- Read target file (%d bytes)', fileContents.length)
8076
resolve(fileContents)
8177
})
82-
})
78+
)
8379
// Parse the file
8480
.then((fileContents) => {
8581
const graph = $rdf.graph()
86-
const contentType = mime.lookup(resourceFile) || DEFAULT_CONTENT_TYPE
8782
debug('PATCH -- Reading %s with content type %s', resourceURI, contentType)
8883
try {
8984
$rdf.parse(fileContents, graph, resourceURI, contentType)
@@ -94,3 +89,19 @@ function readGraph (resourceFile, resourceURI) {
9489
return graph
9590
})
9691
}
92+
93+
// Writes the RDF graph to the given file
94+
function writeGraph (graph, resourceFile, resourceURI, contentType) {
95+
return new Promise((resolve, reject) => {
96+
const resource = graph.sym(resourceURI)
97+
const serialized = $rdf.serialize(resource, graph, resourceURI, contentType)
98+
99+
fs.writeFile(resourceFile, serialized, {encoding: 'utf8'}, function (err) {
100+
if (err) {
101+
return reject(error(500, 'Failed to write file back after patch: ' + err))
102+
}
103+
debug('PATCH -- applied OK (sync)')
104+
resolve('Patch applied OK\n')
105+
})
106+
})
107+
}
Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
module.exports = patch
22

3-
var mime = require('mime-types')
4-
var fs = require('fs')
53
var $rdf = require('rdflib')
64
var debug = require('../../debug').handlers
75
var error = require('../../http-error')
86

9-
const DEFAULT_CONTENT_TYPE = 'text/turtle'
7+
function patch (targetKB, filename, targetURI, text) {
8+
return new Promise((resolve, reject) => {
9+
var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
10+
var patchKB = $rdf.graph()
1011

11-
function patch (targetKB, filename, targetURI, text, callback) {
12-
var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
13-
var patchKB = $rdf.graph()
14-
var targetContentType = mime.lookup(filename) || DEFAULT_CONTENT_TYPE
15-
16-
debug('PATCH -- parsing patch ...')
17-
var patchObject
18-
try {
19-
// Must parse relative to document's base address but patch doc should get diff URI
20-
patchObject = $rdf.sparqlUpdateParser(text, patchKB, patchURI)
21-
} catch (e) {
22-
return callback(error(400, 'Patch format syntax error:\n' + e + '\n'))
23-
}
24-
debug('PATCH -- reading target file ...')
25-
26-
var target = patchKB.sym(targetURI)
27-
targetKB.applyPatch(patchObject, target, function (err) {
28-
if (err) {
29-
var message = err.message || err // returns string at the moment
30-
debug('PATCH FAILED. Returning 409. Message: \'' + message + '\'')
31-
return callback(error(409, 'Error when applying the patch'))
12+
debug('PATCH -- parsing patch ...')
13+
var patchObject
14+
try {
15+
// Must parse relative to document's base address but patch doc should get diff URI
16+
patchObject = $rdf.sparqlUpdateParser(text, patchKB, patchURI)
17+
} catch (e) {
18+
return reject(error(400, 'Patch format syntax error:\n' + e + '\n'))
3219
}
33-
debug('PATCH -- Patched. Writeback URI base ' + targetURI)
34-
var data = $rdf.serialize(target, targetKB, targetURI, targetContentType)
35-
// debug('Writeback data: ' + data)
20+
debug('PATCH -- reading target file ...')
3621

37-
fs.writeFile(filename, data, {encoding: 'utf8'}, function (err, data) {
22+
var target = patchKB.sym(targetURI)
23+
targetKB.applyPatch(patchObject, target, function (err) {
3824
if (err) {
39-
return callback(error(500, 'Failed to write file back after patch: ' + err))
25+
var message = err.message || err // returns string at the moment
26+
debug('PATCH FAILED. Returning 409. Message: \'' + message + '\'')
27+
return reject(error(409, 'Error when applying the patch'))
4028
}
41-
debug('PATCH -- applied OK (sync)')
42-
return callback(null, 'Patch applied OK\n')
29+
resolve(targetKB)
4330
})
4431
})
4532
}

0 commit comments

Comments
 (0)