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.



67
68
69
70
71
72
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 67

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

Instance Method Details

#can_range?Boolean

Returns:

  • (Boolean)


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

def can_range?
  true
end

#dateObject



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

def date
  @file.mtime
end

#filenameObject



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

def filename
  File::basename @filename
end

#length=(l) ⇒ Object



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

def length=(l)
  @length = l
end

#mimeObject

Everything is ‘application/octet-stream’



80
81
82
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 80

def mime
  'application/octet-stream'
end

#read(length = 512) ⇒ Object

Because it can_range?, this method implements length checking



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 94

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



107
108
109
# File 'lib/xmpp4r/bytestreams/helper/filetransfer.rb', line 107

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

#sizeObject



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

def size
  File.size @filename
end