From 81095d895291f522957deaabbe8bd2bfc978f073 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 15 Jun 2016 08:55:34 -0700 Subject: 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. --- lib/file.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'lib/file.js') 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 -- cgit v1.2.3