diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f4cd7f1..0532a5428 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,24 @@ + +## [26.1.2](https://github.com/ipfs/js-ipfs-api/compare/v26.1.0...v26.1.2) (2018-11-03) + + +### Features + +* go-ipfs 0.4.18 ([e3e4d6c](https://github.com/ipfs/js-ipfs-api/commit/e3e4d6c)) +* upload example works with big files ([62b844f](https://github.com/ipfs/js-ipfs-api/commit/62b844f)) + + + + +## [26.1.1](https://github.com/ipfs/js-ipfs-api/compare/v26.1.0...v26.1.1) (2018-11-03) + + +### Features + +* go-ipfs 0.4.18 ([9178e7d](https://github.com/ipfs/js-ipfs-api/commit/9178e7d)) + + + # [26.1.0](https://github.com/ipfs/js-ipfs-api/compare/v26.0.3...v26.1.0) (2018-10-31) diff --git a/examples/upload-file-via-browser/package.json b/examples/upload-file-via-browser/package.json index 13a1235f1..9f38c71f1 100644 --- a/examples/upload-file-via-browser/package.json +++ b/examples/upload-file-via-browser/package.json @@ -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", diff --git a/examples/upload-file-via-browser/src/App.js b/examples/upload-file-via-browser/src/App.js index ec542f15b..4e905cee7 100644 --- a/examples/upload-file-via-browser/src/App.js +++ b/examples/upload-file-via-browser/src/App.js @@ -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() @@ -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 @@ -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() } @@ -47,7 +79,8 @@ class App extends React.Component { return (