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 | |
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.
-rw-r--r-- | docs/api.md | 10 | ||||
-rw-r--r-- | lib/file.js | 13 | ||||
-rw-r--r-- | package.json | 1 |
3 files changed, 22 insertions, 2 deletions
diff --git a/docs/api.md b/docs/api.md index 4750f02..eca7144 100644 --- a/docs/api.md +++ b/docs/api.md @@ -517,6 +517,14 @@ file.appendTo('#containerElement', function (err, elem) { Like `file.appendTo` but renders directly into given element (or CSS selector). +## `file.getBlob(function callback (err, blob) {})` *(browser only)* + +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. + ## `file.getBlobURL(function callback (err, url) {})` *(browser only)* Get a url which can be used in the browser to refer to the file. @@ -525,6 +533,8 @@ The file will be fetched from the network with highest priority, and `callback` called once the file is ready. `callback` must be specified, and will be called with a an `Error` (or `null`) and the Blob URL (`String`). +This method is useful for creating a file download link, like this: + ```js file.getBlobURL(function (err, url) { if (err) throw err 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 diff --git a/package.json b/package.json index 625956f..5e57d12 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "simple-peer": "^6.0.0", "simple-sha1": "^2.0.0", "speedometer": "^1.0.0", + "stream-to-blob": "^1.0.0", "stream-to-blob-url": "^2.0.0", "stream-with-known-length-to-buffer": "^1.0.0", "torrent-discovery": "^8.0.0", |