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

Commit 6c13cfc

Browse files
dignifiedquirenycoliver
authored andcommitted
fix linting
browser-add-file example removed webpack remnants fix trailing comma fix linting errors semicolon lint add filereader global add browser-add-file example
1 parent d62abff commit 6c13cfc

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

examples/browser-add-file/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# JS IPFS API - Example Browser - Add
2+
3+
## Setup
4+
5+
Install [go-ipfs](https://ipfs.io/docs/install/) and run it
6+
7+
```bash
8+
$ ipfs daemon
9+
```
10+
11+
then in this folder run
12+
13+
```bash
14+
$ npm install
15+
$ npm start
16+
```
17+
18+
and open your browser at `http://localhost:8888`

examples/browser-add-file/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>JS IPFS API - Example - Browser - Add</title>
6+
<script src="https://wzrd.in/standalone/buffer"></script> <!-- feross buffer -->
7+
<script src="https://npmcdn.com/ipfs-api/dist/index.js"></script>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<div>
12+
<h1>Add text - <i>works</i></h1>
13+
<textarea id="source">
14+
</textarea>
15+
<button id="storeText">create in ipfs</button>
16+
</div>
17+
<div>
18+
<h1>Add buffer - <i>does not work</i></h1>
19+
<input type="file" id="filePicker">
20+
<button id="storeBuffer">create in ipfs</button>
21+
</div>
22+
<div>
23+
<h1>Response from ipfs - <i>stream res unhandled</i></h1>
24+
<div id="hash">[ipfs hash]</div>
25+
<div id="content">[ipfs content]</div>
26+
</div>
27+
</body>
28+
</html>

examples/browser-add-file/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* globals FileReader */
2+
'use strict'
3+
4+
var ipfs = window.IpfsApi()
5+
6+
function storeText () {
7+
store(document.getElementById('source').value)
8+
}
9+
10+
function storeFile () {
11+
const file = document.getElementById('filePicker').files[0]
12+
const reader = new FileReader()
13+
reader.onload = function () {
14+
store(reader.result)
15+
}
16+
reader.readAsArrayBuffer(file)
17+
}
18+
19+
// from examples/browser-add
20+
function store (toStore) {
21+
ipfs.add(new window.buffer.Buffer(toStore), function (err, res) {
22+
if (err || !res) {
23+
return console.error('ipfs add error', err, res)
24+
}
25+
26+
res.forEach(function (file) {
27+
console.log('successfully stored', file)
28+
display(file.path)
29+
})
30+
})
31+
}
32+
33+
function display (hash) {
34+
ipfs.cat(hash, function (err, res) {
35+
if (err || !res) {
36+
return console.error('ipfs cat error', err, res)
37+
}
38+
if (res.readable) {
39+
console.error('unhandled: cat result is a pipe', res)
40+
} else {
41+
document.getElementById('hash').innerText = hash
42+
document.getElementById('content').innerText = res
43+
}
44+
})
45+
}
46+
47+
document.addEventListener('DOMContentLoaded', function () {
48+
document.getElementById('storeText').onclick = storeText
49+
document.getElementById('storeBuffer').onclick = storeFile
50+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ipfs-api-example-browser-add-file",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "http-server -a 127.0.0.1 -p 8888"
8+
},
9+
"keywords": [],
10+
"author": "nycoliver",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"http-server": "^0.9.0",
14+
"ipfs-api": "^6.0.3"
15+
}
16+
}

examples/browser-add/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ function store () {
1111
}
1212

1313
res.forEach(function (file) {
14-
console.log('successfully stored', file.Hash)
15-
display(file.Hash)
14+
console.log('successfully stored', file)
15+
display(file.path)
1616
})
1717
})
1818
}

examples/browser-add/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
module.exports = {
24
module: {
35
loaders: [{

0 commit comments

Comments
 (0)