@@ -18,8 +18,17 @@ const PATCH_PARSERS = {
1818 'text/n3' : require ( './patch/n3-patch-parser.js' )
1919}
2020
21+ // use mime-type as contentType for new rdf resource
2122const DEFAULT_FOR_NEW_CONTENT_TYPE = 'text/turtle'
2223
24+ function contentTypeForNew ( req ) {
25+ let contentTypeForNew = DEFAULT_FOR_NEW_CONTENT_TYPE
26+ if ( req . path . endsWith ( '.jsonld' ) ) contentTypeForNew = 'application/ld+json'
27+ else if ( req . path . endsWith ( '.n3' ) ) contentTypeForNew = 'text/n3'
28+ else if ( req . path . endsWith ( '.rdf' ) ) contentTypeForNew = 'application/rdf+xml'
29+ return contentTypeForNew
30+ }
31+
2332// Handles a PATCH request
2433async function patchHandler ( req , res , next ) {
2534 debug ( `PATCH -- ${ req . originalUrl } ` )
@@ -33,9 +42,9 @@ async function patchHandler (req, res, next) {
3342 // First check if the file already exists
3443 ( { path, contentType } = await ldp . resourceMapper . mapUrlToFile ( { url : req } ) )
3544 } catch ( err ) {
36- // If the file doesn't exist, request one to be created with the default content type
45+ // If the file doesn't exist, request one to be created with the file mime type as contentType
3746 ( { path, contentType } = await ldp . resourceMapper . mapUrlToFile (
38- { url : req , createIfNotExists : true , contentType : DEFAULT_FOR_NEW_CONTENT_TYPE } ) )
47+ { url : req , createIfNotExists : true , contentType : contentTypeForNew ( req ) } ) )
3948 // check if a folder with same name exists
4049 await ldp . checkItemName ( req )
4150 resourceExists = false
@@ -93,13 +102,14 @@ function readGraph (resource) {
93102 // If the file does not exist, assume empty contents
94103 // (it will be created after a successful patch)
95104 if ( err . code === 'ENOENT' ) {
96- fileContents = ''
105+ fileContents = resource . contentType . includes ( 'json' ) ? JSON . stringify ( '{}' ) : ''
97106 // Fail on all other errors
98107 } else {
99108 return reject ( error ( 500 , `Original file read error: ${ err } ` ) )
100109 }
101110 }
102111 debug ( 'PATCH -- Read target file (%d bytes)' , fileContents . length )
112+ fileContents = resource . contentType . includes ( 'json' ) ? JSON . parse ( fileContents ) : fileContents
103113 resolve ( fileContents )
104114 } )
105115 )
0 commit comments