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) {
|
|
|
|
self.ipfsConnection = IpfsApi('localhost', '5001');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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);
|
2017-12-28 00:10:05 +00:00
|
|
|
self.ipfsConnection = null;
|
|
|
|
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) {
|
2017-12-28 17:16:50 +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-01 13:38:13 +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) {
|
2017-12-28 00:10:05 +00:00
|
|
|
if (!self.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
2018-04-10 17:33:13 +00:00
|
|
|
self.ipfsConnection.get(hash, function (err, files) {
|
|
|
|
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) {
|
2017-12-28 00:10:05 +00:00
|
|
|
if (!self.ipfsConnection) {
|
|
|
|
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;
|
|
|
|
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
2018-05-01 13:38:13 +00:00
|
|
|
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-01 13:38:13 +00:00
|
|
|
if (!this.ipfsConnection) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
2018-04-30 05:56:43 +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
|
|
|
|