Class: Jabber::FileTransfer::FileSource

Inherits:
Object
  • Object
show all
Includes:
TransferSource
Defined in:
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


71
72
73
74
75
76
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 71

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

Instance Method Details

#can_range?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 119

def can_range?
  true
end

#dateObject


92
93
94
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 92

def date
  @file.mtime
end

#filenameObject


78
79
80
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 78

def filename
  File::basename @filename
end

#length=(l) ⇒ Object


115
116
117
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 115

def length=(l)
  @length = l
end

#mimeObject

Everything is 'application/octet-stream'


84
85
86
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 84

def mime
  'application/octet-stream'
end

#read(length = 512) ⇒ Object

Because it can_range?, this method implements length checking


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

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


111
112
113
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 111

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

#sizeObject


88
89
90
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 88

def size
  File.size @filename
end