2018-06-01 13:12:17 +10:00
const _ = require ( 'underscore' ) ;
2018-05-28 22:59:18 +10:00
2018-07-07 19:29:04 +03:00
const IpfsModule = require ( '../ipfs' ) ;
const SwarmModule = require ( '../swarm' ) ;
2018-05-28 22:59:18 +10:00
class Storage {
2018-06-01 13:12:17 +10:00
constructor ( embark , options ) {
2018-07-07 18:11:45 +03:00
const self = this ;
2018-07-07 21:23:51 +03:00
this . embark = embark ;
this . storageConfig = embark . config . storageConfig ;
2018-05-30 16:34:36 +10:00
2018-07-07 21:23:51 +03:00
if ( ! this . storageConfig . enabled ) return ;
2018-06-26 14:18:55 +10:00
2018-07-07 19:49:18 +03:00
this . addSetProviders ( ) ;
2018-05-30 16:34:36 +10:00
2018-07-07 19:29:04 +03:00
new IpfsModule ( embark , options ) ; /*eslint no-new: "off"*/
new SwarmModule ( embark , options ) ; /*eslint no-new: "off"*/
2018-07-07 18:11:45 +03:00
embark . events . setCommandHandler ( 'storage:upload' , ( cb ) => {
let platform = options . storageConfig . upload . provider ;
if ( [ 'swarm' , 'ipfs' ] . indexOf ( platform ) === - 1 ) {
return cb ( { message : _ _ ( 'platform "{{platform}}" is specified as the upload provider, however no plugins have registered an upload command for "{{platform}}".' , { platform : platform } ) } ) ;
}
} ) ;
2018-06-01 13:12:17 +10:00
}
2018-05-30 16:34:36 +10:00
2018-06-01 13:12:17 +10:00
/ * *
* Adds the code to call setProviders in embarkjs after embark is ready
*
* @ returns { void }
* /
addSetProviders ( ) {
2018-07-07 21:56:37 +03:00
const self = this ;
2018-07-07 21:14:55 +03:00
// filter list of dapp connections based on available_providers set in config
2018-07-07 21:23:51 +03:00
let hasSwarm = _ . contains ( this . storageConfig . available _providers , 'swarm' ) ; // don't need to eval this in every loop iteration
2018-07-07 21:14:55 +03:00
// contains valid dapp storage providers
2018-07-07 21:23:51 +03:00
this . _validDappProviders = _ . filter ( this . storageConfig . dappConnection , ( conn ) => {
return _ . contains ( self . storageConfig . available _providers , conn . provider ) || ( conn === '$BZZ' && hasSwarm ) ;
2018-07-07 21:14:55 +03:00
} ) ;
2018-07-07 18:29:45 +03:00
let code = ` \n EmbarkJS.Storage.setProviders( ${ JSON . stringify ( this . _validDappProviders ) } ); ` ;
2018-06-01 13:12:17 +10:00
let shouldInit = ( storageConfig ) => {
return ( this . _validDappProviders !== undefined && this . _validDappProviders . length > 0 && storageConfig . enabled === true ) ;
} ;
2018-06-01 12:53:23 -04:00
2018-07-07 21:23:51 +03:00
this . embark . addProviderInit ( 'storage' , code , shouldInit ) ;
2018-06-01 12:53:23 -04:00
}
2018-05-28 22:59:18 +10:00
}
module . exports = Storage ;