Skip to content

Commit

Permalink
Support importing IPFS files
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Mar 9, 2017
1 parent a6ece05 commit 4f0bed8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,22 @@ var run = function () {
}
})
}

function handleIPFS (url, cb) {
// replace ipfs:// with /ipfs/
url = url.replace(/^ipfs:\/\/?/, '/ipfs/')

$('#output').append($('<div/>').append($('<pre/>').text('Loading ' + url + ' ...')))
return $.get('https://gateway.ipfs.io/' + url)
.done(function (data) {
cb(null, data)
})
.fail(function (xhr, text, err) {
// NOTE: on some browsers, err equals to '' for certain errors (such as offline browser)
cb(err || 'Unknown transport error')
})
}

function handleImportCall (url, cb) {
var match
if (files.exists(url)) {
Expand All @@ -553,6 +569,17 @@ var run = function () {
return
}

// FIXME: at some point we should invalidate the cache
files.addReadOnly(url, content)
cb(null, content)
})
} else if ((match = /^(\/?ipfs:?\/\/?.+)/.exec(url))) {
handleIPFS(match[1], function (err, content) {
if (err) {
cb('Unable to import "' + url + '": ' + err)
return
}

// FIXME: at some point we should invalidate the cache
files.addReadOnly(url, content)
cb(null, content)
Expand Down

0 comments on commit 4f0bed8

Please sign in to comment.