Class: QuartzTorrent::Metainfo::Info
- Inherits:
-
Object
- Object
- QuartzTorrent::Metainfo::Info
- Defined in:
- lib/quartz_torrent/metainfo.rb
Overview
The ‘info’ property of the torrent metainfo.
Instance Attribute Summary collapse
-
#files ⇒ Object
Array of FileInfo objects.
-
#name ⇒ Object
Suggested file or directory name.
-
#pieceLen ⇒ Object
Length of each piece in bytes.
-
#pieces ⇒ Object
Array of SHA1 digests of all peices.
-
#private ⇒ Object
True if no external peer source is allowed.
-
#source ⇒ Object
Optional source.
Class Method Summary collapse
-
.createFromBdecode(infoDict) ⇒ Object
Create a FileInfo object from a bdecoded structure.
Instance Method Summary collapse
-
#bencode ⇒ Object
BEncode this info and return the result.
-
#dataLength ⇒ Object
Total length of the torrent data in bytes.
-
#initialize ⇒ Info
constructor
A new instance of Info.
Constructor Details
#initialize ⇒ Info
Returns a new instance of Info.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/quartz_torrent/metainfo.rb', line 51 def initialize @pieceLen = nil @pieces = nil @private = nil # Suggested file or directory name @name = nil # List of file info for files in the torrent. These include the directory name if this is a # multi-file download. For a single-file download the @files = [] @logger = LogManager.getLogger("metainfo") @source = nil end |
Instance Attribute Details
#files ⇒ Object
Array of FileInfo objects
65 66 67 |
# File 'lib/quartz_torrent/metainfo.rb', line 65 def files @files end |
#name ⇒ Object
Suggested file or directory name
67 68 69 |
# File 'lib/quartz_torrent/metainfo.rb', line 67 def name @name end |
#pieceLen ⇒ Object
Length of each piece in bytes. The last piece may be shorter than this.
69 70 71 |
# File 'lib/quartz_torrent/metainfo.rb', line 69 def pieceLen @pieceLen end |
#pieces ⇒ Object
Array of SHA1 digests of all peices. These digests are in binary format.
71 72 73 |
# File 'lib/quartz_torrent/metainfo.rb', line 71 def pieces @pieces end |
#private ⇒ Object
True if no external peer source is allowed.
73 74 75 |
# File 'lib/quartz_torrent/metainfo.rb', line 73 def private @private end |
#source ⇒ Object
Optional source
75 76 77 |
# File 'lib/quartz_torrent/metainfo.rb', line 75 def source @source end |
Class Method Details
.createFromBdecode(infoDict) ⇒ Object
Create a FileInfo object from a bdecoded structure.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/quartz_torrent/metainfo.rb', line 83 def self.createFromBdecode(infoDict) result = Info.new result.pieceLen = infoDict['piece length'] result.private = infoDict['private'] result.source = infoDict['source'] result.pieces = parsePieces(Metainfo.valueOrException(infoDict['pieces'], "Torrent metainfo is missing the pieces property.")) result.name = Metainfo.valueOrException(infoDict['name'], "Torrent metainfo is missing the name property.") if infoDict.has_key? 'files' # This is a multi-file torrent infoDict['files'].each do |file| result.files.push FileInfo.createFromBdecode(file) result.files.last.path = result.name + File::SEPARATOR + result.files.last.path end else # This is a single-file torrent length = Metainfo.valueOrException(infoDict['length'], "Torrent metainfo listed a single file, but it is missing the length property.") result.files.push FileInfo.new(length, result.name) end result end |
Instance Method Details
#bencode ⇒ Object
BEncode this info and return the result.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/quartz_torrent/metainfo.rb', line 107 def bencode hash = {} raise "Cannot encode Info object with nil pieceLen" if ! @pieceLen raise "Cannot encode Info object with nil name" if ! @name raise "Cannot encode Info object with nil pieces" if ! @pieces raise "Cannot encode Info object with nil files or empty files" if ! @files || @files.empty? hash['piece length'] = @pieceLen hash['private'] = @private if @private hash['source'] = @source if @source hash['name'] = @name hash['pieces'] = @pieces.join if @files.length > 1 # This is a multi-file torrent # When we loaded the torrent, we prepended the 'name' element of the info hash to the path. We need to remove this # name element to end up with the same result. hash['files'] = @files.collect{ |file| {'length' => file.length, 'path' => file.path.split(File::SEPARATOR).drop(1) } } else hash['length'] = @files.first.length end hash.bencode end |
#dataLength ⇒ Object
Total length of the torrent data in bytes.
78 79 80 |
# File 'lib/quartz_torrent/metainfo.rb', line 78 def dataLength files.reduce(0){ |memo,f| memo + f.length} end |