diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 05:45:16 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 05:45:16 +0300 |
commit | 8fd9cb092ae5899767c1ae7bab2be872c9543e35 (patch) | |
tree | 6f0cb39e4a1ee9971bca5430a039f32a13755787 /lib | |
parent | 1aee189a736c0d1c9d8341ee3a4ec599c6cc1af4 (diff) |
Add file.getBuffer() API
Diffstat (limited to 'lib')
-rw-r--r-- | lib/storage.js | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/storage.js b/lib/storage.js index 5454a38..d652c66 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -254,20 +254,32 @@ File.prototype.createReadStream = function (opts) { } /** - * TODO: detect errors and call callback with error * @param {function} cb */ File.prototype.getBlobURL = function (cb) { var self = this - var chunks = [] + self.getBuffer(function (err, buf) { + if (err) return cb(err) + var url = URL.createObjectURL(new Blob([ buf ])) + cb(null, url) + }) +} + +/** + * TODO: detect errors and call callback with error + * @param {function} cb + */ +File.prototype.getBuffer = function (cb) { + var self = this + var buf = new Buffer(self.length) + var start = 0 self.createReadStream() .on('data', function (chunk) { - chunks.push(chunk) + chunk.copy(buf, start) + start += chunk.length }) .on('end', function () { - var buf = Buffer.concat(chunks) - var url = URL.createObjectURL(new Blob([ buf ])) - cb(null, url) + cb(null, buf) }) } |