Skip to content

Commit 5ac4c0f

Browse files
committed
automatically connect to ipfs when attempting to use a storage functionality
1 parent 6bcfa7e commit 5ac4c0f

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

js/build/embark.bundle.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,26 @@ var EmbarkJS =
156156
EmbarkJS.Storage = {
157157
};
158158

159-
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})<F37>
160-
//{server: ‘localhost’, port: ‘5001’};
159+
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})
161160

162161
EmbarkJS.Storage.setProvider = function(provider, options) {
163162
if (provider === 'ipfs') {
164163
this.currentStorage = EmbarkJS.Storage.IPFS;
165-
this.ipfsConnection = IpfsApi(options.server, options.port);
164+
if (options === undefined) {
165+
this.ipfsConnection = IpfsApi('localhost', '5001');
166+
} else {
167+
this.ipfsConnection = IpfsApi(options.server, options.port);
168+
}
166169
} else {
167170
throw Error('unknown provider');
168171
}
169172
};
170173

171174
EmbarkJS.Storage.saveText = function(text) {
172175
var self = this;
176+
if (!this.ipfsConnection) {
177+
this.setProvider('ipfs');
178+
}
173179
var promise = new Promise(function(resolve, reject) {
174180
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
175181
if (err) {
@@ -191,6 +197,10 @@ var EmbarkJS =
191197
throw new Error('no file found');
192198
}
193199

200+
if (!this.ipfsConnection) {
201+
this.setProvider('ipfs');
202+
}
203+
194204
var promise = new Promise(function(resolve, reject) {
195205
var reader = new FileReader();
196206
reader.onloadend = function() {
@@ -214,6 +224,9 @@ var EmbarkJS =
214224
var self = this;
215225
// TODO: detect type, then convert if needed
216226
//var ipfsHash = web3.toAscii(hash);
227+
if (!this.ipfsConnection) {
228+
this.setProvider('ipfs');
229+
}
217230

218231
var promise = new Promise(function(resolve, reject) {
219232
self.ipfsConnection.object.get([hash]).then(function(node) {

js/embark.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,26 @@ EmbarkJS.IPFS = 'ipfs';
109109
EmbarkJS.Storage = {
110110
};
111111

112-
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})<F37>
113-
//{server: ‘localhost’, port: ‘5001’};
112+
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})
114113

115114
EmbarkJS.Storage.setProvider = function(provider, options) {
116115
if (provider === 'ipfs') {
117116
this.currentStorage = EmbarkJS.Storage.IPFS;
118-
this.ipfsConnection = IpfsApi(options.server, options.port);
117+
if (options === undefined) {
118+
this.ipfsConnection = IpfsApi('localhost', '5001');
119+
} else {
120+
this.ipfsConnection = IpfsApi(options.server, options.port);
121+
}
119122
} else {
120123
throw Error('unknown provider');
121124
}
122125
};
123126

124127
EmbarkJS.Storage.saveText = function(text) {
125128
var self = this;
129+
if (!this.ipfsConnection) {
130+
this.setProvider('ipfs');
131+
}
126132
var promise = new Promise(function(resolve, reject) {
127133
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
128134
if (err) {
@@ -144,6 +150,10 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
144150
throw new Error('no file found');
145151
}
146152

153+
if (!this.ipfsConnection) {
154+
this.setProvider('ipfs');
155+
}
156+
147157
var promise = new Promise(function(resolve, reject) {
148158
var reader = new FileReader();
149159
reader.onloadend = function() {
@@ -167,6 +177,9 @@ EmbarkJS.Storage.get = function(hash) {
167177
var self = this;
168178
// TODO: detect type, then convert if needed
169179
//var ipfsHash = web3.toAscii(hash);
180+
if (!this.ipfsConnection) {
181+
this.setProvider('ipfs');
182+
}
170183

171184
var promise = new Promise(function(resolve, reject) {
172185
self.ipfsConnection.object.get([hash]).then(function(node) {

0 commit comments

Comments
 (0)