2018-05-30 06:34:36 +00:00
|
|
|
const UploadIPFS = require('./upload.js');
|
|
|
|
const utils = require('../../utils/utils.js');
|
|
|
|
const fs = require('../../core/fs.js');
|
2018-05-30 06:34:36 +00:00
|
|
|
const IpfsApi = require('ipfs-api');
|
2018-07-20 15:55:17 +00:00
|
|
|
// TODO: not great, breaks module isolation
|
|
|
|
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
|
2017-12-27 00:55:42 +00:00
|
|
|
|
|
|
|
class IPFS {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
2018-07-07 17:47:40 +00:00
|
|
|
const self = this;
|
2017-12-27 00:55:42 +00:00
|
|
|
this.logger = embark.logger;
|
2017-12-27 01:32:51 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
2018-06-01 15:58:11 +00:00
|
|
|
this.storageConfig = embark.config.storageConfig;
|
2018-07-27 09:51:17 +00:00
|
|
|
this.namesystemConfig = embark.config.namesystemConfig;
|
2017-12-27 01:32:51 +00:00
|
|
|
this.embark = embark;
|
2018-07-07 16:29:04 +00:00
|
|
|
|
2018-07-07 21:46:15 +00:00
|
|
|
this.webServerConfig = embark.config.webServerConfig;
|
|
|
|
this.blockchainConfig = embark.config.blockchainConfig;
|
2018-07-07 16:29:04 +00:00
|
|
|
|
2018-07-30 16:06:20 +00:00
|
|
|
if (this.isIpfsStorageEnabledInTheConfig()) {
|
2018-08-22 09:11:57 +00:00
|
|
|
this.downloadIpfsApi();
|
2018-07-27 09:51:17 +00:00
|
|
|
this.setServiceCheck();
|
|
|
|
this.addStorageProviderToEmbarkJS();
|
|
|
|
this.addObjectToConsole();
|
|
|
|
this.registerUploadCommand();
|
2018-07-07 17:47:40 +00:00
|
|
|
|
2018-07-27 09:51:17 +00:00
|
|
|
this._checkService((err) => {
|
|
|
|
if (!err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.logger.info("IPFS node not found, attempting to start own node");
|
2018-09-15 18:48:53 +00:00
|
|
|
this.listenToCommands();
|
|
|
|
this.registerConsoleCommands();
|
2018-07-27 09:51:17 +00:00
|
|
|
self.startProcess(() => {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadIpfsApi() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
self.events.request("version:get:ipfs-api", function(ipfsApiVersion) {
|
|
|
|
let currentIpfsApiVersion = require('../../../package.json').dependencies["ipfs-api"];
|
|
|
|
if (ipfsApiVersion !== currentIpfsApiVersion) {
|
|
|
|
self.events.request("version:getPackageLocation", "ipfs-api", ipfsApiVersion, function(err, location) {
|
|
|
|
self.embark.registerImportFile("ipfs-api", fs.dappPath(location));
|
|
|
|
});
|
2018-07-07 17:47:40 +00:00
|
|
|
}
|
|
|
|
});
|
2017-12-27 01:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setServiceCheck() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
self.events.on('check:backOnline:IPFS', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('IPFS node detected') + '..');
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.events.on('check:wentOffline:IPFS', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('IPFS node is offline') + '..');
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 15:58:11 +00:00
|
|
|
self.events.request("services:register", 'IPFS', function (cb) {
|
2018-07-07 17:47:40 +00:00
|
|
|
self._checkService((err, body) => {
|
2017-12-31 02:44:59 +00:00
|
|
|
if (err) {
|
2018-07-07 19:00:42 +00:00
|
|
|
self.logger.trace("IPFS unavailable");
|
2017-12-27 01:32:51 +00:00
|
|
|
return cb({name: "IPFS ", status: 'off'});
|
|
|
|
}
|
2017-12-31 02:44:59 +00:00
|
|
|
if (body.Version) {
|
2018-07-07 19:00:42 +00:00
|
|
|
self.logger.trace("IPFS available");
|
2017-12-31 02:44:59 +00:00
|
|
|
return cb({name: ("IPFS " + body.Version), status: 'on'});
|
|
|
|
}
|
2018-07-07 19:00:42 +00:00
|
|
|
self.logger.trace("IPFS available");
|
2017-12-31 02:44:59 +00:00
|
|
|
return cb({name: "IPFS ", status: 'on'});
|
2018-07-07 17:47:40 +00:00
|
|
|
});
|
2017-12-27 01:32:51 +00:00
|
|
|
});
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 18:56:37 +00:00
|
|
|
_getNodeUrl() {
|
|
|
|
if (this.storageConfig.upload.provider === 'ipfs') {
|
2018-07-11 07:24:03 +00:00
|
|
|
return utils.buildUrlFromConfig(this.storageConfig.upload) + '/api/v0/version';
|
2018-07-07 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let connection of this.storageConfig.dappConnection) {
|
|
|
|
if (connection.provider === 'ipfs') {
|
2018-07-11 07:24:03 +00:00
|
|
|
return utils.buildUrlFromConfig(connection) + '/api/v0/version';
|
2018-07-07 18:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 17:47:40 +00:00
|
|
|
_checkService(cb) {
|
2018-07-07 18:56:37 +00:00
|
|
|
let url = this._getNodeUrl();
|
2018-07-08 18:24:19 +00:00
|
|
|
utils.getJson(url, cb);
|
2018-07-07 17:47:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 09:51:17 +00:00
|
|
|
addStorageProviderToEmbarkJS() {
|
2017-12-28 17:16:50 +00:00
|
|
|
let code = "";
|
2018-08-22 09:11:57 +00:00
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
2017-12-28 17:16:50 +00:00
|
|
|
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);";
|
2018-07-27 09:51:17 +00:00
|
|
|
|
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:59:18 +00:00
|
|
|
addObjectToConsole() {
|
2018-05-30 06:34:36 +00:00
|
|
|
let ipfs = IpfsApi(this.host, this.port);
|
2018-05-30 21:22:12 +00:00
|
|
|
this.events.emit("runcode:register", "ipfs", ipfs);
|
2018-05-18 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 16:29:04 +00:00
|
|
|
startProcess(callback) {
|
|
|
|
let self = this;
|
|
|
|
const storageProcessesLauncher = new StorageProcessesLauncher({
|
|
|
|
logger: self.logger,
|
|
|
|
events: self.events,
|
|
|
|
storageConfig: self.storageConfig,
|
|
|
|
webServerConfig: self.webServerConfig,
|
2018-10-18 13:13:03 +00:00
|
|
|
blockchainConfig: self.blockchainConfig,
|
|
|
|
corsParts: self.embark.config.corsParts
|
2018-07-07 16:29:04 +00:00
|
|
|
});
|
|
|
|
self.logger.trace(`Storage module: Launching ipfs process...`);
|
2018-07-08 17:43:41 +00:00
|
|
|
return storageProcessesLauncher.launchProcess('ipfs', callback);
|
2018-07-07 16:29:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 17:40:06 +00:00
|
|
|
registerUploadCommand() {
|
|
|
|
const self = this;
|
|
|
|
this.embark.registerUploadCommand('ipfs', (cb) => {
|
|
|
|
let upload_ipfs = new UploadIPFS({
|
|
|
|
buildDir: self.buildDir || 'dist/',
|
|
|
|
storageConfig: self.storageConfig,
|
2018-09-27 05:35:37 +00:00
|
|
|
configIpfsBin: self.storageConfig.ipfs_bin || "ipfs",
|
|
|
|
env: this.embark.env
|
2018-07-08 17:40:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
upload_ipfs.deploy(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-15 18:48:53 +00:00
|
|
|
listenToCommands() {
|
|
|
|
this.events.setCommandHandler('logs:ipfs:turnOn', (cb) => {
|
|
|
|
this.events.emit('logs:storage:enable');
|
|
|
|
return cb(null, 'Enabling IPFS logs');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler('logs:ipfs:turnOff', (cb) => {
|
|
|
|
this.events.emit('logs:storage:disable');
|
|
|
|
return cb(null, 'Disabling IPFS logs');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
registerConsoleCommands() {
|
|
|
|
const self = this;
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => cmd === 'log ipfs on',
|
|
|
|
process: (cb) => self.events.request('logs:ipfs:turnOn', cb)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => cmd === 'log ipfs off',
|
|
|
|
process: (cb) => self.events.request('logs:ipfs:turnOff', cb)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-30 16:06:20 +00:00
|
|
|
isIpfsStorageEnabledInTheConfig() {
|
2018-07-08 22:21:27 +00:00
|
|
|
let {enabled, available_providers, dappConnection} = this.storageConfig;
|
2018-07-08 18:14:44 +00:00
|
|
|
return enabled && (available_providers.indexOf('ipfs') > 0 || dappConnection.find(c => c.provider === 'ipfs'));
|
|
|
|
}
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = IPFS;
|