Class: Jabber::FileTransfer::FileSource

Inherits:
Object
  • Object
show all
Includes:
TransferSource
Defined in:
lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb

Overview

Simple implementation of TransferSource for sending simple files (supports ranged transfers)

Instance Method Summary collapse

Methods included from TransferSource

#md5

Constructor Details

#initialize(filename) ⇒ FileSource

Returns a new instance of FileSource.



70
71
72
73
74
75
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 70

def initialize(filename)
  @file = File.new(filename)
  @filename = filename
  @bytes_read = 0
  @length = nil
end

Instance Method Details

#can_range?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 118

def can_range?
  true
end

#dateObject



91
92
93
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 91

def date
  @file.mtime
end

#filenameObject



77
78
79
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 77

def filename
  File::basename @filename
end

#length=(l) ⇒ Object



114
115
116
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 114

def length=(l)
  @length = l
end

#mimeObject

Everything is ‘application/octet-stream’



83
84
85
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 83

def mime
  'application/octet-stream'
end

#read(length = 512) ⇒ Object

Because it can_range?, this method implements length checking



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 97

def read(length=512)
  if @length
    return nil if @bytes_read >= @length  # Already read everything requested
    if @bytes_read + length > @length # Will we read more than requested?
      length = @length - @bytes_read  # Truncate it!
    end
  end

  buf = @file.read(length)
  @bytes_read += buf.size if buf
  buf
end

#seek(position) ⇒ Object



110
111
112
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 110

def seek(position)
  @file.seek(position)
end

#sizeObject



87
88
89
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 87

def size
  File.size @filename
end