diff --git a/examples/browser-add-file/README.md b/examples/browser-add-file/README.md new file mode 100644 index 000000000..c052a5723 --- /dev/null +++ b/examples/browser-add-file/README.md @@ -0,0 +1,18 @@ +# JS IPFS API - Example Browser - Add + +## Setup + +Install [go-ipfs](https://ipfs.io/docs/install/) and run it + +```bash +$ ipfs daemon +``` + +then in this folder run + +```bash +$ npm install +$ npm start +``` + +and open your browser at `http://localhost:8888` diff --git a/examples/browser-add-file/index.html b/examples/browser-add-file/index.html new file mode 100644 index 000000000..bf1910ee8 --- /dev/null +++ b/examples/browser-add-file/index.html @@ -0,0 +1,28 @@ + + + + + JS IPFS API - Example - Browser - Add + + + + + +
+

Add text - works

+ + +
+
+

Add buffer - does not work

+ + +
+
+

Response from ipfs - stream res unhandled

+
[ipfs hash]
+
[ipfs content]
+
+ + diff --git a/examples/browser-add-file/index.js b/examples/browser-add-file/index.js new file mode 100644 index 000000000..100a9ca76 --- /dev/null +++ b/examples/browser-add-file/index.js @@ -0,0 +1,50 @@ +/* globals FileReader */ +'use strict' + +var ipfs = window.IpfsApi() + +function storeText () { + store(document.getElementById('source').value) +} + +function storeFile () { + const file = document.getElementById('filePicker').files[0] + const reader = new FileReader() + reader.onload = function () { + store(reader.result) + } + reader.readAsArrayBuffer(file) +} + +// from examples/browser-add +function store (toStore) { + ipfs.add(new window.buffer.Buffer(toStore), function (err, res) { + if (err || !res) { + return console.error('ipfs add error', err, res) + } + + res.forEach(function (file) { + console.log('successfully stored', file) + display(file.path) + }) + }) +} + +function display (hash) { + ipfs.cat(hash, function (err, res) { + if (err || !res) { + return console.error('ipfs cat error', err, res) + } + if (res.readable) { + console.error('unhandled: cat result is a pipe', res) + } else { + document.getElementById('hash').innerText = hash + document.getElementById('content').innerText = res + } + }) +} + +document.addEventListener('DOMContentLoaded', function () { + document.getElementById('storeText').onclick = storeText + document.getElementById('storeBuffer').onclick = storeFile +}) diff --git a/examples/browser-add-file/package.json b/examples/browser-add-file/package.json new file mode 100644 index 000000000..56dd07187 --- /dev/null +++ b/examples/browser-add-file/package.json @@ -0,0 +1,16 @@ +{ + "name": "ipfs-api-example-browser-add-file", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "http-server -a 127.0.0.1 -p 8888" + }, + "keywords": [], + "author": "nycoliver", + "license": "MIT", + "devDependencies": { + "http-server": "^0.9.0", + "ipfs-api": "^6.0.3" + } +} diff --git a/examples/browser-add/index.js b/examples/browser-add/index.js index 183956bd9..9484314b0 100644 --- a/examples/browser-add/index.js +++ b/examples/browser-add/index.js @@ -11,8 +11,8 @@ function store () { } res.forEach(function (file) { - console.log('successfully stored', file.Hash) - display(file.Hash) + console.log('successfully stored', file) + display(file.path) }) }) } @@ -31,4 +31,6 @@ function display (hash) { }) } -document.getElementById('store').onclick = store +document.addEventListener('DOMContentLoaded', () => { + document.getElementById('store').onclick = store +}) diff --git a/examples/browser-add/package.json b/examples/browser-add/package.json index a2c8dbcfb..b542c0778 100644 --- a/examples/browser-add/package.json +++ b/examples/browser-add/package.json @@ -4,15 +4,17 @@ "description": "", "main": "index.js", "scripts": { - "start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888" + "start": "webpack index.js bundle.js && http-server -a 127.0.0.1 -p 8888" }, "keywords": [], "author": "Friedel Ziegelmayer", "license": "MIT", "devDependencies": { "brfs": "^1.4.3", - "browserify": "^13.0.1", "http-server": "^0.9.0", - "ipfs-api": "^6.0.3" + "ipfs-api": "^6.0.3", + "json-loader": "^0.5.4", + "transform-loader": "^0.2.3", + "webpack": "^2.1.0-beta.20" } } diff --git a/examples/browser-add/webpack.config.js b/examples/browser-add/webpack.config.js new file mode 100644 index 000000000..135e35980 --- /dev/null +++ b/examples/browser-add/webpack.config.js @@ -0,0 +1,14 @@ +'use strict' + +module.exports = { + module: { + loaders: [{ + test: /\.json/, + loader: 'json' + }], + preLoaders: [{ + test: /\.js$/, + loader: 'transform?brfs' + }] + } +}