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



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/mir/disk/amazon.rb', line 238

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.



251
252
253
# File 'lib/mir/disk/amazon.rb', line 251

def disk
  @disk
end

#nameObject (readonly)

Returns the value of attribute name.



251
252
253
# File 'lib/mir/disk/amazon.rb', line 251

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/mir/disk/amazon.rb', line 260

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)


254
255
256
# File 'lib/mir/disk/amazon.rb', line 254

def multipart?
  @multipart
end