github.com/uber/kraken@v0.1.4/lib/torrent/storage/torrent_info.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package storage 15 16 import ( 17 "github.com/uber/kraken/core" 18 19 "github.com/willf/bitset" 20 ) 21 22 // TorrentInfo encapsulates read-only torrent information. 23 type TorrentInfo struct { 24 metainfo *core.MetaInfo 25 bitfield *bitset.BitSet 26 percentDownloaded int 27 } 28 29 // NewTorrentInfo creates a new TorrentInfo. 30 func NewTorrentInfo(mi *core.MetaInfo, bitfield *bitset.BitSet) *TorrentInfo { 31 numComplete := bitfield.Count() 32 downloaded := int(float64(numComplete) / float64(mi.NumPieces()) * 100) 33 return &TorrentInfo{mi, bitfield, downloaded} 34 } 35 36 func (i *TorrentInfo) String() string { 37 return i.InfoHash().Hex() 38 } 39 40 // Digest returns the torrent's blob digest. 41 func (i *TorrentInfo) Digest() core.Digest { 42 return i.metainfo.Digest() 43 } 44 45 // InfoHash returns the hash torrent metainfo. 46 func (i *TorrentInfo) InfoHash() core.InfoHash { 47 return i.metainfo.InfoHash() 48 } 49 50 // MaxPieceLength returns the max piece length of the torrent. 51 func (i *TorrentInfo) MaxPieceLength() int64 { 52 return i.metainfo.PieceLength() 53 } 54 55 // PercentDownloaded returns the percent of bytes downloaded as an integer 56 // between 0 and 100. Useful for logging. 57 func (i *TorrentInfo) PercentDownloaded() int { 58 return i.percentDownloaded 59 } 60 61 // Bitfield returns the piece status bitfield of the torrent. Note, this is a 62 // snapshot and may be stale information. 63 func (i *TorrentInfo) Bitfield() *bitset.BitSet { 64 return i.bitfield 65 }