class Fog::Proxmox::MultipartBody

An IO-like multipart body that streams a file between a prefix and suffix.

Attributes

size[R]

Public Class Methods

new(prefix, file, suffix) click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 24
def initialize(prefix, file, suffix)
  @prefix = prefix
  @file = file
  @suffix = suffix
  @size = prefix.bytesize + file_size + suffix.bytesize
  rewind
end

Public Instance Methods

binmode() click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 54
def binmode
  @file.binmode if @file.respond_to?(:binmode)
  self
end
read(length = nil, outbuf = nil) click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 32
def read(length = nil, outbuf = nil)
  length = @size - @position if length.nil?
  raise ArgumentError, 'negative length' if length.negative?

  result = ::String.new(encoding: Encoding::BINARY)
  read_into(result, length)
  result = nil if result.empty? && length.positive? && @part > 2

  return result unless outbuf

  outbuf.replace(result || '')
  result && outbuf
end
rewind() click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 46
def rewind
  @file.rewind
  @part = 0
  @offset = 0
  @position = 0
  0
end

Private Instance Methods

file_size() click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 61
def file_size
  return @file.size if @file.respond_to?(:size)
  return @file.stat.size if @file.respond_to?(:stat)

  raise ArgumentError, 'Upload file size cannot be determined'
end
read_into(result, length) click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 68
def read_into(result, length)
  while result.bytesize < length && @part <= 2
    remaining = length - result.bytesize
    chunk = @part == 1 ? @file.read(remaining) : read_string_part(remaining)

    if chunk.nil? || chunk.empty?
      @part += 1
      @offset = 0
    else
      result << chunk
      @position += chunk.bytesize
    end
  end
end
read_string_part(length) click to toggle source
# File lib/fog/proxmox/compute/models/multipart_body.rb, line 83
def read_string_part(length)
  string = @part.zero? ? @prefix : @suffix
  chunk = string.byteslice(@offset, length)
  @offset += chunk.bytesize if chunk
  chunk
end