Class: Backblaze::B2::File

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

Instance Attribute Summary collapse

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

Returns a new instance of 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



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

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

Instance Attribute Details

#file_nameObject (readonly) Also known as: name

Returns the value of attribute file_name.



105
106
107
# File 'lib/backblaze/b2/file.rb', line 105

def file_name
  @file_name
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
97
98
99
100
101
102
# File 'lib/backblaze/b2/file.rb', line 16

def create(data:, bucket:, name: nil, base_name: '', content_type: 'b2/x-auto', info: {})
  raise ArgumentError, '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, 'You must pass a bucket'
  end

  case data
  when String
    data.force_encoding('ASCII-8BIT')
    raise ArgumentError, 'Must provide a file name for data' if name.nil?
  when ::File, Tempfile, ::Paperclip::UploadedFileAdapter
    data.binmode
    data.rewind
    if name.nil?
      raise ArgumentError, 'Must provide a file name with Tempfiles' if data.is_a? Tempfile
      name = ::File.basename(data)
    end
  else
    raise ArgumentError, 'Must provide a file name with streams' if name.nil?
    if data.respond_to?(:read)
      temp = Tempfile.new(name)
      temp.binmode
      IO.copy_stream(data, temp)
      data = temp
      data.rewind
    else
      raise ArgumentError, '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
  elsif data.is_a? ::Paperclip::UploadedFileAdapter
    digest.file(data.path)
    data.rewind
    req.body_stream = 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|
    encoded_key = URI.encode_www_form_component(key)
    req.add_field("X-Bz-Info-#{encoded_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, 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,
    content_length: data.size,
    content_type: content_type,
    content_sha1: digest,
    action: 'upload'
  }

  File.new(params)
end

Instance Method Details

#destroy!(thread_count: 4) ⇒ Object



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

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
  raise Backblaze::DestroyErrors, errors if errors.any?
end

#download_url(bucket:) ⇒ Object



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

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

#exists?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/backblaze/b2/file.rb', line 153

def exists?
  !@destroyed
end

#file_id_download_urlObject



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

def file_id_download_url
  latest.download_url
end

#latestObject



124
125
126
# File 'lib/backblaze/b2/file.rb', line 124

def latest
  @versions.first
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#versionsObject



108
109
110
111
112
113
114
# File 'lib/backblaze/b2/file.rb', line 108

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