2018-05-30 06:34:36 +00:00
|
|
|
/*global web3 */
|
2018-04-30 05:56:43 +00:00
|
|
|
let __embarkSwarm = {};
|
|
|
|
const bytes = require("eth-lib/lib/bytes");
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.setProvider = function (options) {
|
2018-05-30 06:34:36 +00:00
|
|
|
let protocol = options.protocol || 'http';
|
|
|
|
let port = options.port ? `:${options.port}` : '';
|
|
|
|
|
|
|
|
this._config = options;
|
|
|
|
this._connectUrl = `${protocol}://${options.host}${port}`;
|
|
|
|
this._connectError = new Error(`Cannot connect to Swarm node on ${this._connectUrl}`);
|
2018-04-30 05:56:43 +00:00
|
|
|
|
2018-05-01 14:20:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-30 05:56:43 +00:00
|
|
|
try {
|
2018-05-30 06:34:36 +00:00
|
|
|
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
|
|
|
|
web3.bzz.setProvider(this._connectUrl);
|
|
|
|
}
|
|
|
|
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
|
|
|
|
web3.bzz.setProvider(web3.bzz.givenProvider);
|
2018-04-30 05:56:43 +00:00
|
|
|
}
|
|
|
|
resolve(this);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2018-05-30 06:34:36 +00:00
|
|
|
reject(this._connectError);
|
2018-04-30 05:56:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.isAvailable = function () {
|
2018-04-30 05:56:43 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-30 06:34:36 +00:00
|
|
|
// if web3 swarm object doesn't exist
|
|
|
|
if (!web3.bzz) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
// swarm obj exists, but has no provider set (seems to happen a LOT!),
|
|
|
|
// try setting the provider to our currently set provider again
|
|
|
|
else if(!web3.bzz.currentProvider && this._config.host){
|
|
|
|
web3.bzz.setProvider(this._connectUrl);
|
|
|
|
}
|
|
|
|
if (!web3.bzz.currentProvider) {
|
2018-05-01 13:38:13 +00:00
|
|
|
return resolve(false);
|
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
web3.bzz.isAvailable()
|
2018-04-30 05:56:43 +00:00
|
|
|
.then(resolve)
|
2018-05-01 13:38:13 +00:00
|
|
|
.catch(() => {
|
2018-05-30 06:34:36 +00:00
|
|
|
reject(this._connectError);
|
2018-05-01 13:38:13 +00:00
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.saveText = function (text) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.isAvailable().then((isAvailable) => {
|
2018-05-01 13:38:13 +00:00
|
|
|
if (!isAvailable) {
|
2018-05-30 06:34:36 +00:00
|
|
|
return reject(this._connectError);
|
2018-05-01 13:38:13 +00:00
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
web3.bzz.upload(text)
|
2018-04-30 05:56:43 +00:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.get = function (hash) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.isAvailable().then((isAvailable) => {
|
2018-05-01 13:38:13 +00:00
|
|
|
if (!isAvailable) {
|
2018-05-30 06:34:36 +00:00
|
|
|
return reject(this._connectError);
|
2018-05-01 13:38:13 +00:00
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
web3.bzz.download(hash)
|
2018-04-30 05:56:43 +00:00
|
|
|
.then((uint8Array) => resolve(bytes.toString(bytes.fromUint8Array(uint8Array))))
|
|
|
|
.catch(reject);
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.uploadFile = function (inputSelector) {
|
2018-04-30 05:56:43 +00:00
|
|
|
let file = inputSelector[0].files[0];
|
|
|
|
|
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error('no file found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-01 14:20:27 +00:00
|
|
|
const reader = new FileReader();
|
2018-04-30 05:56:43 +00:00
|
|
|
reader.onloadend = (event) => {
|
2018-05-01 14:20:27 +00:00
|
|
|
const fileContent = new Uint8Array(event.target.result);
|
2018-04-30 05:56:43 +00:00
|
|
|
this.isAvailable().then((isAvailable) => {
|
2018-05-01 13:38:13 +00:00
|
|
|
if (!isAvailable) {
|
2018-05-30 06:34:36 +00:00
|
|
|
return reject(this._connectError);
|
2018-05-01 13:38:13 +00:00
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
web3.bzz.upload(fileContent)
|
2018-05-01 13:38:13 +00:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2018-04-30 05:56:43 +00:00
|
|
|
}).catch(reject);
|
|
|
|
};
|
|
|
|
reader.onerror = reject;
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.getUrl = function (hash) {
|
2018-05-31 10:18:25 +00:00
|
|
|
return `${this._config.getUrl || this._connectUrl + '/bzz:/'}${hash}`;
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
|
2018-08-22 09:58:01 +00:00
|
|
|
const NotAvailable = "Not available with Swarm";
|
2018-08-22 09:11:57 +00:00
|
|
|
|
|
|
|
__embarkSwarm.resolve = function () {
|
|
|
|
return new Error(NotAvailable);
|
|
|
|
};
|
|
|
|
|
|
|
|
__embarkSwarm.register = function () {
|
|
|
|
return new Error(NotAvailable);
|
|
|
|
};
|