Files: a168beb1551b3abbd4db5b2ccc4bd53583876661 / modules_core / file-input.js
1095 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 | } |
32 | reader.readAsArrayBuffer(file) |
33 | } |
34 | }) |
35 | } |
36 | } |
37 | } |
38 | |
39 |
Built with git-ssb-web