Class: QuartzTorrent::Metainfo

Inherits:
Object
  • Object
show all
Defined in:
lib/quartz_torrent/metainfo.rb

Overview

Torrent metainfo structure. This is what’s usually found in .torrent files. This class generally follows the structure of the metadata format.

Defined Under Namespace

Classes: FileInfo, Info

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetainfo

Returns a new instance of Metainfo.



151
152
153
154
155
156
157
158
159
# File 'lib/quartz_torrent/metainfo.rb', line 151

def initialize
  @info = nil
  @announce = nil
  @announceList = nil
  @creationDate = nil
  @comment = nil
  @createdBy = nil
  @encoding = nil
end

Instance Attribute Details

#announceObject

Announce URL of the tracker



169
170
171
# File 'lib/quartz_torrent/metainfo.rb', line 169

def announce
  @announce
end

#announceListObject

Returns the value of attribute announceList.



170
171
172
# File 'lib/quartz_torrent/metainfo.rb', line 170

def announceList
  @announceList
end

#commentObject

Comment



175
176
177
# File 'lib/quartz_torrent/metainfo.rb', line 175

def comment
  @comment
end

#createdByObject

Created By



177
178
179
# File 'lib/quartz_torrent/metainfo.rb', line 177

def createdBy
  @createdBy
end

#creationDateObject

Creation date as a ruby Time object



173
174
175
# File 'lib/quartz_torrent/metainfo.rb', line 173

def creationDate
  @creationDate
end

#encodingObject

The string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile



179
180
181
# File 'lib/quartz_torrent/metainfo.rb', line 179

def encoding
  @encoding
end

#infoObject

A Metainfo::Info object



162
163
164
# File 'lib/quartz_torrent/metainfo.rb', line 162

def info
  @info
end

#infoHashObject

A 20-byte SHA1 hash of the value of the info key from the metainfo. This is neede when connecting to the tracker or to a peer.



166
167
168
# File 'lib/quartz_torrent/metainfo.rb', line 166

def infoHash
  @infoHash
end

Class Method Details

.createFromFile(path) ⇒ Object

Create a Metainfo object from the named file.



218
219
220
221
222
223
224
# File 'lib/quartz_torrent/metainfo.rb', line 218

def self.createFromFile(path)
  result = 
  File.open(path,"rb") do |io|
    result = self.createFromIO(io)
  end
  result
end

.createFromIO(io) ⇒ Object

Create a Metainfo object from the passed IO.



213
214
215
# File 'lib/quartz_torrent/metainfo.rb', line 213

def self.createFromIO(io)
  self.createFromString(io.read)
end

.createFromString(data) ⇒ Object

Create a Metainfo object from the passed bencoded string.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/quartz_torrent/metainfo.rb', line 182

def self.createFromString(data)
  logger = LogManager.getLogger("metainfo")

  decoded = BEncode.load(data, {:ignore_trailing_junk => 1})
  logger.debug "Decoded torrent metainfo: #{decoded.inspect}"
  result = Metainfo.new
  result.createdBy = decoded['created by']
  result.comment = decoded['comment']
  result.creationDate = decoded['creation date']
  if result.creationDate 
    if !result.creationDate.is_a?(Integer)
      if result.creationDate =~ /^\d+$/
        result.creationDate = result.creationDate.to_i
      else
        logger.warn "Torrent metainfo contained invalid date: '#{result.creationDate.class}'"
        result.creationDate = nil
      end
    end

    result.creationDate = Time.at(result.creationDate) if result.creationDate
  end
  result.encoding = decoded['encoding']
  result.announce = decoded['announce'].strip
  result.announceList = decoded['announce-list']
  result.info = Info.createFromBdecode(decoded['info'])
  result.infoHash = Digest::SHA1.digest( decoded['info'].bencode )

  result
end

.valueOrException(v, msg) ⇒ Object

If ‘v’ is null, throw an exception. Otherwise return ‘v’.



12
13
14
15
16
17
18
# File 'lib/quartz_torrent/metainfo.rb', line 12

def self.valueOrException(v, msg)
  if ! v
    LogManager.getLogger("metainfo").error msg
    raise "Invalid torrent metainfo"
  end
  v
end