Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modernize lib/file.js and calculate downloaded correctly #1476

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

no message

  • Loading branch information
jimmywarting committed Aug 19, 2018
commit 84a69f7d5d921c417d8ed9e96615d5de3d23a060
@@ -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 () {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.