From e0c077f328c0880cb8326de4f7718aaeae140796 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 12 Jun 2016 21:24:43 +0200 Subject: Piece Count --- meta/torrent.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'meta') diff --git a/meta/torrent.go b/meta/torrent.go index 33eebd2..358ed01 100644 --- a/meta/torrent.go +++ b/meta/torrent.go @@ -12,10 +12,6 @@ type File struct { Path []string `bencode:"path"` } -func (f File) String() string { - return fmt.Sprintf("%s (%d)", path.Join(f.Path...), f.Length) -} - type Info struct { Length int `bencode:"length"` MD5Sum []byte `bencode:"md5sum,optional"` @@ -26,16 +22,30 @@ type Info struct { Private bool `bencode:"private"` } +func (i Info) SingleFile() bool { + return len(i.Files) > 0 +} + +// returns pieceCoung and size of last piece +func (i Info) pieceCount() int { + n := i.Length / i.PieceLength + if i.Length%i.PieceLength > 0 { + n++ + } + return n +} + func (i Info) String() string { var s string if l := len(i.Files); l > 0 { s += fmt.Sprintln("files:", l) for _, f := range i.Files { - s += fmt.Sprintln(f) + s += fmt.Sprintf(" %s (%d)\n", path.Join(i.Name, path.Join(f.Path...)), f.Length) + i.Length += f.Length } } s += fmt.Sprintf("%s (%d)\n", i.Name, i.Length) - s += fmt.Sprintf("chunks: %d * %d + %d\n", + s += fmt.Sprintf("chunks: %d × %d + %d\n", i.Length/i.PieceLength, i.PieceLength, i.Length%i.PieceLength) return s } -- cgit v1.2.3