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

Commit e4dc6fc

Browse files
committed
browser-add-file example
1 parent ed096a5 commit e4dc6fc

File tree

9 files changed

+63101
-0
lines changed

9 files changed

+63101
-0
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/bundle.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId])
10+
/******/ return installedModules[moduleId].exports;
11+
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
29+
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
36+
/******/ // identity function for calling harmory imports with the correct context
37+
/******/ __webpack_require__.i = function(value) { return value; };
38+
39+
/******/ // define getter function for harmory exports
40+
/******/ __webpack_require__.d = function(exports, name, getter) {
41+
/******/ Object.defineProperty(exports, name, {
42+
/******/ configurable: false,
43+
/******/ enumerable: true,
44+
/******/ get: getter
45+
/******/ });
46+
/******/ };
47+
48+
/******/ // getDefaultExport function for compatibility with non-harmony modules
49+
/******/ __webpack_require__.n = function(module) {
50+
/******/ var getter = module && module.__esModule ?
51+
/******/ function getDefault() { return module['default']; } :
52+
/******/ function getModuleExports() { return module; };
53+
/******/ __webpack_require__.d(getter, 'a', getter);
54+
/******/ return getter;
55+
/******/ };
56+
57+
/******/ // Object.prototype.hasOwnProperty.call
58+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
59+
60+
/******/ // __webpack_public_path__
61+
/******/ __webpack_require__.p = "";
62+
63+
/******/ // Load entry module and return exports
64+
/******/ return __webpack_require__(__webpack_require__.s = 0);
65+
/******/ })
66+
/************************************************************************/
67+
/******/ ([
68+
/* 0 */
69+
/***/ function(module, exports) {
70+
71+
var ipfs = window.IpfsApi()
72+
73+
function storeText () {
74+
store(document.getElementById('source').value);
75+
}
76+
77+
function storeFile () {
78+
const file = document.getElementById('filePicker').files[0];
79+
const reader = new FileReader();
80+
reader.onload = function() {
81+
store(reader.result);
82+
};
83+
reader.readAsArrayBuffer(file);
84+
}
85+
86+
// from examples/browser-add
87+
function store (toStore) {
88+
ipfs.add(new window.buffer.Buffer(toStore), function (err, res) {
89+
if (err || !res) {
90+
return console.error('ipfs add error', err, res)
91+
}
92+
93+
res.forEach(function (file) {
94+
console.log('successfully stored', file)
95+
display(file.path)
96+
})
97+
})
98+
}
99+
100+
function display (hash) {
101+
ipfs.cat(hash, function (err, res) {
102+
if (err || !res) {
103+
return console.error('ipfs cat error', err, res)
104+
}
105+
if (res.readable) {
106+
console.error('unhandled: cat result is a pipe', res)
107+
} else {
108+
document.getElementById('hash').innerText = hash
109+
document.getElementById('content').innerText = res
110+
}
111+
})
112+
}
113+
114+
document.addEventListener('DOMContentLoaded', function() {
115+
document.getElementById('storeText').onclick = storeText;
116+
document.getElementById('storeBuffer').onclick = storeFile;
117+
})
118+
119+
120+
/***/ }
121+
/******/ ]);

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

0 commit comments

Comments
 (0)