Class: Reactor::Tools::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/tools/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cm_obj) ⇒ Uploader

Returns a new instance of Uploader.



8
9
10
# File 'lib/reactor/tools/uploader.rb', line 8

def initialize(cm_obj)
  self.cm_obj = cm_obj
end

Instance Attribute Details

#cm_objObject

Returns the value of attribute cm_obj.



6
7
8
# File 'lib/reactor/tools/uploader.rb', line 6

def cm_obj
  @cm_obj
end

Instance Method Details

#upload(data_or_io, extension) ⇒ Object

Uses streaming interface to upload data from given IO stream or memory location. Extension is used as basis for content detection. Larger file transfers should be executed through IO streams, which conserve memory.

After the data has been successfuly transfered to streaming interface it stores contentType and resulting ticket into Reactor::Cm::Obj provided on initialization.

NOTE: there is a known bug for Mac OS X: if you are uploading more files (IO objects) in sequence, the upload may fail randomly. For this platform and this case fallback to memory streaming is used.



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
# File 'lib/reactor/tools/uploader.rb', line 26

def upload(data_or_io, extension)
  if (data_or_io.kind_of?IO)
    io = data_or_io
    begin
      ticket_id = stream_io(io, extension)
    rescue Errno::EINVAL => e
      if RUBY_PLATFORM.downcase.include?("darwin")
        # mac os x is such a piece of shit
        # writing to a socket can fail with EINVAL, randomly without
        # visible reason when using body_stream
        # in this case fallback to memory upload which always works (?!?!)
        Reactor::Cm::LOGGER.log "MacOS X bug detected for #{io.inspect}"
        io.rewind
        return upload(io.read, extension)
      else
        raise e
      end
    end
  else
    ticket_id = stream_data(data_or_io, extension)
  end

  cm_obj.set(:contentType, extension)
  cm_obj.set(:blob, {ticket_id=>{:encoding=>'stream'}})

  ticket_id
end