Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Update for latest IPFS, bug fix. #10

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ function getFile(ipfs, rootHash, filename, callback) {
var hash = null
var fileSize, fileName

res.links.forEach(function(link) {
if (link.name === filename) {
hash = link.cid.toString()
fileSize = link.size
fileName = link.name
res.Links.forEach(function(link) {
if (link.Name === filename) {
hash = link.Hash
fileSize = link.Tsize
fileName = link.Name
return false
}
});
Expand All @@ -79,25 +79,16 @@ function getFile(ipfs, rootHash, filename, callback) {
}

console.log("Requesting '" + rootHash + "/" + filename + "'")

const stream = ipfs.cat(hash);
stream.then((value) => {
console.log("Received data for file '" + rootHash + "/" + fileName + "' size: " + value.length)

callback(null, value);
}).catch((err) => {
callback(err, null);
})

var resBuf = new ArrayBuffer(fileSize)
var bufView = new Uint8Array(resBuf)
var offs = 0

const stream = ipfs.catReadableStream(hash)
console.log("Received stream for file '" + rootHash + "/" + fileName + "'")
stream.on('data', function (chunk) {
console.log("Received " + chunk.length + " bytes for file '" +
rootHash + "/" + fileName + "'")
bufView.set(chunk, offs)
offs += chunk.length
});
stream.on('error', function (err) {
callback(err, null)
});
stream.on('end', function () {
callback(null, resBuf)
});
});
}

Expand Down