Files: 2c6e20555336c47eae846c5db7b268cf2a85c159 / modules_core / file-input.js
1162 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 | var plugs = require('../plugs') |
8 | |
9 | var add = plugs.first(exports.sbot_blobs_add = []) |
10 | |
11 | module.exports = { |
12 | needs: {sbot_blobs_add: 'first'}, |
13 | gives: 'file_input', |
14 | create: function () { |
15 | return function FileInput(onAdded) { |
16 | return h('input', { type: 'file', |
17 | onchange: function (ev) { |
18 | var file = ev.target.files[0] |
19 | if (!file) return |
20 | var reader = new FileReader() |
21 | reader.onload = function () { |
22 | pull( |
23 | pull.values(split(new Buffer(reader.result), 64*1024)), |
24 | add(function (err, blob) { |
25 | if(err) return console.error(err) |
26 | onAdded({ |
27 | link: blob, |
28 | name: file.name, |
29 | size: reader.result.length || reader.result.byteLength, |
30 | type: mime(file.name) |
31 | }) |
32 | |
33 | }) |
34 | ) |
35 | } |
36 | reader.readAsArrayBuffer(file) |
37 | } |
38 | }) |
39 | } |
40 | } |
41 | } |
42 | |
43 |
Built with git-ssb-web