This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs(example): show folks how to use pull-streams instead #988
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
'use strict' | ||
/* global self */ | ||
|
||
const $startButton = document.querySelector('#start') | ||
const $stopButton = document.querySelector('#stop') | ||
|
@@ -20,7 +19,9 @@ const $details = document.querySelector('#details') | |
const $allDisabledButtons = document.querySelectorAll('button:disabled') | ||
const $allDisabledInputs = document.querySelectorAll('input:disabled') | ||
const $filesList = document.querySelector('.file-list') | ||
const streamBuffers = require('stream-buffers') | ||
const Ipfs = require('../../../../src/core') | ||
const pullFilereader = require('pull-filereader') | ||
const pull = require('pull-stream') | ||
|
||
let node | ||
let peerInfo | ||
|
@@ -33,7 +34,7 @@ function start () { | |
if (!node) { | ||
updateView('starting', node) | ||
|
||
node = new self.Ipfs({repo: 'ipfs-' + Math.random()}) | ||
node = new Ipfs({repo: 'ipfs-' + Math.random()}) | ||
|
||
node.on('start', () => { | ||
node.id().then((id) => { | ||
|
@@ -119,93 +120,44 @@ function onDrop (event) { | |
onError('IPFS must be started before files can be added') | ||
return | ||
} | ||
const dt = event.dataTransfer | ||
const files = dt.files | ||
|
||
function readFileContents (file) { | ||
return new Promise((resolve) => { | ||
const reader = new window.FileReader() | ||
reader.onload = (event) => resolve(event.target.result) | ||
reader.readAsArrayBuffer(file) | ||
}) | ||
} | ||
|
||
let filesArray = [] | ||
for (let i = 0; i < files.length; i++) { | ||
filesArray.push(files[i]) | ||
let files = [] | ||
for (let i = 0; i < event.dataTransfer.files.length; i++) { | ||
files.push(event.dataTransfer.files[i]) | ||
} | ||
|
||
filesArray.map((file) => { | ||
readFileContents(file) | ||
.then((buffer) => { | ||
let fileSize = buffer.byteLength | ||
|
||
if (fileSize < 50000000) { | ||
return node.files.add([{ | ||
path: file.name, | ||
content: new node.types.Buffer(buffer) | ||
}]) | ||
} else { | ||
// use createAddStream and chunk the file. | ||
let progress = 0 | ||
|
||
let myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({ | ||
// frequency: 10, // in milliseconds. | ||
chunkSize: 32048 // in bytes. | ||
}) | ||
|
||
node.files.createAddStream((err, stream) => { | ||
if (err) throw err | ||
|
||
stream.on('data', (file) => { | ||
$multihashInput.value = file.hash | ||
$filesStatus.innerHTML = `Added ${file.path} as ${file.hash}` | ||
|
||
if (progressbar) { | ||
clearInterval(progressbar) | ||
progress = 0 | ||
} | ||
}) | ||
|
||
myReadableStreamBuffer.on('data', (chunk) => { | ||
progress += chunk.byteLength | ||
}) | ||
|
||
if (!myReadableStreamBuffer.destroy) { | ||
myReadableStreamBuffer.destroy = () => {} | ||
} | ||
|
||
stream.write({ | ||
path: file.name, | ||
content: myReadableStreamBuffer | ||
}) | ||
|
||
myReadableStreamBuffer.put(Buffer.from(buffer)) | ||
myReadableStreamBuffer.stop() | ||
|
||
myReadableStreamBuffer.on('end', () => { | ||
stream.end() | ||
}) | ||
|
||
myReadableStreamBuffer.resume() | ||
|
||
// progress. | ||
let progressbar = setInterval(() => { | ||
console.log('progress: ', progress, '/', fileSize, ' = ', Math.floor((progress / fileSize) * 100), '%') | ||
}, 5000) | ||
}) | ||
pull( | ||
pull.values(files), | ||
pull.through((file) => console.log('Adding %s', file)), | ||
pull.asyncMap((file, cb) => pull( | ||
pull.values([{ | ||
path: file.name, | ||
content: pullFilereader(file) | ||
}]), | ||
node.files.createAddPullStream(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is awesome, but we shouldn't have examples with undocumented (and already agreed to change) API Needs this solved first. ipfs-inactive/interface-js-ipfs-core#126 |
||
pull.collect((err, res) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
}) | ||
.then((files) => { | ||
if (files && files.length) { | ||
$multihashInput.value = files[0].hash | ||
$filesStatus.innerHTML = files | ||
const file = res[0] | ||
console.log('Adding %s finished', file.path) | ||
|
||
$multihashInput.value = file.hash | ||
$filesStatus.innerHTML = `Added ${file.path} as ${file.hash}` | ||
cb(null, file) | ||
}))), | ||
pull.collect((err, files) => { | ||
if (err) { | ||
return onError(err) | ||
} | ||
if (files && files.length) { | ||
$multihashInput.value = files[0].hash | ||
$filesStatus.innerHTML = files | ||
.map((e) => `Added ${e.path} as ${e.hash}`) | ||
.join('<br>') | ||
} | ||
}) | ||
.catch(onError) | ||
}) | ||
} | ||
}) | ||
) | ||
} | ||
|
||
/* | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was supposed to be the example that showed that "no extra batteries required". Kind of sad we lost that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@diasdavid unpkg doesn't allow linking to a branch? We could also publish latest master ourselves on IPFS and link that instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to keep this, with the new release that would be fine to use the unkpkg version again