This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathApp.js
67 lines (60 loc) · 1.64 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict'
const React = require('react')
const ipfsAPI = require('ipfs-api')
class App extends React.Component {
constructor () {
super()
this.state = {
added_file_hash: null
}
this.ipfsApi = ipfsAPI('localhost', '5001')
// bind methods
this.captureFile = this.captureFile.bind(this)
this.saveToIpfs = this.saveToIpfs.bind(this)
this.arrayBufferToString = this.arrayBufferToString.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
captureFile (event) {
event.stopPropagation()
event.preventDefault()
const file = event.target.files[0]
let reader = new window.FileReader()
reader.onloadend = () => this.saveToIpfs(reader)
reader.readAsArrayBuffer(file)
}
saveToIpfs (reader) {
let ipfsId
const buffer = Buffer.from(reader.result)
this.ipfsApi.add(buffer)
.then((response) => {
console.log(response)
ipfsId = response[0].hash
console.log(ipfsId)
this.setState({added_file_hash: ipfsId})
}).catch((err) => {
console.error(err)
})
}
arrayBufferToString (arrayBuffer) {
return String.fromCharCode.apply(null, new Uint16Array(arrayBuffer))
}
handleSubmit (event) {
event.preventDefault()
}
render () {
return (
<div>
<form id='captureMedia' onSubmit={this.handleSubmit}>
<input type='file' onChange={this.captureFile} />
</form>
<div>
<a target='_blank'
href={'https://ipfs.io/ipfs/' + this.state.added_file_hash}>
{this.state.added_file_hash}
</a>
</div>
</div>
)
}
}
module.exports = App