@@ -10,6 +10,7 @@ const utils = require('../utils.js')
1010const error = require ( '../http-error' )
1111const $rdf = require ( 'rdflib' )
1212const crypto = require ( 'crypto' )
13+ const overQuota = require ( '../utils' ) . overQuota
1314
1415const DEFAULT_TARGET_TYPE = 'text/turtle'
1516
@@ -53,7 +54,7 @@ function patchHandler (req, res, next) {
5354 ] )
5455 // Patch the graph and write it back to the file
5556 . then ( ( [ graph , patchObject ] ) => applyPatch ( patchObject , graph , target ) )
56- . then ( graph => writeGraph ( graph , target ) )
57+ . then ( graph => writeGraph ( graph , target , root , ldp . serverUri ) )
5758 // Send the result to the client
5859 . then ( result => { res . send ( result ) } )
5960 . then ( next , next )
@@ -137,19 +138,27 @@ function applyPatch (patchObject, graph, target) {
137138}
138139
139140// Writes the RDF graph to the given resource
140- function writeGraph ( graph , resource ) {
141+ function writeGraph ( graph , resource , root , serverUri ) {
141142 debug ( 'PATCH -- Writing patched file' )
142143 return new Promise ( ( resolve , reject ) => {
143144 const resourceSym = graph . sym ( resource . uri )
144145 const serialized = $rdf . serialize ( resourceSym , graph , resource . uri , resource . contentType )
145146
146- fs . writeFile ( resource . file , serialized , { encoding : 'utf8' } , function ( err ) {
147- if ( err ) {
148- return reject ( error ( 500 , `Failed to write file after patch: ${ err } ` ) )
147+ // First check if we are above quota
148+ overQuota ( root , serverUri ) . then ( ( isOverQuota ) => {
149+ if ( isOverQuota ) {
150+ return reject ( error ( 413 ,
151+ 'User has exceeded their storage quota' ) )
149152 }
150- debug ( 'PATCH -- applied successfully' )
151- resolve ( 'Patch applied successfully.\n' )
152- } )
153+
154+ fs . writeFile ( resource . file , serialized , { encoding : 'utf8' } , function ( err ) {
155+ if ( err ) {
156+ return reject ( error ( 500 , `Failed to write file after patch: ${ err } ` ) )
157+ }
158+ debug ( 'PATCH -- applied successfully' )
159+ resolve ( 'Patch applied successfully.\n' )
160+ } )
161+ } ) . catch ( ( ) => reject ( error ( 500 , 'Error finding user quota' ) ) )
153162 } )
154163}
155164
0 commit comments