automatically connect to ipfs when attempting to use a storage functionality

This commit is contained in:
Iuri Matias 2016-12-02 06:48:02 -05:00
parent 6bcfa7e1f0
commit 5ac4c0f327
2 changed files with 32 additions and 6 deletions

View File

@ -156,13 +156,16 @@ var EmbarkJS =
EmbarkJS.Storage = {
};
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})<F37>
//{server: localhost, port: 5001};
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})
EmbarkJS.Storage.setProvider = function(provider, options) {
if (provider === 'ipfs') {
this.currentStorage = EmbarkJS.Storage.IPFS;
this.ipfsConnection = IpfsApi(options.server, options.port);
if (options === undefined) {
this.ipfsConnection = IpfsApi('localhost', '5001');
} else {
this.ipfsConnection = IpfsApi(options.server, options.port);
}
} else {
throw Error('unknown provider');
}
@ -170,6 +173,9 @@ var EmbarkJS =
EmbarkJS.Storage.saveText = function(text) {
var self = this;
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
if (err) {
@ -191,6 +197,10 @@ var EmbarkJS =
throw new Error('no file found');
}
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onloadend = function() {
@ -214,6 +224,9 @@ var EmbarkJS =
var self = this;
// TODO: detect type, then convert if needed
//var ipfsHash = web3.toAscii(hash);
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.object.get([hash]).then(function(node) {

View File

@ -109,13 +109,16 @@ EmbarkJS.IPFS = 'ipfs';
EmbarkJS.Storage = {
};
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})<F37>
//{server: localhost, port: 5001};
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})
EmbarkJS.Storage.setProvider = function(provider, options) {
if (provider === 'ipfs') {
this.currentStorage = EmbarkJS.Storage.IPFS;
this.ipfsConnection = IpfsApi(options.server, options.port);
if (options === undefined) {
this.ipfsConnection = IpfsApi('localhost', '5001');
} else {
this.ipfsConnection = IpfsApi(options.server, options.port);
}
} else {
throw Error('unknown provider');
}
@ -123,6 +126,9 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
EmbarkJS.Storage.saveText = function(text) {
var self = this;
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
if (err) {
@ -144,6 +150,10 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
throw new Error('no file found');
}
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onloadend = function() {
@ -167,6 +177,9 @@ EmbarkJS.Storage.get = function(hash) {
var self = this;
// TODO: detect type, then convert if needed
//var ipfsHash = web3.toAscii(hash);
if (!this.ipfsConnection) {
this.setProvider('ipfs');
}
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.object.get([hash]).then(function(node) {