Files: 86a733778b0428dc270a65273d76c6bb187dad06 / modules / file-input.js
1052 bytesRaw
1 | var u = require('../util') |
2 | var h = require('hyperscript') |
3 | var pull = require('pull-stream') |
4 | var mime = require('mime-types') |
5 | var split = require('split-buffer') |
6 | |
7 | function first(plug) { |
8 | return function () { |
9 | var args = [].slice.call(arguments) |
10 | args.unshift(plug) |
11 | return u.firstPlug.apply(null, args) |
12 | } |
13 | } |
14 | |
15 | var add = first(exports.sbot_blobs_add = []) |
16 | |
17 | exports.file_input = function FileInput(onAdded) { |
18 | |
19 | return h('input', { type: 'file', |
20 | onchange: function (ev) { |
21 | var file = ev.target.files[0] |
22 | var reader = new FileReader() |
23 | reader.onload = function () { |
24 | pull( |
25 | pull.values(split(new Buffer(reader.result), 64*1024)), |
26 | add(function (err, blob) { |
27 | if(err) return console.error(err) |
28 | onAdded({ |
29 | link: blob, |
30 | name: file.name, |
31 | size: reader.result.length || reader.result.byteLength, |
32 | type: mime.contentType(file.name) |
33 | }) |
34 | |
35 | }) |
36 | ) |
37 | } |
38 | reader.readAsArrayBuffer(file) |
39 | } |
40 | }) |
41 | } |
42 | |
43 | |
44 | |
45 | |
46 |
Built with git-ssb-web