@@ -18,8 +18,24 @@ const PATCH_PARSERS = {
1818 'text/n3' : require ( './patch/n3-patch-parser.js' )
1919}
2020
21+ // use media-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+
32+ function contentForNew ( contentType ) {
33+ let contentForNew = ''
34+ if ( contentType . includes ( 'ld+json' ) ) contentForNew = JSON . stringify ( '{}' )
35+ else if ( contentType . includes ( 'rdf+xml' ) ) contentForNew = '<rdf:RDF\n xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n\n</rdf:RDF>'
36+ return contentForNew
37+ }
38+
2339// Handles a PATCH request
2440async function patchHandler ( req , res , next ) {
2541 debug ( `PATCH -- ${ req . originalUrl } ` )
@@ -33,9 +49,9 @@ async function patchHandler (req, res, next) {
3349 // First check if the file already exists
3450 ( { path, contentType } = await ldp . resourceMapper . mapUrlToFile ( { url : req } ) )
3551 } catch ( err ) {
36- // If the file doesn't exist, request one to be created with the default content type
52+ // If the file doesn't exist, request to create one with the file media type as contentType
3753 ( { path, contentType } = await ldp . resourceMapper . mapUrlToFile (
38- { url : req , createIfNotExists : true , contentType : DEFAULT_FOR_NEW_CONTENT_TYPE } ) )
54+ { url : req , createIfNotExists : true , contentType : contentTypeForNew ( req ) } ) )
3955 // check if a folder with same name exists
4056 await ldp . checkItemName ( req )
4157 resourceExists = false
@@ -93,13 +109,14 @@ function readGraph (resource) {
93109 // If the file does not exist, assume empty contents
94110 // (it will be created after a successful patch)
95111 if ( err . code === 'ENOENT' ) {
96- fileContents = ''
112+ fileContents = contentForNew ( resource . contentType )
97113 // Fail on all other errors
98114 } else {
99115 return reject ( error ( 500 , `Original file read error: ${ err } ` ) )
100116 }
101117 }
102118 debug ( 'PATCH -- Read target file (%d bytes)' , fileContents . length )
119+ fileContents = resource . contentType . includes ( 'json' ) ? JSON . parse ( fileContents ) : fileContents
103120 resolve ( fileContents )
104121 } )
105122 )
@@ -177,23 +194,34 @@ function writeGraph (graph, resource, root, serverUri) {
177194 debug ( 'PATCH -- Writing patched file' )
178195 return new Promise ( ( resolve , reject ) => {
179196 const resourceSym = graph . sym ( resource . url )
180- const serialized = $rdf . serialize ( resourceSym , graph , resource . url , resource . contentType )
181-
182- // First check if we are above quota
183- overQuota ( root , serverUri ) . then ( ( isOverQuota ) => {
184- if ( isOverQuota ) {
185- return reject ( error ( 413 ,
186- 'User has exceeded their storage quota' ) )
187- }
188197
189- fs . writeFile ( resource . path , serialized , { encoding : 'utf8' } , function ( err ) {
190- if ( err ) {
191- return reject ( error ( 500 , `Failed to write file after patch: ${ err } ` ) )
198+ function doWrite ( serialized ) {
199+ // First check if we are above quota
200+ overQuota ( root , serverUri ) . then ( ( isOverQuota ) => {
201+ if ( isOverQuota ) {
202+ return reject ( error ( 413 ,
203+ 'User has exceeded their storage quota' ) )
192204 }
193- debug ( 'PATCH -- applied successfully' )
194- resolve ( 'Patch applied successfully.\n' )
205+
206+ fs . writeFile ( resource . path , serialized , { encoding : 'utf8' } , function ( err ) {
207+ if ( err ) {
208+ return reject ( error ( 500 , `Failed to write file after patch: ${ err } ` ) )
209+ }
210+ debug ( 'PATCH -- applied successfully' )
211+ resolve ( 'Patch applied successfully.\n' )
212+ } )
213+ } ) . catch ( ( ) => reject ( error ( 500 , 'Error finding user quota' ) ) )
214+ }
215+
216+ if ( resource . contentType === 'application/ld+json' ) {
217+ $rdf . serialize ( resourceSym , graph , resource . url , resource . contentType , function ( err , result ) {
218+ if ( err ) return reject ( error ( 500 , `Failed to serialize after patch: ${ err } ` ) )
219+ doWrite ( result )
195220 } )
196- } ) . catch ( ( ) => reject ( error ( 500 , 'Error finding user quota' ) ) )
221+ } else {
222+ const serialized = $rdf . serialize ( resourceSym , graph , resource . url , resource . contentType )
223+ doWrite ( serialized )
224+ }
197225 } )
198226}
199227
0 commit comments