Class: Upload

Inherits:
Transfer show all
Defined in:
lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb

Instance Attribute Summary

Attributes inherited from Transfer

#bytes, #filename, #filesize, #peer

Instance Method Summary collapse

Methods inherited from Transfer

#done?, #say, #stats, #stats_loop, #transfer

Constructor Details

#initialize(filetransfer, iq, filename, filesize, can_range, msgblock) ⇒ Upload

Returns a new instance of Upload.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 87

def initialize(filetransfer, iq, filename, filesize, can_range, msgblock)
  super(filetransfer, iq.from, filename, filesize, msgblock)

  if filename.size < 1
    say "What is this file for?"
    @done = true
    return
  end

  offset = nil
  if File::exist?(filename)
    if File::size(filename) < filesize and can_range
      @bytes = offset = File::size(filename)
      say "Will retrieve #{filename} starting at #{offset}"
    else
      say "#{filename} already exists"
      filetransfer.decline(iq)
      @done = true
      return
    end
  end

  Thread.new {
    begin
      stream = filetransfer.accept(iq, offset)
      if stream.kind_of?(Jabber::Bytestreams::SOCKS5Bytestreams)
        stream.connect_timeout = 5
        stream.add_streamhost_callback(nil, nil, &@streamhost_cb)
      end

      if stream.accept
        outfile = File.new(filename, (offset ? 'a' : 'w'))

        transfer(stream, outfile)

        outfile.close
        stream.close
        @done = true
      else
        say "Stream failed"
        @done = true
      end
    rescue
      puts $!.backtrace.first
      say "Error: #{$!}"
      @done = true
    end
  }
end