Refactored EmbarkJS Storage logic to allow multiple storage providers
This commit is contained in:
parent
494e776f05
commit
fdf0129e5d
|
@ -74,6 +74,10 @@ var EmbarkJS =
|
||||||
/*jshint esversion: 6 */
|
/*jshint esversion: 6 */
|
||||||
//var Ipfs = require('./ipfs.js');
|
//var Ipfs = require('./ipfs.js');
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Smart Contracts
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
var EmbarkJS = {
|
var EmbarkJS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -210,6 +214,10 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Storage
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
EmbarkJS.Storage = {
|
EmbarkJS.Storage = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -221,40 +229,54 @@ EmbarkJS.Storage.Providers = {
|
||||||
EmbarkJS.Storage.IPFS = {
|
EmbarkJS.Storage.IPFS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
EmbarkJS.Storage.connect = function(provider){
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this.ipfsConnection) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
this.setProvider('ipfs');
|
|
||||||
}
|
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
resolve(self.currentStorage.ipfsConnection);
|
||||||
if (err) {
|
});
|
||||||
reject(err);
|
}
|
||||||
} else {
|
else {
|
||||||
resolve(result[0].path);
|
reject('Storage provider not supported');
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.saveText = function(text) {
|
EmbarkJS.Storage.saveText = function(text) {
|
||||||
return this.currentStorage.sendMessage(text);
|
return this.currentStorage.saveText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.get = function(hash) {
|
||||||
|
return this.currentStorage.get(hash);
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.uploadFile = function(inputSelector){
|
||||||
|
return this.currentStorage.uploadFile(inputSelector);
|
||||||
|
}
|
||||||
|
|
||||||
|
EmbarkJS.Storage.getUrl = function(hash){
|
||||||
|
return this.currentStorage.getUrl(hash);
|
||||||
|
}
|
||||||
|
|
||||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
//I don't think currentStorage is used anywhere, this might not be needed
|
//I don't think currentStorage is used anywhere, this might not be needed
|
||||||
//for now until additional storage providers are supported. But keeping it
|
//for now until additional storage providers are supported. But keeping it
|
||||||
//anyways
|
//anyways
|
||||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||||
|
try{
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001');
|
||||||
} else {
|
} else {
|
||||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch(err){
|
||||||
|
this.currentStorage.ipfsConnection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||||
throw Error('Swarm not implemented');
|
throw Error('Swarm not implemented');
|
||||||
this.currentStorage = EmbarkJS.Storage.SWARM;
|
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||||
|
@ -269,12 +291,16 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.saveText = function(text) {
|
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this.ipfsConnection) {
|
if (!self.ipfsConnection) {
|
||||||
this.setProvider('ipfs');
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
}
|
}
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
|
@ -287,7 +313,29 @@ EmbarkJS.Storage.saveText = function(text) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||||
|
var self = this;
|
||||||
|
// TODO: detect type, then convert if needed
|
||||||
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
|
if (!self.ipfsConnection) {
|
||||||
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
|
}
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
|
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||||
|
resolve(node.data);
|
||||||
|
}).catch(function (err){
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var file = inputSelector[0].files[0];
|
var file = inputSelector[0].files[0];
|
||||||
|
|
||||||
|
@ -295,11 +343,15 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||||
throw new Error('no file found');
|
throw new Error('no file found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.ipfsConnection) {
|
if (!self.ipfsConnection) {
|
||||||
this.setProvider('ipfs');
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
}
|
}
|
||||||
|
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
var fileContent = reader.result;
|
var fileContent = reader.result;
|
||||||
|
@ -318,31 +370,16 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.get = function(hash) {
|
EmbarkJS.Storage.IPFS.getUrl = 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) {
|
|
||||||
resolve(node.data);
|
|
||||||
}).catch(function (err){
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
EmbarkJS.Storage.getUrl = function(hash) {
|
|
||||||
//var ipfsHash = web3.toAscii(hash);
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
|
|
||||||
return 'http://localhost:8080/ipfs/' + hash;
|
return 'http://localhost:8080/ipfs/' + hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Messaging
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
EmbarkJS.Messages = {
|
EmbarkJS.Messages = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
117
js/embark.js
117
js/embark.js
|
@ -1,6 +1,10 @@
|
||||||
/*jshint esversion: 6 */
|
/*jshint esversion: 6 */
|
||||||
//var Ipfs = require('./ipfs.js');
|
//var Ipfs = require('./ipfs.js');
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Smart Contracts
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
var EmbarkJS = {
|
var EmbarkJS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -137,6 +141,10 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Storage
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
EmbarkJS.Storage = {
|
EmbarkJS.Storage = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,40 +156,54 @@ EmbarkJS.Storage.Providers = {
|
||||||
EmbarkJS.Storage.IPFS = {
|
EmbarkJS.Storage.IPFS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
EmbarkJS.Storage.connect = function(provider){
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this.ipfsConnection) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
this.setProvider('ipfs');
|
|
||||||
}
|
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
resolve(self.currentStorage.ipfsConnection);
|
||||||
if (err) {
|
});
|
||||||
reject(err);
|
}
|
||||||
} else {
|
else {
|
||||||
resolve(result[0].path);
|
reject('Storage provider not supported');
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.saveText = function(text) {
|
EmbarkJS.Storage.saveText = function(text) {
|
||||||
return this.currentStorage.sendMessage(text);
|
return this.currentStorage.saveText(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.get = function(hash) {
|
||||||
|
return this.currentStorage.get(hash);
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.uploadFile = function(inputSelector){
|
||||||
|
return this.currentStorage.uploadFile(inputSelector);
|
||||||
|
}
|
||||||
|
|
||||||
|
EmbarkJS.Storage.getUrl = function(hash){
|
||||||
|
return this.currentStorage.getUrl(hash);
|
||||||
|
}
|
||||||
|
|
||||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||||
//I don't think currentStorage is used anywhere, this might not be needed
|
//I don't think currentStorage is used anywhere, this might not be needed
|
||||||
//for now until additional storage providers are supported. But keeping it
|
//for now until additional storage providers are supported. But keeping it
|
||||||
//anyways
|
//anyways
|
||||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||||
|
try{
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001');
|
||||||
} else {
|
} else {
|
||||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch(err){
|
||||||
|
this.currentStorage.ipfsConnection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||||
throw Error('Swarm not implemented');
|
throw Error('Swarm not implemented');
|
||||||
this.currentStorage = EmbarkJS.Storage.SWARM;
|
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||||
|
@ -196,12 +218,16 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.saveText = function(text) {
|
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this.ipfsConnection) {
|
if (!self.ipfsConnection) {
|
||||||
this.setProvider('ipfs');
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
}
|
}
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
|
@ -214,7 +240,29 @@ EmbarkJS.Storage.saveText = function(text) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||||
|
var self = this;
|
||||||
|
// TODO: detect type, then convert if needed
|
||||||
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
|
if (!self.ipfsConnection) {
|
||||||
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
|
}
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
|
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||||
|
resolve(node.data);
|
||||||
|
}).catch(function (err){
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var file = inputSelector[0].files[0];
|
var file = inputSelector[0].files[0];
|
||||||
|
|
||||||
|
@ -222,11 +270,15 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||||
throw new Error('no file found');
|
throw new Error('no file found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.ipfsConnection) {
|
if (!self.ipfsConnection) {
|
||||||
this.setProvider('ipfs');
|
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||||
}
|
}
|
||||||
|
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
|
if (!self.ipfsConnection){
|
||||||
|
var connectionError = new Error('No IPFS connection');
|
||||||
|
reject(connectionError);
|
||||||
|
}
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
var fileContent = reader.result;
|
var fileContent = reader.result;
|
||||||
|
@ -245,31 +297,16 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Storage.get = function(hash) {
|
EmbarkJS.Storage.IPFS.getUrl = 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) {
|
|
||||||
resolve(node.data);
|
|
||||||
}).catch(function (err){
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
EmbarkJS.Storage.getUrl = function(hash) {
|
|
||||||
//var ipfsHash = web3.toAscii(hash);
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
|
|
||||||
return 'http://localhost:8080/ipfs/' + hash;
|
return 'http://localhost:8080/ipfs/' + hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//=========================================================
|
||||||
|
// Embark Messaging
|
||||||
|
//=========================================================
|
||||||
|
|
||||||
EmbarkJS.Messages = {
|
EmbarkJS.Messages = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue