2018-05-30 06:34:36 +00:00
|
|
|
/*global web3 */
|
2018-09-10 00:21:52 +00:00
|
|
|
let __embarkSwarm = {_swarmConnection: undefined};
|
2018-09-13 11:29:44 +00:00
|
|
|
let SwarmAPI = require('swarm-api');
|
|
|
|
if (SwarmAPI.default) {
|
|
|
|
SwarmAPI = SwarmAPI.default;
|
|
|
|
}
|
2018-04-30 05:56:43 +00:00
|
|
|
|
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) {
|
2018-09-12 02:43:10 +00:00
|
|
|
this._swarmConnection = new SwarmAPI({gateway: this._connectUrl});
|
2018-05-30 06:34:36 +00:00
|
|
|
}
|
2018-09-10 06:27:19 +00:00
|
|
|
else if (options.useOnlyGivenProvider && web3.bzz.givenProvider !== null) {
|
2018-09-12 02:43:10 +00:00
|
|
|
this._swarmConnection = new SwarmAPI({gateway: 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
|
2018-09-10 06:27:19 +00:00
|
|
|
if (!this._swarmConnection) {
|
2018-05-30 06:34:36 +00:00
|
|
|
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
|
2018-09-10 06:27:19 +00:00
|
|
|
else if (!this._swarmConnection.gateway && this._config.host) {
|
2018-09-10 00:21:52 +00:00
|
|
|
this._swarmConnection.gateway = this._connectUrl;
|
2018-05-30 06:34:36 +00:00
|
|
|
}
|
2018-09-10 00:21:52 +00:00
|
|
|
if (!this._swarmConnection.gateway) {
|
2018-05-01 13:38:13 +00:00
|
|
|
return resolve(false);
|
|
|
|
}
|
2018-09-10 06:27:19 +00:00
|
|
|
this._swarmConnection.isAvailable((err, isAvailable) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(isAvailable);
|
|
|
|
});
|
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-09-10 06:27:19 +00:00
|
|
|
this._swarmConnection.uploadRaw(text, (err, hash) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(hash);
|
|
|
|
});
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-09-10 06:27:19 +00:00
|
|
|
this._swarmConnection.downloadRaw(hash, (err, content) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(content);
|
|
|
|
});
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-09-10 06:27:19 +00:00
|
|
|
this._swarmConnection.uploadRaw(fileContent, (err, hash) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(hash);
|
|
|
|
});
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
reader.onerror = reject;
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkSwarm.getUrl = function (hash) {
|
2018-09-10 06:27:19 +00:00
|
|
|
return `${this._connectUrl}/bzz-raw:/${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
|
|
|
|
2018-08-23 08:59:02 +00:00
|
|
|
__embarkSwarm.resolve = function (_name, callback) {
|
|
|
|
callback(NotAvailable);
|
2018-08-22 09:11:57 +00:00
|
|
|
};
|
|
|
|
|
2018-08-23 08:59:02 +00:00
|
|
|
__embarkSwarm.register = function (_addr, callback) {
|
|
|
|
callback(NotAvailable);
|
2018-08-22 09:11:57 +00:00
|
|
|
};
|