Files: 6df421c40b28e10fe7138c1519f7c78ebb52f470 / tmpl / docs / basics / read-a-file.html.js
1782 bytesRaw
1 | var page = require('../../page.part') |
2 | var com = require('../../com.part') |
3 | |
4 | module.exports = () => page({ |
5 | section: 'docs', |
6 | tab: 'docs-basics', |
7 | path :'/docs/basics/read-a-file.html', |
8 | content: ` |
9 | <p> |
10 | When Scuttlebot finds the hash-ID of a file in a message, it will request its contents from the network. |
11 | You can then read the file from the blobstore. |
12 | </p> |
13 | |
14 | <h2>Wait for the file to arrive</h2> |
15 | ${ com.code({ js: jsSnippet1, bash: bashSnippet1 }) } |
16 | |
17 | <h2>Read a file to memory</h2> |
18 | ${ com.code({ js: jsSnippet2, bash: bashSnippet2 }) } |
19 | |
20 | <h2>Read a file to disk</h2> |
21 | ${ com.code({ js: jsSnippet3, bash: bashSnippet3 }) } |
22 | |
23 | <p class="next"><a href="/docs/basics/encryption.html">Encryption</a></p> |
24 | <ul class="see-also"> |
25 | <li><a href="/modules/scuttlebot-blobs.html">Scuttlebot.Blobs API</a></li> |
26 | <li><a href="/advanced/linking-messages.html">Linking messages</a></li> |
27 | <li><a href="/advanced/pull-streams.html">Pull streams</a></li> |
28 | </ul> |
29 | ` |
30 | }) |
31 | |
32 | var jsSnippet1 = ` |
33 | // want will not cb() until the file arrives |
34 | sbot.blobs.want(hash, function (err) { |
35 | // ready to read |
36 | }) |
37 | ` |
38 | |
39 | var bashSnippet1 = ` |
40 | $ sbot blobs.want "&hT/5N2Kgbdv3...XYCA=.sha256" |
41 | ` |
42 | |
43 | var jsSnippet2 = ` |
44 | var pull = require('pull-stream') |
45 | pull( |
46 | sbot.blobs.get(hash), |
47 | pull.collect(function (err, values) { |
48 | // eg values.join('') == 'hello, world' |
49 | }) |
50 | ) |
51 | ` |
52 | |
53 | var bashSnippet2 = ` |
54 | $ sbot blobs.get "&hT/5N2Kgbdv3...XYCA=.sha256" |
55 | hello, world |
56 | ` |
57 | |
58 | var jsSnippet3 = ` |
59 | var fs = require('fs') |
60 | var pull = require('pull-stream') |
61 | var toPull = require('stream-to-pull-stream') |
62 | pull( |
63 | sbot.blobs.get(hash), |
64 | toPull.sink(fs.createWriteStream('./hello.txt'), cb) |
65 | ) |
66 | ` |
67 | |
68 | var bashSnippet3 = ` |
69 | $ sbot blobs.get "&hT/5N2Kgbdv3...XYCA=.sha256" > ./hello.txt |
70 | ` |
Built with git-ssb-web