@@ -18,8 +18,24 @@ const PATCH_PARSERS = {
18
18
'text/n3' : require ( './patch/n3-patch-parser.js' )
19
19
}
20
20
21
+ // use media-type as contentType for new RDF resource
21
22
const DEFAULT_FOR_NEW_CONTENT_TYPE = 'text/turtle'
22
23
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
+
23
39
// Handles a PATCH request
24
40
async function patchHandler ( req , res , next ) {
25
41
debug ( `PATCH -- ${ req . originalUrl } ` )
@@ -33,9 +49,9 @@ async function patchHandler (req, res, next) {
33
49
// First check if the file already exists
34
50
( { path, contentType } = await ldp . resourceMapper . mapUrlToFile ( { url : req } ) )
35
51
} 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
37
53
( { 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 ) } ) )
39
55
// check if a folder with same name exists
40
56
await ldp . checkItemName ( req )
41
57
resourceExists = false
@@ -93,13 +109,14 @@ function readGraph (resource) {
93
109
// If the file does not exist, assume empty contents
94
110
// (it will be created after a successful patch)
95
111
if ( err . code === 'ENOENT' ) {
96
- fileContents = ''
112
+ fileContents = contentForNew ( resource . contentType )
97
113
// Fail on all other errors
98
114
} else {
99
115
return reject ( error ( 500 , `Original file read error: ${ err } ` ) )
100
116
}
101
117
}
102
118
debug ( 'PATCH -- Read target file (%d bytes)' , fileContents . length )
119
+ fileContents = resource . contentType . includes ( 'json' ) ? JSON . parse ( fileContents ) : fileContents
103
120
resolve ( fileContents )
104
121
} )
105
122
)
@@ -177,23 +194,34 @@ function writeGraph (graph, resource, root, serverUri) {
177
194
debug ( 'PATCH -- Writing patched file' )
178
195
return new Promise ( ( resolve , reject ) => {
179
196
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
- }
188
197
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' ) )
192
204
}
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 )
195
220
} )
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
+ }
197
225
} )
198
226
}
199
227
0 commit comments