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

Commit e9821ce

Browse files
committed
update example to pull-streams
1 parent ef4bf22 commit e9821ce

File tree

1 file changed

+14
-19
lines changed
  • examples/exchange-files-in-browser/public/js

1 file changed

+14
-19
lines changed

examples/exchange-files-in-browser/public/js/app.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,20 @@ function createFileBlob (data, multihash) {
7676
}
7777

7878
function getFile () {
79-
const multihash = $multihashInput.value
79+
const cid = $multihashInput.value
8080

8181
$multihashInput.value = ''
8282

8383
$errors.className = 'hidden'
8484

85-
if (!multihash) {
86-
return console.log('no multihash was inserted')
87-
}
85+
if (!cid) { return console.log('no multihash was inserted') }
8886

89-
node.files.get(multihash, (err, files) => {
87+
node.files.get(cid, (err, files) => {
9088
if (err) { return onError(err) }
9189

9290
files.forEach((file) => {
9391
if (file.content) {
94-
console.log('-> typeof', typeof file.content)
95-
const listItem = createFileBlob(file.content, multihash)
92+
const listItem = createFileBlob(file.content, cid)
9693
$filesList.insertBefore(listItem, $filesList.firstChild)
9794
}
9895
})
@@ -132,21 +129,19 @@ function onDrop (event) {
132129
const fileStream = new ReadableStreamBuffer({ chunkSize: 32048 })
133130
if (!fileStream.destroy) { fileStream.destroy = () => {} }
134131

135-
// TODO change this to use a pull-stream instead! :)
136-
const stream = node.files.addReadableStream()
137-
138-
stream.on('data', (fileAdded) => {
139-
$multihashInput.value = fileAdded.hash
140-
$filesStatus.innerHTML = `Added ${fileAdded.path} as ${fileAdded.hash}`
141-
})
142-
143-
stream.write({ path: file.name, content: fileStream })
144-
145132
fileStream.put(Buffer.from(buffer))
146133
fileStream.stop()
147134

148-
fileStream.on('end', () => stream.end())
149-
fileStream.resume()
135+
node.files.add([{
136+
path: file.name,
137+
content: fileStream
138+
}], (err, filesAdded) => {
139+
if (err) { return onError(err) }
140+
141+
const f = filesAdded[0]
142+
$multihashInput.value = f.hash
143+
$filesStatus.innerHTML = `Added ${f.path} as ${f.hash}`
144+
})
150145
})
151146
.catch(onError)
152147
})

0 commit comments

Comments
 (0)