Add support to use IPFS gateway to read ipfs data #149

Merged
bumi merged 2 commits from feature/support-ipfs-gateway into master 2019-07-01 15:41:08 +00:00
2 changed files with 9 additions and 3 deletions

View File

@ -1,13 +1,14 @@
const ipfsClient = require('ipfs-http-client'); const ipfsClient = require('ipfs-http-client');
const multihashes = require('multihashes'); const multihashes = require('multihashes');
const fetch = require('node-fetch');
class IPFS { class IPFS {
constructor (config) { constructor (config) {
if (!config) { if (!config) {
config = { host: 'localhost', port: '5001', protocol: 'http' }; config = { host: 'localhost', port: '5001', protocol: 'http' };
} }
this._ipfsAPI = ipfsClient(config);
this._config = config; this._config = config;
this._ipfsAPI = ipfsClient(config);
} }
catAndMerge (data, deserialize) { catAndMerge (data, deserialize) {
@ -38,8 +39,12 @@ class IPFS {
if (hashData.hasOwnProperty('hashSize')) { if (hashData.hasOwnProperty('hashSize')) {
ipfsHash = this.encodeHash(hashData); ipfsHash = this.encodeHash(hashData);
} }
if (this._config['gatewayUrl']) {
return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text());
} else {
return this._ipfsAPI.cat(ipfsHash); return this._ipfsAPI.cat(ipfsHash);
} }
}
pin (hashData) { pin (hashData) {
let ipfsHash = hashData; // default - if it is a string let ipfsHash = hashData; // default - if it is a string

View File

@ -58,8 +58,9 @@
}, },
"dependencies": { "dependencies": {
"ethers": "^4.0.29", "ethers": "^4.0.29",
"ipfs-http-client": "^32.0.1", "ipfs-http-client": "^30.1.3",
"kosmos-schemas": "^2.1.0", "kosmos-schemas": "^2.1.0",
"node-fetch": "^2.6.0",
"tv4": "^1.3.0" "tv4": "^1.3.0"
}, },
"keywords": [ "keywords": [