2017-12-28 17:16:50 +00:00
|
|
|
import IpfsApi from 'ipfs-api';
|
|
|
|
|
2017-12-28 00:10:05 +00:00
|
|
|
let __embarkIPFS = {};
|
|
|
|
|
|
|
|
__embarkIPFS.setProvider = function(options) {
|
|
|
|
var self = this;
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
try {
|
|
|
|
if (options === undefined) {
|
|
|
|
self.ipfsConnection = IpfsApi('localhost', '5001');
|
|
|
|
self._getUrl = "http://localhost:8080/ipfs/";
|
|
|
|
} else {
|
2018-03-02 16:02:52 +00:00
|
|
|
var ipfsOptions = {host: options.server, protocol: 'http'};
|
|
|
|
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) {
|
|
|
|
console.log(err);
|
|
|
|
self.ipfsConnection = null;
|
|
|
|
reject(new Error('Failed to connect to IPFS'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
__embarkIPFS.saveText = function(text) {
|
|
|
|
const self = this;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
__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);
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (!self.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
self.ipfsConnection.object.get(hash).then(function(node) {
|
|
|
|
resolve(node.data.toString());
|
|
|
|
}).catch(function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
__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');
|
|
|
|
}
|
|
|
|
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
if (!self.ipfsConnection) {
|
|
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onloadend = function() {
|
|
|
|
var fileContent = reader.result;
|
|
|
|
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
|
|
|
self.ipfsConnection.add(buffer, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result[0].path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
__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
|
|
|
};
|
|
|
|
|