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

Commit d622347

Browse files
committed
Final touch before weekend
1 parent 8ce1e98 commit d622347

File tree

1 file changed

+7
-4
lines changed
  • examples/transfer-files/complete/js

1 file changed

+7
-4
lines changed

examples/transfer-files/complete/js/app.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const stop = () => {
6262

6363
const connectPeer = (e) => {
6464
e.target.disabled = true
65+
// Connect directly to a peer via it's multiaddr
6566
ipfs.swarm.connect($connectPeer.value, (err) => {
6667
console.log(err)
6768
if (err) return onError(err)
@@ -77,13 +78,16 @@ const catFile = () => {
7778
$multihashInput.value = ''
7879
$errors.className = 'hidden'
7980
if (multihash) {
81+
// Get a file or many files
8082
ipfs.files.get(multihash, (err, stream) => {
8183
if (err) onError(err)
8284
console.log(stream)
85+
// .get gives us a stream of files
8386
stream.on('data', (file) => {
8487
console.log(file)
8588
const buf = []
8689
if (file.content) {
90+
// once we get a file, we also want to read the data for that file
8791
file.content.on('data', (data) => {
8892
console.log('file got data')
8993
buf.push(data)
@@ -100,6 +104,7 @@ const catFile = () => {
100104
link.setAttribute('href', downloadLink)
101105
link.setAttribute('download', multihash)
102106
const date = (new Date()).toLocaleTimeString()
107+
103108
link.innerText = date + ' - ' + multihash + ' - Size: ' + file.size
104109
const fileList = document.querySelector('.file-list')
105110

@@ -180,6 +185,8 @@ const onDrop = (event) => {
180185
// Get peers from IPFS and display them
181186
let numberOfPeersLastTime = 0
182187
const updatePeers = () => {
188+
// Once in a while, we need to refresh our list of peers in the UI
189+
// .swarm.peers returns an array with all our currently connected peer
183190
ipfs.swarm.peers((err, res) => {
184191
if (err) onError(err)
185192
if (numberOfPeersLastTime !== res.length) {
@@ -206,10 +213,6 @@ function setupEventListeners () {
206213
$stopButton.addEventListener('click', stop)
207214
$catButton.addEventListener('click', catFile)
208215
$connectPeerButton.addEventListener('click', connectPeer)
209-
210-
// TODO temporary default values, remove before merging
211-
$connectPeer.value = '/ip4/0.0.0.0/tcp/9999/ws/ipfs/QmSGmyZtL3BPLxkF9yyaitLsotvratuqeWq1UR8V9BDXcV'
212-
$multihashInput.value = 'QmXxyxhxbt9TU4pJFdpAnqAsTraCMvCNsWsyfe2ZZUjJUn'
213216
}
214217

215218
const states = {

0 commit comments

Comments
 (0)