automatically connect to ipfs when attempting to use a storage functionality
This commit is contained in:
parent
6bcfa7e1f0
commit
5ac4c0f327
|
@ -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) {
|
||||
|
|
19
js/embark.js
19
js/embark.js
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue