Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit cf063ca

Browse files
achingbraindryajov
authored andcommitted
docs: Add browser example for streaming files
1 parent 9696f44 commit cf063ca

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
2020
- [js-ipfs in the browser with WebPack](./browser-webpack)
2121
- [js-ipfs in the browser with a `<script>` tag](./browser-script-tag)
2222
- [js-ipfs in electron](./run-in-electron)
23+
- [Using streams to add a directory of files to ipfs](./browser-add-readable-stream)
2324

2425
## Understanding the IPFS Stack
2526

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Using duplex streams to add files to IPFS in the browser
2+
3+
If you have a number of files that you'd like to add to IPFS and end up with a hash representing the directory containing your files, you can invoke [`ipfs.files.add`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add) with an array of objects.
4+
5+
But what if you don't know how many there will be in advance? You can add multiple files to a directory in IPFS over time by using [`ipfs.files.addReadableStream`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addreadablestream).
6+
7+
See `index.js` for a working example and open `index.html` in your browser to see it run.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<pre id="output"></pre>
4+
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
5+
<script src="index.js"></script>
6+
</body>
7+
</html>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
/* global Ipfs */
4+
/* eslint-env browser */
5+
6+
const repoPath = 'ipfs-' + Math.random()
7+
const ipfs = new Ipfs({ repo: repoPath })
8+
9+
ipfs.on('ready', () => {
10+
const directory = 'directory'
11+
12+
// Our list of files
13+
const files = createFiles(directory)
14+
15+
streamFiles(directory, files, (err, directoryHash) => {
16+
if (err) {
17+
return log(`There was an error adding the files ${err}`)
18+
}
19+
20+
ipfs.ls(directoryHash, (err, files) => {
21+
if (err) {
22+
return log(`There was an error listing the files ${err}`)
23+
}
24+
25+
log(`
26+
--
27+
28+
Directory contents:
29+
30+
${directory}/ ${directoryHash}`)
31+
32+
files.forEach((file, index) => {
33+
log(` ${index < files.length - 1 ? '\u251C' : '\u2514'}\u2500 ${file.name} ${file.path} ${file.hash}`)
34+
})
35+
})
36+
})
37+
})
38+
39+
const createFiles = (directory) => {
40+
return [{
41+
path: `${directory}/file1.txt`,
42+
43+
// content could be a stream, a url etc
44+
content: ipfs.types.Buffer.from('one', 'utf8')
45+
}, {
46+
path: `${directory}/file2.txt`,
47+
content: ipfs.types.Buffer.from('two', 'utf8')
48+
}, {
49+
path: `${directory}/file3.txt`,
50+
content: ipfs.types.Buffer.from('three', 'utf8')
51+
}]
52+
}
53+
54+
const streamFiles = (directory, files, cb) => {
55+
// Create a stream to write files to
56+
const stream = ipfs.files.addReadableStream()
57+
stream.on('data', function (data) {
58+
log(`Added ${data.path} hash: ${data.hash}`)
59+
60+
// The last data event will contain the directory hash
61+
if (data.path === directory) {
62+
cb(null, data.hash)
63+
}
64+
})
65+
66+
// Add the files one by one
67+
files.forEach(file => stream.write(file))
68+
69+
// When we have no more files to add, close the stream
70+
stream.end()
71+
}
72+
73+
const log = (line) => {
74+
document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
75+
}

0 commit comments

Comments
 (0)