From 84a69f7d5d921c417d8ed9e96615d5de3d23a060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 19 Aug 2018 15:17:14 +0200 Subject: no message --- lib/file.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/file.js b/lib/file.js index 4d7d71a..23a6576 100644 --- a/lib/file.js +++ b/lib/file.js @@ -36,18 +36,30 @@ class File extends EventEmitter { get downloaded () { if (!this._torrent.bitfield) return 0 - let downloaded = 0 - for (let index = this._startPiece; index <= this._endPiece; ++index) { - if (this._torrent.bitfield.get(index)) { + + const { pieces, bitfield, pieceLength } = this._torrent + const { _startPiece:start, _endPiece:end } = this + const piece = pieces[start] + + // Calculate first piece diffrently, it sometimes have a offset + let downloaded = bitfield.get(start) + ? pieceLength - this.offset + : Math.max(piece.length - piece.missing - this.offset, 0) + + for (let index = start + 1; index <= end; ++index) { + if (bitfield.get(index)) { // verified data - downloaded += (index === this._endPiece) ? this._torrent.lastPieceLength : this._torrent.pieceLength + downloaded += pieceLength } else { // "in progress" data - const piece = this._torrent.pieces[index] + const piece = pieces[index] downloaded += piece.length - piece.missing } } - return downloaded + + // We don't have a end-offset and one small file can fith in the middle + // of one chunk, so return this.length if it's oversized + return Math.min(downloaded, this.length) } get progress () { -- cgit v1.2.3 From 3f89fe9c1ed4643d9a9f8099b43e39fb297e85ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 19 Aug 2018 15:20:24 +0200 Subject: standard style --- lib/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/file.js b/lib/file.js index 23a6576..2715fbb 100644 --- a/lib/file.js +++ b/lib/file.js @@ -38,7 +38,7 @@ class File extends EventEmitter { if (!this._torrent.bitfield) return 0 const { pieces, bitfield, pieceLength } = this._torrent - const { _startPiece:start, _endPiece:end } = this + const { _startPiece: start, _endPiece: end } = this const piece = pieces[start] // Calculate first piece diffrently, it sometimes have a offset -- cgit v1.2.3