diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-06-15 18:55:34 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-06-15 18:55:34 +0300 |
commit | 81095d895291f522957deaabbe8bd2bfc978f073 (patch) | |
tree | 12005a8a086aa1bcc6490d79f3736618c5bd02a5 /lib | |
parent | 612c31a3faf068565104d5d370f6c07e94942989 (diff) |
API: Add file.getBlob() method
Get a W3C `Blob` object which contains the file data.
The file will be fetched from the network with highest priority, and
`callback` will be
called once the file is ready. `callback` must be specified, and will
be called with a an
`Error` (or `null`) and the `Blob` object.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/file.js | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/file.js b/lib/file.js index ff19a1c..5f8021c 100644 --- a/lib/file.js +++ b/lib/file.js @@ -7,6 +7,7 @@ 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') @@ -74,10 +75,14 @@ File.prototype.getBuffer = function (cb) { streamToBuffer(this.createReadStream(), this.length, cb) } +File.prototype.getBlob = function (cb) { + if (typeof window === 'undefined') throw new Error('browser-only method') + streamToBlob(this.createReadStream(), this._getMimeType(), cb) +} + File.prototype.getBlobURL = function (cb) { if (typeof window === 'undefined') throw new Error('browser-only method') - var mime = render.mime[path.extname(this.name).toLowerCase()] - streamToBlobURL(this.createReadStream(), mime, cb) + streamToBlobURL(this.createReadStream(), this._getMimeType(), cb) } File.prototype.appendTo = function (elem, cb) { @@ -90,6 +95,10 @@ File.prototype.renderTo = function (elem, cb) { render.render(this, elem, cb) } +File.prototype._getMimeType = function () { + return render.mime[path.extname(this.name).toLowerCase()] +} + File.prototype._destroy = function () { this._destroyed = true this._torrent = null |