Class: B2::File

Inherits:
Base
  • Object
show all
Defined in:
lib/b2/file.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#download_url

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from B2::Base

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



3
4
5
# File 'lib/b2/file.rb', line 3

def 
  @account_id
end

#bucket_idObject

Returns the value of attribute bucket_id.



3
4
5
# File 'lib/b2/file.rb', line 3

def bucket_id
  @bucket_id
end

#content_lengthObject

Returns the value of attribute content_length.



3
4
5
# File 'lib/b2/file.rb', line 3

def content_length
  @content_length
end

#content_sha1Object

Returns the value of attribute content_sha1.



3
4
5
# File 'lib/b2/file.rb', line 3

def content_sha1
  @content_sha1
end

#content_typeObject

Returns the value of attribute content_type.



3
4
5
# File 'lib/b2/file.rb', line 3

def content_type
  @content_type
end

#file_idObject

Returns the value of attribute file_id.



3
4
5
# File 'lib/b2/file.rb', line 3

def file_id
  @file_id
end

#file_infoObject

Returns the value of attribute file_info.



3
4
5
# File 'lib/b2/file.rb', line 3

def file_info
  @file_info
end

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/b2/file.rb', line 3

def filename
  @filename
end

Instance Method Details

#create(upload_url, file, authorization_token, folder: nil, content_type: 'b2/x-auto', info: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
47
# 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) ⇒ Object



53
54
55
# 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) ⇒ Object



57
58
59
# 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) ⇒ Object



49
50
51
# File 'lib/b2/file.rb', line 49

def get_download_url(bucket_name, filename)
  "#{self.download_url}/file/#{bucket_name}/#{filename}"
end