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
+110
−102
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
Loading status checks…
modernize lib/file.js
- Loading branch information
| @@ -1,127 +1,123 @@ | ||
| module.exports = File | ||
|
|
||
| var eos = require('end-of-stream') | ||
| var EventEmitter = require('events').EventEmitter | ||
| var FileStream = require('./file-stream') | ||
| var inherits = require('inherits') | ||
| var path = require('path') | ||
| var render = require('render-media') | ||
| var stream = require('readable-stream') | ||
| var streamToBlob = require('stream-to-blob') | ||
| var streamToBlobURL = require('stream-to-blob-url') | ||
| var streamToBuffer = require('stream-with-known-length-to-buffer') | ||
|
|
||
| inherits(File, EventEmitter) | ||
|
|
||
| function File (torrent, file) { | ||
| EventEmitter.call(this) | ||
|
|
||
| this._torrent = torrent | ||
| this._destroyed = false | ||
|
|
||
| this.name = file.name | ||
| this.path = file.path | ||
| this.length = file.length | ||
| this.offset = file.offset | ||
|
|
||
| this.done = false | ||
|
|
||
| var start = file.offset | ||
| var end = start + file.length - 1 | ||
|
|
||
| this._startPiece = start / this._torrent.pieceLength | 0 | ||
| this._endPiece = end / this._torrent.pieceLength | 0 | ||
|
|
||
| if (this.length === 0) { | ||
| this.done = true | ||
| this.emit('done') | ||
| const eos = require('end-of-stream') | ||
| const {EventEmitter} = require('events') | ||
| const FileStream = require('./file-stream') | ||
| const path = require('path') | ||
| const render = require('render-media') | ||
| const stream = require('readable-stream') | ||
| const streamToBlob = require('stream-to-blob') | ||
| const streamToBlobURL = require('stream-to-blob-url') | ||
| const streamToBuffer = require('stream-with-known-length-to-buffer') | ||
|
|
||
| class File extends EventEmitter { | ||
| constructor (torrent, file) { | ||
| super() | ||
|
|
||
| this._torrent = torrent | ||
| this._destroyed = false | ||
|
|
||
| this.name = file.name | ||
| this.path = file.path | ||
| this.length = file.length | ||
| this.offset = file.offset | ||
|
|
||
| this.done = false | ||
|
|
||
| const start = file.offset | ||
| const end = start + file.length - 1 | ||
|
|
||
| this._startPiece = start / this._torrent.pieceLength | 0 | ||
| this._endPiece = end / this._torrent.pieceLength | 0 | ||
|
|
||
| if (this.length === 0) { | ||
| this.done = true | ||
| this.emit('done') | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Object.defineProperty(File.prototype, 'downloaded', { | ||
| get: function () { | ||
| get downloaded () { | ||
| if (!this._torrent.bitfield) return 0 | ||
| var downloaded = 0 | ||
| for (var index = this._startPiece; index <= this._endPiece; ++index) { | ||
| let downloaded = 0 | ||
| for (let index = this._startPiece; index <= this._endPiece; ++index) { | ||
| if (this._torrent.bitfield.get(index)) { | ||
| // verified data | ||
| downloaded += (index === this._endPiece) ? this._torrent.lastPieceLength : this._torrent.pieceLength | ||
| downloaded += index === this._endPiece ? this._torrent.lastPieceLength : this._torrent.pieceLength | ||
jimmywarting
Author
Contributor
|
||
| } else { | ||
| // "in progress" data | ||
| var piece = this._torrent.pieces[index] | ||
| downloaded += (piece.length - piece.missing) | ||
| const piece = this._torrent.pieces[index] | ||
| downloaded += piece.length - piece.missing | ||
| } | ||
| } | ||
| return downloaded | ||
| } | ||
| }) | ||
|
|
||
| Object.defineProperty(File.prototype, 'progress', { | ||
| get: function () { return this.length ? this.downloaded / this.length : 0 } | ||
| }) | ||
| get progress () { | ||
| return this.length ? this.downloaded / this.length : 0 | ||
| } | ||
|
|
||
| File.prototype.select = function (priority) { | ||
| if (this.length === 0) return | ||
| this._torrent.select(this._startPiece, this._endPiece, priority) | ||
| } | ||
| select (priority) { | ||
| if (this.length === 0) return | ||
| this._torrent.select(this._startPiece, this._endPiece, priority) | ||
| } | ||
|
|
||
| File.prototype.deselect = function () { | ||
| if (this.length === 0) return | ||
| this._torrent.deselect(this._startPiece, this._endPiece, false) | ||
| } | ||
| deselect () { | ||
| if (this.length === 0) return | ||
| this._torrent.deselect(this._startPiece, this._endPiece, false) | ||
| } | ||
|
|
||
| createReadStream (opts) { | ||
| if (this.length === 0) { | ||
| const empty = new stream.PassThrough() | ||
| process.nextTick(() => { | ||
| empty.end() | ||
| }) | ||
| return empty | ||
| } | ||
|
|
||
| File.prototype.createReadStream = function (opts) { | ||
| var self = this | ||
| if (this.length === 0) { | ||
| var empty = new stream.PassThrough() | ||
| process.nextTick(function () { | ||
| empty.end() | ||
| const fileStream = new FileStream(this, opts) | ||
| this._torrent.select(fileStream._startPiece, fileStream._endPiece, true, () => { | ||
| fileStream._notify() | ||
| }) | ||
| return empty | ||
| eos(fileStream, () => { | ||
| if (this._destroyed) return | ||
| if (!this._torrent.destroyed) { | ||
| this._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) | ||
| } | ||
| }) | ||
| return fileStream | ||
| } | ||
|
|
||
| var fileStream = new FileStream(self, opts) | ||
| self._torrent.select(fileStream._startPiece, fileStream._endPiece, true, function () { | ||
| fileStream._notify() | ||
| }) | ||
| eos(fileStream, function () { | ||
| if (self._destroyed) return | ||
| if (!self._torrent.destroyed) { | ||
| self._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) | ||
| } | ||
| }) | ||
| return fileStream | ||
| } | ||
| getBuffer (cb) { | ||
| streamToBuffer(this.createReadStream(), this.length, cb) | ||
| } | ||
|
|
||
| File.prototype.getBuffer = function (cb) { | ||
| streamToBuffer(this.createReadStream(), this.length, cb) | ||
| } | ||
| getBlob (cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| streamToBlob(this.createReadStream(), this._getMimeType(), cb) | ||
| } | ||
|
|
||
| File.prototype.getBlob = function (cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| streamToBlob(this.createReadStream(), this._getMimeType(), cb) | ||
| } | ||
| getBlobURL (cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| streamToBlobURL(this.createReadStream(), this._getMimeType(), cb) | ||
| } | ||
|
|
||
| File.prototype.getBlobURL = function (cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| streamToBlobURL(this.createReadStream(), this._getMimeType(), cb) | ||
| } | ||
| appendTo (elem, opts, cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| render.append(this, elem, opts, cb) | ||
| } | ||
|
|
||
| File.prototype.appendTo = function (elem, opts, cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| render.append(this, elem, opts, cb) | ||
| } | ||
| renderTo (elem, opts, cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| render.render(this, elem, opts, cb) | ||
| } | ||
|
|
||
| File.prototype.renderTo = function (elem, opts, cb) { | ||
| if (typeof window === 'undefined') throw new Error('browser-only method') | ||
| render.render(this, elem, opts, cb) | ||
| } | ||
| _getMimeType () { | ||
| return render.mime[path.extname(this.name).toLowerCase()] | ||
| } | ||
|
|
||
| File.prototype._getMimeType = function () { | ||
| return render.mime[path.extname(this.name).toLowerCase()] | ||
| _destroy () { | ||
| this._destroyed = true | ||
| this._torrent = null | ||
| } | ||
| } | ||
|
|
||
| File.prototype._destroy = function () { | ||
| this._destroyed = true | ||
| this._torrent = null | ||
| } | ||
| module.exports = File | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
I think these parentheses make the code more readable.