From 71a79058144b3d3ea4db4923c079645eeeedf8c6 Mon Sep 17 00:00:00 2001 From: Mikeal Rogers Date: Wed, 9 Nov 2016 23:13:56 -0800 Subject: [PATCH] Adding property for downloaded bytes per file. --- lib/file.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/file.js b/lib/file.js index 65c9187f..d1e72bbe 100644 --- a/lib/file.js +++ b/lib/file.js @@ -38,6 +38,24 @@ function File (torrent, file) { } } +Object.defineProperty(File.prototype, 'downloaded', { + get: function () { + if (!this._torrent.bitfield) return 0 + var downloaded = 0 + for (var index = this._startPiece; index <= this._endPiece; ++index) { + if (this._torrent.bitfield.get(index)) { + // verified data + downloaded += this._torrent.pieceLength + } else { + // "in progress" data + var piece = this._torrent.pieces[index] + downloaded += (piece.length - piece.missing) + } + } + return downloaded + } +}) + File.prototype.select = function (priority) { if (this.length === 0) return this._torrent.select(this._startPiece, this._endPiece, priority)