@@ -114,16 +114,14 @@ UpdateResponse updateResumableUpload(String uploadId, HttpHeaderParser.ContentRa
114114 throw failAndThrow ("Attempted to update a non-existent resumable: " + uid );
115115 }
116116
117- if (contentRange .hasRange () == false ) {
118- // Content-Range: */... is a status check https://cloud.google.com/storage/docs/performing-resumable-uploads#status-check
117+ ResumableUpload valueToReturn = existing ;
118+
119+ // Handle the request, a range indicates a chunk of data was submitted
120+ if (contentRange .hasRange ()) {
119121 if (existing .completed ) {
120- updateResponse .set (new UpdateResponse (RestStatus .OK .getStatus (), calculateRangeHeader (blobs .get (existing .path ))));
121- } else {
122- final HttpHeaderParser .Range range = calculateRangeHeader (existing );
123- updateResponse .set (new UpdateResponse (RESUME_INCOMPLETE , range ));
122+ throw failAndThrow ("Attempted to write more to a completed resumable upload" );
124123 }
125- return existing ;
126- } else {
124+
127125 if (contentRange .start () > contentRange .end ()) {
128126 throw failAndThrow ("Invalid content range " + contentRange );
129127 }
@@ -143,16 +141,20 @@ UpdateResponse updateResumableUpload(String uploadId, HttpHeaderParser.ContentRa
143141 existing .contents ,
144142 requestBody .slice (offset , requestBody .length ())
145143 );
146- // We just received the last chunk, update the blob and remove the resumable upload from the map
147- if (contentRange .hasSize () && updatedContent .length () == contentRange .size ()) {
148- updateBlob (existing .path (), existing .ifGenerationMatch , updatedContent );
149- updateResponse .set (new UpdateResponse (RestStatus .OK .getStatus (), null ));
150- return existing .update (BytesArray .EMPTY , true );
151- }
152- final ResumableUpload updated = existing .update (updatedContent , false );
153- updateResponse .set (new UpdateResponse (RESUME_INCOMPLETE , calculateRangeHeader (updated )));
154- return updated ;
144+ valueToReturn = existing .update (updatedContent , false );
145+ }
146+
147+ // Next we determine the response
148+ if (valueToReturn .completed ) {
149+ updateResponse .set (new UpdateResponse (RestStatus .OK .getStatus (), calculateRangeHeader (valueToReturn )));
150+ } else if (contentRange .hasSize () && contentRange .size () == valueToReturn .contents .length ()) {
151+ valueToReturn = existing .update (valueToReturn .contents , true );
152+ updateBlob (valueToReturn .path (), valueToReturn .ifGenerationMatch (), valueToReturn .contents );
153+ updateResponse .set (new UpdateResponse (RestStatus .OK .getStatus (), calculateRangeHeader (valueToReturn )));
154+ } else {
155+ updateResponse .set (new UpdateResponse (RESUME_INCOMPLETE , calculateRangeHeader (valueToReturn )));
155156 }
157+ return valueToReturn ;
156158 });
157159 assert updateResponse .get () != null : "Should always produce an update response" ;
158160 return updateResponse .get ();
0 commit comments