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*/
|
/*global IpfsApi*/
|
||||||
|
|
||||||
let __embarkIPFS = {};
|
const __embarkIPFS = {};
|
||||||
|
|
||||||
const NoConnectionError = 'No IPFS connection. Please ensure to call Embark.Storage.setProvider()';
|
const NoConnectionError = 'No IPFS connection. Please ensure to call Embark.Storage.setProvider()';
|
||||||
|
|
||||||
__embarkIPFS.setProvider = function (options) {
|
__embarkIPFS.setProvider = function (options) {
|
||||||
var self = this;
|
const self = this;
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
try {
|
try {
|
||||||
if (!options) {
|
if (!options) {
|
||||||
|
@ -13,7 +13,7 @@ __embarkIPFS.setProvider = function (options) {
|
||||||
self._ipfsConnection = IpfsApi('localhost', '5001');
|
self._ipfsConnection = IpfsApi('localhost', '5001');
|
||||||
self._getUrl = "http://localhost:8080/ipfs/";
|
self._getUrl = "http://localhost:8080/ipfs/";
|
||||||
} else {
|
} else {
|
||||||
var ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
const ipfsOptions = {host: options.host || options.server, protocol: 'http'};
|
||||||
if (options.protocol) {
|
if (options.protocol) {
|
||||||
ipfsOptions.protocol = options.protocol;
|
ipfsOptions.protocol = options.protocol;
|
||||||
}
|
}
|
||||||
|
@ -50,10 +50,9 @@ __embarkIPFS.isAvailable = function () {
|
||||||
|
|
||||||
__embarkIPFS.saveText = function (text) {
|
__embarkIPFS.saveText = function (text) {
|
||||||
const self = this;
|
const self = this;
|
||||||
var promise = new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (!self._ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error(NoConnectionError);
|
return reject(new Error(NoConnectionError));
|
||||||
return reject(connectionError);
|
|
||||||
}
|
}
|
||||||
self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -63,15 +62,13 @@ __embarkIPFS.saveText = function (text) {
|
||||||
resolve(result[0].path);
|
resolve(result[0].path);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkIPFS.get = function (hash) {
|
__embarkIPFS.get = function (hash) {
|
||||||
const self = this;
|
const self = this;
|
||||||
// TODO: detect type, then convert if needed
|
// TODO: detect type, then convert if needed
|
||||||
//var ipfsHash = web3.toAscii(hash);
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
var promise = new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (!self._ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error(NoConnectionError);
|
var connectionError = new Error(NoConnectionError);
|
||||||
return reject(connectionError);
|
return reject(connectionError);
|
||||||
|
@ -83,27 +80,23 @@ __embarkIPFS.get = function (hash) {
|
||||||
resolve(files[0].content.toString());
|
resolve(files[0].content.toString());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkIPFS.uploadFile = function (inputSelector) {
|
__embarkIPFS.uploadFile = function (inputSelector) {
|
||||||
const self = this;
|
const self = this;
|
||||||
var file = inputSelector[0].files[0];
|
const file = inputSelector[0].files[0];
|
||||||
|
|
||||||
if (file === undefined) {
|
if (file === undefined) {
|
||||||
throw new Error('no file found');
|
throw new Error('no file found');
|
||||||
}
|
}
|
||||||
|
|
||||||
var promise = new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (!self._ipfsConnection) {
|
if (!self._ipfsConnection) {
|
||||||
var connectionError = new Error(NoConnectionError);
|
return reject(new Error(NoConnectionError));
|
||||||
return reject(connectionError);
|
|
||||||
}
|
}
|
||||||
var reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onloadend = function () {
|
reader.onloadend = function () {
|
||||||
var fileContent = reader.result;
|
const buffer = self._ipfsConnection.Buffer.from(reader.result);
|
||||||
var buffer = self._ipfsConnection.Buffer.from(fileContent);
|
|
||||||
self._ipfsConnection.add(buffer, function (err, result) {
|
self._ipfsConnection.add(buffer, function (err, result) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
|
@ -114,8 +107,6 @@ __embarkIPFS.uploadFile = function (inputSelector) {
|
||||||
};
|
};
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkIPFS.getUrl = function (hash) {
|
__embarkIPFS.getUrl = function (hash) {
|
||||||
|
@ -125,8 +116,7 @@ __embarkIPFS.getUrl = function (hash) {
|
||||||
__embarkIPFS.resolve = function (name, callback) {
|
__embarkIPFS.resolve = function (name, callback) {
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
if (!this._ipfsConnection) {
|
if (!this._ipfsConnection) {
|
||||||
var connectionError = new Error(NoConnectionError);
|
return callback(new Error(NoConnectionError));
|
||||||
return callback(connectionError);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._ipfsConnection.name.resolve(name)
|
this._ipfsConnection.name.resolve(name)
|
||||||
|
@ -144,6 +134,10 @@ __embarkIPFS.register = function(addr, callback) {
|
||||||
return new Error(NoConnectionError);
|
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)
|
this._ipfsConnection.name.publish("/ipfs/" + addr)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
callback(null, res.Name);
|
callback(null, res.Name);
|
||||||
|
|
Loading…
Reference in New Issue