Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 62b844f

Browse files
lideldaviddias
authored andcommitted
feat: upload example works with big files
Old example created a Buffer from the file handle which quickly hit allocation limits with big files. This change replaces Buffer with a stream, which enables example to work with big files (tested with 3.6GB video, took ~30seconds with go-ipfs on localhost). While I was at it, I added a demo of a pattern for persisting filenames by wrapping them in unixfs directory. License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 0175406 commit 62b844f

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

examples/upload-file-via-browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"babel-core": "^5.4.7",
1515
"babel-loader": "^5.1.2",
1616
"ipfs-api": "../../",
17+
"pull-file-reader": "^1.0.2",
1718
"react": "^15.4.2",
1819
"react-dom": "^15.4.2",
1920
"react-hot-loader": "^1.3.1",

examples/upload-file-via-browser/src/App.js

+40-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
const React = require('react')
33
const ipfsAPI = require('ipfs-api')
44

5+
// create a stream from a file, which enables uploads of big files without allocating memory twice
6+
const fileReaderPullStream = require('pull-file-reader')
7+
58
class App extends React.Component {
69
constructor () {
710
super()
@@ -20,15 +23,19 @@ class App extends React.Component {
2023
event.stopPropagation()
2124
event.preventDefault()
2225
const file = event.target.files[0]
23-
let reader = new window.FileReader()
24-
reader.onloadend = () => this.saveToIpfs(reader)
25-
reader.readAsArrayBuffer(file)
26+
if (document.getElementById("keepFilename").checked) {
27+
this.saveToIpfsWithFilename(file)
28+
} else {
29+
this.saveToIpfs(file)
30+
}
2631
}
2732

28-
saveToIpfs (reader) {
33+
// Example #1
34+
// Add file to IPFS and return a CID
35+
saveToIpfs (file) {
2936
let ipfsId
30-
const buffer = Buffer.from(reader.result)
31-
this.ipfsApi.add(buffer, { progress: (prog) => console.log(`received: ${prog}`) })
37+
const fileStream = fileReaderPullStream(file)
38+
this.ipfsApi.add(fileStream, { progress: (prog) => console.log(`received: ${prog}`) })
3239
.then((response) => {
3340
console.log(response)
3441
ipfsId = response[0].hash
@@ -39,6 +46,31 @@ class App extends React.Component {
3946
})
4047
}
4148

49+
// Example #2
50+
// Add file to IPFS and wrap it in a directory to keep the original filename
51+
saveToIpfsWithFilename (file) {
52+
let ipfsId
53+
const fileStream = fileReaderPullStream(file)
54+
const fileDetails = {
55+
path: file.name,
56+
content: fileStream
57+
}
58+
const options = {
59+
wrapWithDirectory: true,
60+
progress: (prog) => console.log(`received: ${prog}`)
61+
}
62+
this.ipfsApi.add(fileDetails, options)
63+
.then((response) => {
64+
console.log(response)
65+
// CID of wrapping directory is returned last
66+
ipfsId = response[response.length-1].hash
67+
console.log(ipfsId)
68+
this.setState({added_file_hash: ipfsId})
69+
}).catch((err) => {
70+
console.error(err)
71+
})
72+
}
73+
4274
handleSubmit (event) {
4375
event.preventDefault()
4476
}
@@ -47,7 +79,8 @@ class App extends React.Component {
4779
return (
4880
<div>
4981
<form id='captureMedia' onSubmit={this.handleSubmit}>
50-
<input type='file' onChange={this.captureFile} />
82+
<input type='file' onChange={this.captureFile} /><br/>
83+
<label for='keepFilename'><input type='checkbox' id='keepFilename' name='keepFilename' /> keep filename</label>
5184
</form>
5285
<div>
5386
<a target='_blank'

0 commit comments

Comments
 (0)