Add support to use IPFS gateway to read ipfs data

This makes caching of IPFS documents easier. Browsers should cache those
by default
This commit is contained in:
2019-07-01 15:53:18 +02:00
parent 16141ed482
commit 61fe9add1b
2 changed files with 8 additions and 2 deletions

View File

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