2017-12-28 17:16:50 +00:00
|
|
|
import IpfsApi from 'ipfs-api';
|
|
|
|
|
2017-12-28 00:10:05 +00:00
|
|
|
let __embarkIPFS = {};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.setProvider = function (options) {
|
2017-12-28 00:10:05 +00:00
|
|
|
var self = this;
|
2018-05-01 13:38:13 +00:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2017-12-28 00:10:05 +00:00
|
|
|
try {
|
|
|
|
if (options === undefined) {
|
2018-05-30 06:34:36 +00:00
|
|
|
self._config = options;
|
|
|
|
self._ipfsConnection = IpfsApi('localhost', '5001');
|
2017-12-28 00:10:05 +00:00
|
|
|
self._getUrl = "http://localhost:8080/ipfs/";
|
|
|
|
} else {
|
2018-05-25 07:13:57 +00:00
|
|
|
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
2018-03-02 16:02:52 +00:00
|
|
|
if (options.protocol) {
|
|
|
|
ipfsOptions.protocol = options.protocol;
|
|
|
|
}
|
2018-03-02 21:57:55 +00:00
|
|
|
if (options.port && options.port !== 'false') {
|
2018-03-02 16:02:52 +00:00
|
|
|
ipfsOptions.port = options.port;
|
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
self._ipfsConnection = IpfsApi(ipfsOptions);
|
2017-12-28 00:10:05 +00:00
|
|
|
self._getUrl = options.getUrl || "http://localhost:8080/ipfs/";
|
|
|
|
}
|
|
|
|
resolve(self);
|
|
|
|
} catch (err) {
|
2018-05-25 07:13:57 +00:00
|
|
|
console.error(err);
|
2018-05-25 07:13:57 +00:00
|
|
|
self.ipfsConnection = null;
|
2017-12-28 00:10:05 +00:00
|
|
|
reject(new Error('Failed to connect to IPFS'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.saveText = function (text) {
|
2017-12-28 00:10:05 +00:00
|
|
|
const self = this;
|
2018-05-01 13:38:13 +00:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2018-05-30 06:34:36 +00:00
|
|
|
if (!self._ipfsConnection) {
|
2017-12-28 00:10:05 +00:00
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
2017-12-28 00:10:05 +00:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.get = function (hash) {
|
2017-12-28 17:16:50 +00:00
|
|
|
const self = this;
|
2017-12-28 00:10:05 +00:00
|
|
|
// TODO: detect type, then convert if needed
|
|
|
|
//var ipfsHash = web3.toAscii(hash);
|
2018-05-01 13:38:13 +00:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2018-05-30 06:34:36 +00:00
|
|
|
if (!self._ipfsConnection) {
|
2017-12-28 00:10:05 +00:00
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
self._ipfsConnection.get(hash, function (err, files) {
|
2018-04-10 17:33:13 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(files[0].content.toString());
|
2017-12-28 00:10:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.uploadFile = function (inputSelector) {
|
2017-12-28 17:16:50 +00:00
|
|
|
const self = this;
|
2017-12-28 00:10:05 +00:00
|
|
|
var file = inputSelector[0].files[0];
|
|
|
|
|
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error('no file found');
|
|
|
|
}
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2018-05-30 06:34:36 +00:00
|
|
|
if (!self._ipfsConnection) {
|
2017-12-28 00:10:05 +00:00
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
var reader = new FileReader();
|
2018-05-01 13:38:13 +00:00
|
|
|
reader.onloadend = function () {
|
2017-12-28 00:10:05 +00:00
|
|
|
var fileContent = reader.result;
|
2018-05-30 06:34:36 +00:00
|
|
|
var buffer = self._ipfsConnection.Buffer.from(fileContent);
|
|
|
|
self._ipfsConnection.add(buffer, function (err, result) {
|
2017-12-28 00:10:05 +00:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.isAvailable = function () {
|
2018-04-30 05:56:43 +00:00
|
|
|
return new Promise((resolve) => {
|
2018-05-30 06:34:36 +00:00
|
|
|
if (!this._ipfsConnection) {
|
2018-05-01 13:38:13 +00:00
|
|
|
return resolve(false);
|
|
|
|
}
|
2018-05-30 06:34:36 +00:00
|
|
|
this._ipfsConnection.id()
|
2018-05-01 13:38:13 +00:00
|
|
|
.then((id) => {
|
|
|
|
resolve(Boolean(id));
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
resolve(false);
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkIPFS.getUrl = function (hash) {
|
2018-03-02 21:57:55 +00:00
|
|
|
return (this._getUrl || "http://localhost:8080/ipfs/") + hash;
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
|