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