lib/vcloud-rest/connection.rb
def upload_file(uploadURL, uploadFile, progressUrl, config={})
        raise ::IOError, "#{uploadFile} not found." unless File.exists?(uploadFile)

        # Set chunksize to 10M if not specified otherwise
        chunkSize = (config[:chunksize] || 10485760)

        # Set progress bar to default format if not specified otherwise
        progressBarFormat = (config[:progressbar_format] || "%e <%B> %p%% %t")

        # Set progress bar length to 120 if not specified otherwise
        progressBarLength = (config[:progressbar_length] || 120)

        # Open our file for upload
        uploadFileHandle = File.new(uploadFile, "rb" )
        fileName = File.basename(uploadFileHandle)

        progressBarTitle = "Uploading: " + uploadFile.to_s

        # Create a progressbar object if progress bar is enabled
        if config[:progressbar_enable] == true && uploadFileHandle.size.to_i > chunkSize
          progressbar = ProgressBar.create(
            :title => progressBarTitle,
            :starting_at => 0,
            :total => uploadFileHandle.size.to_i,
            :length => progressBarLength,
            :format => progressBarFormat
          )
        else
          @logger.info progressBarTitle
        end
        # Create a new HTTP client
        clnt = HTTPClient.new

        # Disable SSL cert verification
        clnt.ssl_config.verify_mode=(OpenSSL::SSL::VERIFY_NONE)

        # Suppress SSL depth message
        clnt.ssl_config.verify_callback=proc{ |ok, ctx|; true };

        # Perform ranged upload until the file reaches its end
        until uploadFileHandle.eof?

          # Create ranges for this chunk upload
          rangeStart = uploadFileHandle.pos
          rangeStop = uploadFileHandle.pos.to_i + chunkSize

          # Read current chunk
          fileContent = uploadFileHandle.read(chunkSize)

          # If statement to handle last chunk transfer if is > than filesize
          if rangeStop.to_i > uploadFileHandle.size.to_i
            contentRange = "bytes #{rangeStart.to_s}-#{uploadFileHandle.size.to_s}/#{uploadFileHandle.size.to_s}"
            rangeLen = uploadFileHandle.size.to_i - rangeStart.to_i
          else
            contentRange = "bytes #{rangeStart.to_s}-#{rangeStop.to_s}/#{uploadFileHandle.size.to_s}"
            rangeLen = rangeStop.to_i - rangeStart.to_i
          end

          # Build headers
          extheader = {
            'x-vcloud-authorization' => @auth_key,
            'Content-Range' => contentRange,
            'Content-Length' => rangeLen.to_s
          }

          begin
            uploadRequest = "#{@host_url}#{uploadURL}"
            connection = clnt.request('PUT', uploadRequest, nil, fileContent, extheader)

            if config[:progressbar_enable] == true && uploadFileHandle.size.to_i > chunkSize
              params = {
                'method' => :get,
                'command' => progressUrl
              }
              response, headers = send_request(params)

              response.css("Files File [name='#{fileName}']").each do |file|
                progressbar.progress=file[:bytesTransferred].to_i
              end
            end
          rescue
            retryTime = (config[:retry_time] || 5)
            @logger.warn "Range #{contentRange} failed to upload, retrying the chunk in #{retryTime.to_s} seconds, to stop the action press CTRL+C."
            sleep retryTime.to_i
            retry
          end
        end
        uploadFileHandle.close
      end
def upload_file(uploadURL, uploadFile, progressUrl, config={})
        raise ::IOError, "#{uploadFile} not found." unless File.exists?(uploadFile)

        # Set chunksize to 10M if not specified otherwise
        chunkSize = (config[:chunksize] || 10485760)

        # Set progress bar to default format if not specified otherwise
        progressBarFormat = (config[:progressbar_format] || "%e <%B> %p%% %t")

        # Set progress bar length to 120 if not specified otherwise
        progressBarLength = (config[:progressbar_length] || 120)

        # Open our file for upload
        uploadFileHandle = File.new(uploadFile, "rb" )
        fileName = File.basename(uploadFileHandle)

        progressBarTitle = "Uploading: " + uploadFile.to_s

        # Create a progressbar object if progress bar is enabled
        if config[:progressbar_enable] == true && uploadFileHandle.size.to_i > chunkSize
          progressbar = ProgressBar.create(
            :title => progressBarTitle,
            :starting_at => 0,
            :total => uploadFileHandle.size.to_i,
            :length => progressBarLength,
            :format => progressBarFormat
          )
        else
          @logger.info progressBarTitle
        end
        # Create a new HTTP client
        clnt = HTTPClient.new

        # Disable SSL cert verification
        clnt.ssl_config.verify_mode=(OpenSSL::SSL::VERIFY_NONE)

        # Suppress SSL depth message
        clnt.ssl_config.verify_callback=proc{ |ok, ctx|; true };

        # Perform ranged upload until the file reaches its end
        until uploadFileHandle.eof?

          # Create ranges for this chunk upload
          rangeStart = uploadFileHandle.pos
          rangeStop = uploadFileHandle.pos.to_i + chunkSize

          # Read current chunk
          fileContent = uploadFileHandle.read(chunkSize)

          # If statement to handle last chunk transfer if is > than filesize
          if rangeStop.to_i > uploadFileHandle.size.to_i
            contentRange = "bytes #{rangeStart.to_s}-#{uploadFileHandle.size.to_s}/#{uploadFileHandle.size.to_s}"
            rangeLen = uploadFileHandle.size.to_i - rangeStart.to_i
          else
            contentRange = "bytes #{rangeStart.to_s}-#{rangeStop.to_s}/#{uploadFileHandle.size.to_s}"
            rangeLen = rangeStop.to_i - rangeStart.to_i
          end

          # Build headers
          extheader = {
            'x-vcloud-authorization' => @auth_key,
            'Content-Range' => contentRange,
            'Content-Length' => rangeLen.to_s
          }

          begin
            uploadRequest = "#{@host_url}#{uploadURL}"
            connection = clnt.request('PUT', uploadRequest, nil, fileContent, extheader)

            if config[:progressbar_enable] == true && uploadFileHandle.size.to_i > chunkSize
              params = {
                'method' => :get,
                'command' => progressUrl
              }
              response, headers = send_request(params)

              response.css("Files File [name='#{fileName}']").each do |file|
                progressbar.progress=file[:bytesTransferred].to_i
              end
            end
          rescue
            retryTime = (config[:retry_time] || 5)
            @logger.warn "Range #{contentRange} failed to upload, retrying the chunk in #{retryTime.to_s} seconds, to stop the action press CTRL+C."
            sleep retryTime.to_i
            retry
          end
        end
        uploadFileHandle.close
      end
Upload a large file in configurable chunks, output an optional progressbar