Merge pull request #810 from embark-framework/bug_fix/fix-log-ipns
Make sure the string is an IPFS hash and some linting
This commit is contained in:
commit
4e3e27df6c
|
@ -1,11 +1,11 @@
|
|||
/*global IpfsApi*/
|
||||
|
||||
let __embarkIPFS = {};
|
||||
const __embarkIPFS = {};
|
||||
|
||||
const NoConnectionError = 'No IPFS connection. Please ensure to call Embark.Storage.setProvider()';
|
||||
|
||||
__embarkIPFS.setProvider = function (options) {
|
||||
var self = this;
|
||||
const self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
try {
|
||||
if (!options) {
|
||||
|
@ -13,7 +13,7 @@ __embarkIPFS.setProvider = function (options) {
|
|||
self._ipfsConnection = IpfsApi('localhost', '5001');
|
||||
self._getUrl = "http://localhost:8080/ipfs/";
|
||||
} else {
|
||||
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
||||
const ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
||||
if (options.protocol) {
|
||||
ipfsOptions.protocol = options.protocol;
|
||||
}
|
||||
|
@ -50,10 +50,9 @@ __embarkIPFS.isAvailable = function () {
|
|||
|
||||
__embarkIPFS.saveText = function (text) {
|
||||
const self = this;
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!self._ipfsConnection) {
|
||||
var connectionError = new Error(NoConnectionError);
|
||||
return reject(connectionError);
|
||||
return reject(new Error(NoConnectionError));
|
||||
}
|
||||
self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
||||
if (err) {
|
||||
|
@ -63,15 +62,13 @@ __embarkIPFS.saveText = function (text) {
|
|||
resolve(result[0].path);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
__embarkIPFS.get = function (hash) {
|
||||
const self = this;
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!self._ipfsConnection) {
|
||||
var connectionError = new Error(NoConnectionError);
|
||||
return reject(connectionError);
|
||||
|
@ -83,27 +80,23 @@ __embarkIPFS.get = function (hash) {
|
|||
resolve(files[0].content.toString());
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
__embarkIPFS.uploadFile = function (inputSelector) {
|
||||
const self = this;
|
||||
var file = inputSelector[0].files[0];
|
||||
const file = inputSelector[0].files[0];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!self._ipfsConnection) {
|
||||
var connectionError = new Error(NoConnectionError);
|
||||
return reject(connectionError);
|
||||
return reject(new Error(NoConnectionError));
|
||||
}
|
||||
var reader = new FileReader();
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = function () {
|
||||
var fileContent = reader.result;
|
||||
var buffer = self._ipfsConnection.Buffer.from(fileContent);
|
||||
const buffer = self._ipfsConnection.Buffer.from(reader.result);
|
||||
self._ipfsConnection.add(buffer, function (err, result) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
|
@ -114,8 +107,6 @@ __embarkIPFS.uploadFile = function (inputSelector) {
|
|||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
__embarkIPFS.getUrl = function (hash) {
|
||||
|
@ -125,8 +116,7 @@ __embarkIPFS.getUrl = function (hash) {
|
|||
__embarkIPFS.resolve = function (name, callback) {
|
||||
callback = callback || function () {};
|
||||
if (!this._ipfsConnection) {
|
||||
var connectionError = new Error(NoConnectionError);
|
||||
return callback(connectionError);
|
||||
return callback(new Error(NoConnectionError));
|
||||
}
|
||||
|
||||
this._ipfsConnection.name.resolve(name)
|
||||
|
@ -144,6 +134,10 @@ __embarkIPFS.register = function(addr, callback) {
|
|||
return new Error(NoConnectionError);
|
||||
}
|
||||
|
||||
if (addr.length !== 46 || !addr.startsWith('Qm')) {
|
||||
return callback('String is not an IPFS hash');
|
||||
}
|
||||
|
||||
this._ipfsConnection.name.publish("/ipfs/" + addr)
|
||||
.then(res => {
|
||||
callback(null, res.Name);
|
||||
|
|
Loading…
Reference in New Issue