2018-06-01 03:12:17 +00:00
const _ = require ( 'underscore' ) ;
2018-05-28 12:59:18 +00:00
2018-07-07 16:29:04 +00:00
const IpfsModule = require ( '../ipfs' ) ;
const SwarmModule = require ( '../swarm' ) ;
2018-05-28 12:59:18 +00:00
class Storage {
2018-06-01 03:12:17 +00:00
constructor ( embark , options ) {
2018-07-07 18:23:51 +00:00
this . embark = embark ;
this . storageConfig = embark . config . storageConfig ;
2018-05-30 06:34:36 +00:00
2018-07-07 18:23:51 +00:00
if ( ! this . storageConfig . enabled ) return ;
2018-06-26 04:18:55 +00:00
2018-07-07 16:49:18 +00:00
this . addSetProviders ( ) ;
2018-05-30 06:34:36 +00:00
2018-07-07 16:29:04 +00:00
new IpfsModule ( embark , options ) ; /*eslint no-new: "off"*/
new SwarmModule ( embark , options ) ; /*eslint no-new: "off"*/
2018-07-07 15:11:45 +00: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-07-07 21:02:46 +00:00
embark . events . request ( "storage:upload:" + platform , cb ) ;
2018-07-07 15:11:45 +00:00
} ) ;
2018-06-01 03:12:17 +00:00
}
2018-05-30 06:34:36 +00:00
2018-06-01 03:12:17 +00:00
addSetProviders ( ) {
2018-07-07 18:56:37 +00:00
const self = this ;
2018-07-07 18:14:55 +00:00
2018-07-07 21:56:15 +00:00
let code = ` \n EmbarkJS.Storage.setProviders( ${ JSON . stringify ( this . storageConfig . dappConnection ) } ); ` ;
2018-06-01 03:12:17 +00:00
let shouldInit = ( storageConfig ) => {
2018-07-07 21:56:15 +00:00
return storageConfig . enabled ;
2018-06-01 03:12:17 +00:00
} ;
2018-06-01 16:53:23 +00:00
2018-07-07 18:23:51 +00:00
this . embark . addProviderInit ( 'storage' , code , shouldInit ) ;
2018-06-01 16:53:23 +00:00
}
2018-05-28 12:59:18 +00:00
}
module . exports = Storage ;