@@ -9,6 +9,7 @@ const debug = require('../debug').handlers
99const utils = require ( '../utils.js' )
1010const error = require ( '../http-error' )
1111const $rdf = require ( 'rdflib' )
12+ const crypto = require ( 'crypto' )
1213
1314const DEFAULT_TARGET_TYPE = 'text/turtle'
1415
@@ -23,31 +24,32 @@ function patchHandler (req, res, next) {
2324 debug ( 'PATCH -- ' + req . originalUrl )
2425 res . header ( 'MS-Author-Via' , 'SPARQL' )
2526
26- // Obtain details of the patch document
27- const patch = {
28- text : req . body ? req . body . toString ( ) : '' ,
29- contentType : ( req . get ( 'content-type' ) || '' ) . match ( / ^ [ ^ ; \s ] * / ) [ 0 ]
30- }
31- const patchGraph = PATCHERS [ patch . contentType ]
32- if ( ! patchGraph ) {
33- return next ( error ( 415 , 'Unknown patch content type: ' + patch . contentType ) )
34- }
35- debug ( 'PATCH -- Received patch (%d bytes, %s)' , patch . text . length , patch . contentType )
36-
3727 // Obtain details of the target resource
3828 const ldp = req . app . locals . ldp
3929 const root = ! ldp . idp ? ldp . root : ldp . root + req . hostname + '/'
40- const target = {
41- file : utils . uriToFilename ( req . path , root ) ,
42- uri : utils . uriAbs ( req ) + req . originalUrl
43- }
30+ const target = { }
31+ target . file = utils . uriToFilename ( req . path , root )
32+ target . uri = utils . uriAbs ( req ) + req . originalUrl
4433 target . contentType = mime . lookup ( target . file ) || DEFAULT_TARGET_TYPE
4534 debug ( 'PATCH -- Target <%s> (%s)' , target . uri , target . contentType )
4635
36+ // Obtain details of the patch document
37+ const patch = { }
38+ patch . text = req . body ? req . body . toString ( ) : ''
39+ patch . uri = `${ target . uri } #patch-${ hash ( patch . text ) } `
40+ patch . contentType = ( req . get ( 'content-type' ) || '' ) . match ( / ^ [ ^ ; \s ] * / ) [ 0 ]
41+ debug ( 'PATCH -- Received patch (%d bytes, %s)' , patch . text . length , patch . contentType )
42+
43+ // Find the appropriate patcher for the given content type
44+ const patchGraph = PATCHERS [ patch . contentType ]
45+ if ( ! patchGraph ) {
46+ return next ( error ( 415 , 'Unknown patch content type: ' + patch . contentType ) )
47+ }
48+
4749 // Read the RDF graph to be patched from the file
4850 readGraph ( target )
4951 // Patch the graph and write it back to the file
50- . then ( graph => patchGraph ( graph , target . uri , patch . text ) )
52+ . then ( graph => patchGraph ( graph , target . uri , patch . uri , patch . text ) )
5153 . then ( graph => writeGraph ( graph , target ) )
5254 // Send the result to the client
5355 . then ( result => { res . send ( result ) } )
@@ -108,3 +110,8 @@ function writeGraph (graph, resource) {
108110 } )
109111 } )
110112}
113+
114+ // Creates a hash of the given text
115+ function hash ( text ) {
116+ return crypto . createHash ( 'md5' ) . update ( text ) . digest ( 'hex' )
117+ }
0 commit comments