Files: 3c201fb3cd2ad22f338850c91b72818022074849 / modules_core / file-input.js
1068 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var mime = require('simple-mime')('application/octect-stream') |
4 | var split = require('split-buffer') |
5 | |
6 | module.exports = { |
7 | needs: {sbot_blobs_add: 'first'}, |
8 | gives: 'file_input', |
9 | create: function (api) { |
10 | return function FileInput(onAdded) { |
11 | return h('input', { type: 'file', |
12 | onchange: function (ev) { |
13 | var file = ev.target.files[0] |
14 | if (!file) return |
15 | var reader = new FileReader() |
16 | reader.onload = function () { |
17 | pull( |
18 | pull.values(split(new Buffer(reader.result), 64*1024)), |
19 | api.sbot_blobs_add(function (err, blob) { |
20 | if(err) return console.error(err) |
21 | onAdded({ |
22 | link: blob, |
23 | name: file.name, |
24 | size: reader.result.length || reader.result.byteLength, |
25 | type: mime(file.name) |
26 | }) |
27 | |
28 | }) |
29 | ) |
30 | } |
31 | reader.readAsArrayBuffer(file) |
32 | } |
33 | }) |
34 | } |
35 | } |
36 | } |
37 | |
38 |
Built with git-ssb-web