Class: Backblaze::B2::File

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get, #head, #post, #put

Methods included from Utils

#camelize, included, #underscore

Constructor Details

#initialize(file_name:, bucket_id:, versions: nil, **file_version_args) ⇒ File


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/backblaze/b2/file.rb', line 3

def initialize(file_name:, bucket_id:, versions: nil, **file_version_args)
  @file_name = file_name
  @bucket_id = bucket_id
  if versions
    @fetched_all = true
    @versions = versions
  else
    @fetched_all = false
    @versions = [FileVersion.new(file_version_args.merge(file_name: file_name))]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object


155
156
157
158
159
160
161
# File 'lib/backblaze/b2/file.rb', line 155

def method_missing(m, *args, &block)
  if latest.respond_to?(m)
    latest.send(m, *args, &block)
  else
    super
  end
end

Class Method Details

.create(data:, bucket:, name: nil, base_name: '', content_type: 'b2/x-auto', info: {}) ⇒ Object

Raises:

  • (ArgumentError)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/backblaze/b2/file.rb', line 16

def create(data:, bucket:, name: nil, base_name: '', content_type: 'b2/x-auto', info: {})
  raise ArgumentError.new('data must not be nil') if data.nil?

  case bucket
  when String
    upload_url = Bucket.upload_url(bucket_id: bucket)
  when Bucket
    upload_url = bucket.upload_url
  else
    raise ArgumentError.new('You must pass a bucket')
  end

  close_file = false
  case data
  when String
    data.force_encoding('ASCII-8BIT')
    raise ArgumentError.new('Must provide a file name for data') if name.nil?
  when ::File, Tempfile
    data.binmode
    data.rewind
    if name.nil?
      raise ArgumentError.new('Must provide a file name with Tempfiles') if data.is_a? Tempfile
      name = ::File.basename(data)
    end
  else
    raise ArgumentError.new('Must provide a file name with streams') if name.nil?
    if data.respond_to?(:read)
      close_file = true
      temp = Tempfile.new(name)
      temp.binmode
      IO.copy_stream(data, temp)
      data = temp
      data.rewind
    else
      raise ArgumentError.new('Unsuitable data type. Please read the docs.')
    end
  end

  uri = URI(upload_url[:url])
  req = Net::HTTP::Post.new(uri)

  req.add_field("Authorization", upload_url[:token])
  req.add_field("X-Bz-File-Name", "#{base_name}/#{name}".tr_s('/', '/').sub(/\A\//, ''))
  req.add_field("Content-Type", content_type)
  req.add_field("Content-Length", data.size)

  digest = Digest::SHA1.new
  if data.is_a? String
    digest.update(data)
    req.body = data
  else
    digest.file(data)
    data.rewind
    req.body_stream = data
  end

  req.add_field("X-Bz-Content-Sha1", digest)

  info.first(10).map do |key, value|
    req.add_field("X-Bz-Info-#{URI.encode(key)}", value)
  end

  http = Net::HTTP.new(req.uri.host, req.uri.port)
  http.use_ssl = (req.uri.scheme == 'https')
  res = http.start {|make| make.request(req)}

  response = JSON.parse(res.body)

  raise Backblaze::FileError.new(response) unless res.code.to_i == 200

  params = {
    file_name: response['fileName'],
    bucket_id: response['bucketId'],
    size: response['contentLength'],
    file_id: response['fileId'],
    upload_timestamp: Time.now.to_i * 1000,
    action: 'upload'
  }

  File.new(params)
end

Instance Method Details

#destroy!(thread_count: 4) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/backblaze/b2/file.rb', line 124

def destroy!(thread_count: 4)
  versions
  thread_count = @versions.length if thread_count > @versions.length || thread_count < 1
  lock = Mutex.new
  errors = []
  threads = []
  thread_count.times do
    threads << Thread.new do
      version = nil
      loop do
        lock.synchronize { version = @versions.pop }
        break if version.nil?
        begin
          version.destroy!
        rescue Backblaze::FileError => e
          lock.synchronize { errors << e }
        end
      end
    end
  end
  threads.map(&:join)
  @destroyed = true
  if errors.any?
    raise Backblaze::DestroyErrors.new(errors)
  end
end

#download_url(bucket:) ⇒ Object


112
113
114
# File 'lib/backblaze/b2/file.rb', line 112

def download_url(bucket:)
  "#{Backblaze::B2.download_url}/file/#{bucket.is_a?(Bucket) ? bucket.name : bucket}/#{file_name}"
end

#exists?Boolean


151
152
153
# File 'lib/backblaze/b2/file.rb', line 151

def exists?
  !@destroyed
end

#file_id_download_urlObject


116
117
118
# File 'lib/backblaze/b2/file.rb', line 116

def file_id_download_url
  latest.download_url
end

#file_nameObject Also known as: name


99
100
101
# File 'lib/backblaze/b2/file.rb', line 99

def file_name
  @file_name
end

#latestObject


120
121
122
# File 'lib/backblaze/b2/file.rb', line 120

def latest
  @versions.first
end

#respond_to?(m) ⇒ Boolean


163
164
165
166
167
168
169
# File 'lib/backblaze/b2/file.rb', line 163

def respond_to?(m)
  if latest.respond_to?(m)
    true
  else
    super
  end
end

#versionsObject


104
105
106
107
108
109
110
# File 'lib/backblaze/b2/file.rb', line 104

def versions
  unless @fetched_all
    @versions = file_versions(bucket_id: @bucket_id, convert: true, limit: -1, double_check_server: false, file_name: file_name)
    @fetched_all = true
  end
  @versions
end