class B2::File

Attributes

account_id[RW]
bucket_id[RW]
content_length[RW]
content_sha1[RW]
content_type[RW]
file_id[RW]
file_info[RW]
filename[RW]

Public Instance Methods

create(upload_url, file, authorization_token, folder: nil, content_type: 'b2/x-auto', info: {}) click to toggle source
# File lib/b2/file.rb, line 5
def create(upload_url, file, authorization_token, folder: nil, content_type: 'b2/x-auto', info: {})
  if file.respond_to?(:read)
    content_type = file.content_type
    size = file.size.to_s
    filename = folder.nil? ? ERB::Util.url_encode(file.original_filename) : "#{folder}/#{ERB::Util.url_encode(file.original_filename)}"
    digest = Digest::SHA1.file(file.path).hexdigest
    body = file.read
  elsif file.is_a?(String)
    size = ::File.size(file).to_s
    filename = folder.nil? ? ERB::Util.url_encode(::File.basename(file)) : "#{folder}/#{ERB::Util.url_encode(::File.basename(file))}"
    digest = Digest::SHA1.file(file).hexdigest
    body = ::File.read(file)
  else
    raise ArgumentError.new('Unsuitable data type. Please read the docs.')
  end

  additional_headers = {}
  info.first(10).each do |key, value|
    additional_headers["X-Bz-Info-#{key}"] = value
  end

  headers = {
    "Authorization" => authorization_token,
    "Content-Type" => content_type,
    "X-Bz-File-Name" => filename,
    "Content-Length" => size,
    "X-Bz-Content-Sha1" => digest
  }

  headers.merge!(additional_headers)

  response = HTTParty.post(upload_url, body: body, headers: headers)
  assign_return_value(response)
end
delete_file_version(filename, file_id) click to toggle source
# File lib/b2/file.rb, line 40
def delete_file_version(filename, file_id)
  body = {
    fileName: filename,
    fileId: file_id
  }
  response = post('/b2_delete_file_version', body: body.to_json)
  assign_return_value(response)
end
download_file_by_id(file_id) click to toggle source
# File lib/b2/file.rb, line 53
def download_file_by_id(file_id)
  get("#{self.download_url}/b2api/v1/b2_download_file_by_id?fileId=#{file_id}")
end
download_file_by_name(bucket_name, filename) click to toggle source
# File lib/b2/file.rb, line 57
def download_file_by_name(bucket_name, filename)
  get("#{self.download_url}/file/#{bucket_name}/#{filename}")
end
get_download_url(bucket_name, filename) click to toggle source
# File lib/b2/file.rb, line 49
def get_download_url(bucket_name, filename)
  "#{self.download_url}/file/#{bucket_name}/#{filename}"
end

Private Instance Methods

assign_return_value(response) click to toggle source
# File lib/b2/file.rb, line 62
def assign_return_value(response)
  if response.code.eql?(200)
    self.bucket_id = response["bucketId"]
    self.account_id = response["accountId"]
    self.content_length = response["contentLength"]
    self.content_sha1 = response["contentSha1"]
    self.content_type = response["contentType"]
    self.file_id = response["fileId"]
    self.file_info = response["fileInfo"]
    self.filename = response["fileName"]
  end
  response
end