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

Commit 3b51fae

Browse files
committed
docs: Add browser example for streaming files
1 parent 42545dc commit 3b51fae

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
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#files-api) 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 streams.
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
3+
/* global Ipfs */
4+
/* eslint-env browser */
5+
6+
const repoPath = 'ipfs-' + Math.random()
7+
const node = new Ipfs({ repo: repoPath })
8+
9+
node.on('ready', () => {
10+
const directory = 'directory'
11+
12+
// Our list of files
13+
const files = [{
14+
path: `${directory}/file1.txt`,
15+
16+
// content could be a stream, a url etc
17+
content: node.types.Buffer.from('one', 'utf8')
18+
}, {
19+
path: `${directory}/file2.txt`,
20+
content: node.types.Buffer.from('two', 'utf8')
21+
}, {
22+
path: `${directory}/file3.txt`,
23+
content: node.types.Buffer.from('three', 'utf8')
24+
}]
25+
26+
streamFiles(directory, files, (err, directoryHash) => {
27+
if (err) {
28+
return log(`There was an error adding the files ${err}`)
29+
}
30+
31+
node.ls(directoryHash, (err, files) => {
32+
if (err) {
33+
return log(`There was an error listing the files ${err}`)
34+
}
35+
36+
log(`
37+
--
38+
39+
Directory contents:
40+
41+
${directory}/ ${directoryHash}`)
42+
43+
files.forEach((file, index) => {
44+
log(` ${index < files.length - 1 ? '\u251C' : '\u2514'}\u2500 ${file.name} ${file.path} ${file.hash}`)
45+
})
46+
})
47+
})
48+
})
49+
50+
const streamFiles = (directory, files, cb) => {
51+
// Create a stream to write files to
52+
const stream = node.files.addReadableStream()
53+
stream.on('data', function (data) {
54+
log(`Added ${data.path} hash: ${data.hash}`)
55+
56+
// The last data event will contain the directory hash
57+
if (data.path === directory) {
58+
cb(null, data.hash)
59+
}
60+
})
61+
62+
// Add the files one by one
63+
files.forEach(file => stream.write(file))
64+
65+
// When we have no more files to add, close the stream
66+
stream.end()
67+
}
68+
69+
const log = (line) => {
70+
document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
71+
}

0 commit comments

Comments
 (0)