Class: Mir::Disk::MultiPartFile

Inherits:
Object
  • Object
show all
Defined in:
lib/mir/disk/amazon.rb

Overview

Used to hide the inner details of multipart file uploads and downloads. It is important that this class does not throw any exceptions as these exceptions may be swallowed further up the stack by worker threads

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(disk, name) ⇒ MultiPartFile

Returns a new instance of MultiPartFile.

Parameters:

  • the (Disk)

    remote disk

  • the (String)

    name of the resource



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mir/disk/amazon.rb', line 213

def initialize(disk, name)
  @disk, @name = disk, name
  multiname = Utils.filename_with_sequence(name, 1)
            
  if disk.key_exists?(name)
    @multipart = false
  elsif disk.key_exists?(multiname)
    @multipart = true
  else
    raise Disk::RemoteFileNotFound
  end
end

Instance Attribute Details

#diskObject (readonly)

Returns the value of attribute disk.



226
227
228
# File 'lib/mir/disk/amazon.rb', line 226

def disk
  @disk
end

#nameObject (readonly)

Returns the value of attribute name.



226
227
228
# File 'lib/mir/disk/amazon.rb', line 226

def name
  @name
end

Instance Method Details

#get(dest) ⇒ Object

Downloads the resource to the destination. If the file is stored in parts it is download sequentially in pieces



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/mir/disk/amazon.rb', line 235

def get(dest)
  output = File.new(dest, "wb")
  begin
    if multipart?
      seq = 1
      while part = Utils.filename_with_sequence(name, seq) do
        break unless disk.key_exists? part
        output.write disk.read(part)
        seq += 1
      end
    else
      output.write disk.read(name)
    end
  rescue Exception => e
    Mir.logger.error e
  ensure
    output.close
  end
end

#multipart?Boolean

Whether the resource is broken into chunks on the remote store

Returns:

  • (Boolean)


229
230
231
# File 'lib/mir/disk/amazon.rb', line 229

def multipart?
  @multipart
end