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

Commit

Permalink
feat: upload example works with big files
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lidel authored and daviddias committed Nov 3, 2018
1 parent 0175406 commit 62b844f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/upload-file-via-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"babel-core": "^5.4.7",
"babel-loader": "^5.1.2",
"ipfs-api": "../../",
"pull-file-reader": "^1.0.2",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-hot-loader": "^1.3.1",
Expand Down
47 changes: 40 additions & 7 deletions examples/upload-file-via-browser/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
const React = require('react')
const ipfsAPI = require('ipfs-api')

// create a stream from a file, which enables uploads of big files without allocating memory twice
const fileReaderPullStream = require('pull-file-reader')

class App extends React.Component {
constructor () {
super()
Expand All @@ -20,15 +23,19 @@ class App extends React.Component {
event.stopPropagation()
event.preventDefault()
const file = event.target.files[0]
let reader = new window.FileReader()
reader.onloadend = () => this.saveToIpfs(reader)
reader.readAsArrayBuffer(file)
if (document.getElementById("keepFilename").checked) {
this.saveToIpfsWithFilename(file)
} else {
this.saveToIpfs(file)
}
}

saveToIpfs (reader) {
// Example #1
// Add file to IPFS and return a CID
saveToIpfs (file) {
let ipfsId
const buffer = Buffer.from(reader.result)
this.ipfsApi.add(buffer, { progress: (prog) => console.log(`received: ${prog}`) })
const fileStream = fileReaderPullStream(file)
this.ipfsApi.add(fileStream, { progress: (prog) => console.log(`received: ${prog}`) })
.then((response) => {
console.log(response)
ipfsId = response[0].hash
Expand All @@ -39,6 +46,31 @@ class App extends React.Component {
})
}

// Example #2
// Add file to IPFS and wrap it in a directory to keep the original filename
saveToIpfsWithFilename (file) {
let ipfsId
const fileStream = fileReaderPullStream(file)
const fileDetails = {
path: file.name,
content: fileStream
}
const options = {
wrapWithDirectory: true,
progress: (prog) => console.log(`received: ${prog}`)
}
this.ipfsApi.add(fileDetails, options)
.then((response) => {
console.log(response)
// CID of wrapping directory is returned last
ipfsId = response[response.length-1].hash
console.log(ipfsId)
this.setState({added_file_hash: ipfsId})
}).catch((err) => {
console.error(err)
})
}

handleSubmit (event) {
event.preventDefault()
}
Expand All @@ -47,7 +79,8 @@ class App extends React.Component {
return (
<div>
<form id='captureMedia' onSubmit={this.handleSubmit}>
<input type='file' onChange={this.captureFile} />
<input type='file' onChange={this.captureFile} /><br/>
<label for='keepFilename'><input type='checkbox' id='keepFilename' name='keepFilename' /> keep filename</label>
</form>
<div>
<a target='_blank'
Expand Down

0 comments on commit 62b844f

Please sign in to comment.