Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions google-apis-core/lib/google/apis/core/storage_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@ def send_upload_command(client)
request_header = header.dup
request_header[CONTENT_RANGE_HEADER] = get_content_range_header current_chunk_size
request_header[CONTENT_LENGTH_HEADER] = current_chunk_size.to_s
last_chunk= remaining_content_size <= current_chunk_size
Comment thread
shubhangi-google marked this conversation as resolved.
Outdated
hash_data= JSON.parse(body)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The body variable (which is self.body of the command) can be an empty string if no request_object (metadata) is provided. JSON.parse('') will raise a JSON::ParserError, causing the upload to crash. This line needs to safely handle cases where body is empty or not valid JSON.

Furthermore, if the body contains static metadata like checksums, parsing it in every send_upload_command call (which occurs for each chunk) is inefficient. Consider parsing this data once, perhaps in the prepare! method, and storing the formatted_string in an instance variable for reuse.

hash_data = body.to_s.empty? ? {} : JSON.parse(body)

target_keys = ["crc32c", "md5Hash"]
formatted_string = hash_data.slice(*target_keys).map { |key, value| "#{key}=#{value}" }.join(',')
request_header['X-Goog-Hash'] = formatted_string if last_chunk
Comment thread
shubhangi-google marked this conversation as resolved.
Outdated

chunk_body =
if @upload_chunk_size == 0
upload_io
else
StringIO.new(upload_io.read(current_chunk_size))
end

binding.pry if last_chunk
Comment thread
shubhangi-google marked this conversation as resolved.
Outdated
response = client.put(@upload_url, chunk_body, request_header)

result = process_response(response.status.to_i, response.headers, response.body)
Expand All @@ -191,7 +197,7 @@ def send_upload_command(client)
success(result)
rescue => e
logger.warn {
"error occured please use uploadId-#{response.headers['X-GUploader-UploadID']} to resume your upload"
"error occurred please use uploadId-#{response.headers['X-GUploader-UploadID']} to resume your upload , error==> #{e}"
} unless response.nil?
upload_io.pos = @offset
error(e, rethrow: true)
Expand Down Expand Up @@ -246,7 +252,7 @@ def cancel_resumable_upload(client)

def handle_resumable_upload_http_response_codes(response)
code = response.status.to_i

binding.pry
Comment thread
shubhangi-google marked this conversation as resolved.
Outdated
case code
when 308
if response.headers['Range']
Expand All @@ -262,6 +268,7 @@ def handle_resumable_upload_http_response_codes(response)
@upload_incomplete = false
when 200, 201
# Upload is complete.
binding.pry
Comment thread
shubhangi-google marked this conversation as resolved.
Outdated
@upload_incomplete = false
else
logger.debug { sprintf("Unexpected response: #{response.status.to_i} - #{response.body}") }
Expand Down