mirror of https://github.com/embarklabs/embark.git
76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
/*global __embarkIPFS*/
|
|
|
|
__embarkIPFS.saveText = function (text) {
|
|
const self = this;
|
|
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.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
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) {
|
|
if (!self._ipfsConnection) {
|
|
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
|
reject(connectionError);
|
|
}
|
|
self._ipfsConnection.get(hash, function (err, files) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
resolve(files[0].content.toString());
|
|
});
|
|
});
|
|
|
|
return promise;
|
|
};
|
|
|
|
__embarkIPFS.uploadFile = function (inputSelector) {
|
|
const self = this;
|
|
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) {
|
|
return (this._getUrl || "http://localhost:8080/ipfs/") + hash;
|
|
};
|