From de7161175209f2e913baec8219fd12d6739e8d97 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 10 Feb 2015 00:18:59 +1100 Subject: [PATCH] Add File.Progress --- client.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/client.go b/client.go index d043f067..9fe47f06 100644 --- a/client.go +++ b/client.go @@ -1653,12 +1653,37 @@ type File struct { path string offset int64 length int64 + metainfo.FileInfo } func (f *File) Length() int64 { return f.length } +type FilePieceState struct { + Length int64 + State byte +} + +func (f *File) Progress() (ret []FilePieceState) { + pieceSize := int64(f.t.UsualPieceSize()) + off := f.offset % pieceSize + remaining := f.length + for i := int(f.offset / pieceSize); ; i++ { + if remaining == 0 { + break + } + len1 := pieceSize - off + if len1 > remaining { + len1 = remaining + } + ret = append(ret, FilePieceState{len1, f.t.pieceStatusChar(i)}) + off = 0 + remaining -= len1 + } + return +} + func (f *File) PrioritizeRegion(off, len int64) { if off < 0 || off >= f.length { return @@ -1680,6 +1705,7 @@ func (t Torrent) Files() (ret []File) { strings.Join(fi.Path, "/"), offset, fi.Length, + fi, }) offset += fi.Length }