@@ -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+ }
0 commit comments