class ChinoRuby::Blobs

Public Instance Methods

commit_upload(upload_id) click to toggle source
# File lib/chino_ruby/classes.rb, line 1546
def commit_upload(upload_id)
  check_string(upload_id)
  data = { upload_id: upload_id }.to_json
  blob = Blob.new
  blob.from_json(post_resource("/blobs/commit", data).to_json, true)
  blob
end
delete_blob(blob_id, force) click to toggle source
# File lib/chino_ruby/classes.rb, line 1585
def delete_blob(blob_id, force)
  check_string(blob_id)
  check_boolean(force)
  delete_resource("/blobs/#{blob_id}", force)
end
get(blob_id, destination) click to toggle source
# File lib/chino_ruby/classes.rb, line 1554
def get(blob_id, destination)
  check_string(blob_id)
  check_string(destination)
  uri = return_uri("/blobs/#{blob_id}")
  req = Net::HTTP::Get.new(uri.path)
  if @customer_id == "Bearer "
    req.add_field("Authorization", @customer_id+@customer_key)
  else
    req.basic_auth @customer_id, @customer_key
  end
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
    http.request(req)
  }
  blob = GetBlobResponse.new
  blob.blob_id = blob_id
  filename = res.header['Content-Disposition'].partition('=').last
  blob.filename = filename
  blob.path = destination
  # FIXME: this is relative to the LIBRARY directory, not running app
  # file_path = File.join File.expand_path("../..", File.dirname(__FILE__)), destination
  file_path = File.join Dir.pwd, destination
  FileUtils.mkdir_p(file_path) unless File.exist?(file_path)
  File.open(File.join(file_path+filename), 'wb') { |file|
    file << res.body
    blob.md5 = (Digest::MD5.file file).hexdigest
    blob.sha1 = (Digest::SHA1.file file).hexdigest
    blob.size = file.size
  }
  blob
end
init_upload(filename, document_id, field) click to toggle source
# File lib/chino_ruby/classes.rb, line 1516
def init_upload(filename, document_id, field)
  check_string(filename)
  check_string(document_id)
  check_string(field)
  data = { file_name: filename, document_id: document_id, field: field }.to_json
  blob = InitBlobResponse.new
  blob.from_json(ActiveSupport::JSON.decode(post_resource("/blobs", data).to_json)['blob'].to_json)
  blob
end
upload_blob(path, filename, document_id, field, chunk_size = 1024*32) click to toggle source
# File lib/chino_ruby/classes.rb, line 1495
def upload_blob(path, filename, document_id, field, chunk_size = 1024*32)
  check_string(path)
  check_string(document_id)
  check_string(field)
  check_string(filename)
  blob = InitBlobResponse.new
  blob = init_upload(filename, document_id, field)
  bytes = []
  offset = 0
  #FIXME: this is relative to the LIBRARY directory, not running app
  # file_path = File.join File.expand_path("../..", File.dirname(__FILE__)), path, filename
  file_path = File.join path, filename
  File.open(file_path, 'rb') { |file|
    while (buffer = file.read(chunk_size)) do
      upload_chunk(blob.upload_id, buffer, offset)
      offset = offset+buffer.length
    end
    commit_upload(blob.upload_id)
  }
end
upload_chunk(upload_id, bytes, offset) click to toggle source
# File lib/chino_ruby/classes.rb, line 1526
def upload_chunk(upload_id, bytes, offset)
  uri = return_uri("/blobs/#{upload_id}")
  req = Net::HTTP::Put.new(uri)
  req.body = bytes
  req.add_field("length", bytes.length)
  req.add_field("offset", offset)
  req.add_field("Content-Type", "application/octet-stream")
  if @customer_id == "Bearer "
    req.add_field("Authorization", @customer_id+@customer_key)
  else
    req.basic_auth @customer_id, @customer_key
  end
  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
    http.request(req)
  }
  blob = InitBlobResponse.new
  blob.from_json(parse_response(res)['data'].to_json, true)
  blob
end