Skip to content

Commit

Permalink
Merge pull request #111 from helios-ag/flysystem
Browse files Browse the repository at this point in the history
flysystem driver support
  • Loading branch information
helios-ag committed Feb 14, 2015
2 parents ed1bff8 + 3532846 commit 18d20e8
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 157 deletions.
100 changes: 95 additions & 5 deletions Configuration/ElFinderConfigurationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@

use FM\ElfinderBundle\Model\ElFinderConfigurationProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Adapter\Ftp;
use League\Flysystem\Dropbox\DropboxAdapter;
use League\Flysystem\Sftp\SftpAdapter;
use League\Flysystem\AwsS3v2\AwsS3Adapter as AwsS3v2;
use League\Flysystem\AwsS3v3\AwsS3Adapter as AwsS3v3;

use League\Flysystem\Copy\CopyAdapter;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use Aws\S3\S3Client;
use Dropbox\Client;
use Barracuda\Copy\API;

/**
* Class ElFinderConfigurationReader
Expand Down Expand Up @@ -65,17 +77,24 @@ public function getConfiguration($instance)
$path = $parameter['path'];
$homeFolder = $request->attributes->get('homeFolder');
if ($homeFolder !== '') {
$homeFolder .= '/';
$homeFolder = '/'.$homeFolder.'/';
}
if($parameter['flysystem']['enabled']) {
$adapter = $parameter['flysystem']['type']; // ftp ex.
$opt = $parameter['flysystem']['options'];
$filesystem = $this->configureFlysystem($opt, $adapter);
}

$driver = $this->container->has($parameter['driver']) ? $this->container->get($parameter['driver']) : null;

$driverOptions = array(
'driver' => $parameter['driver'],
'service' => $driver,
'glideURL' => $parameter['glide_url'],
'glideKey' => $parameter['glide_key'],
'disabled' => $parameter['disabled_commands'],
'plugin' => $parameter['plugin'],
'path' => $path . '/' . $homeFolder,
'alias' => $parameter['alias'],
'path' => $path . $homeFolder, //removed slash for Flysystem compatibility
'URL' => isset($parameter['url']) && $parameter['url']
? strpos($parameter['url'], 'http') === 0
? $parameter['url']
Expand All @@ -85,15 +104,86 @@ public function getConfiguration($instance)
'uploadDeny' => $parameter['upload_deny'],
'uploadMaxSize' => $parameter['upload_max_size']
);
if(!$parameter['showhidden']) {
if(!$parameter['show_hidden']) {
$driverOptions['accessControl'] = array($this, 'access');
};

if($parameter['driver'] == 'Flysystem') {
$driverOptions['filesystem'] = $filesystem;
}
$options['roots'][] = array_merge($driverOptions, $this->configureDriver($parameter));
}

return $options;
}

/**
* @param $opt
* @param $adapter
* @return Filesystem
*/
private function configureFlysystem($opt, $adapter)
{
switch ($adapter) {
case 'local':
$filesystem = new Filesystem(new Local($opt['local']['path']));
break;
case 'ftp':
$settings = array(
'host' => $opt['ftp']['host'],
'username' => $opt['ftp']['username'],
'password' => $opt['ftp']['password'],

/** optional config settings */
'port' => $opt['ftp']['port'],
'root' => $opt['ftp']['root'],
'passive' => $opt['ftp']['passive'],
'ssl' => $opt['ftp']['ssl'],
'timeout' => $opt['ftp']['timeout']
);
$filesystem = (!$opt['ftp']['sftp']) ? new Filesystem(new Ftp($settings)): new Filesystem(new SftpAdapter($settings));
break;
case 'aws_s3_v2':
$client = S3Client::factory([
'key' => $opt['aws_s3_v2']['key'],
'secret' => $opt['aws_s3_v2']['secret'],
'region' => $opt['aws_s3_v2']['region']
]);
$filesystem = new Filesystem(new AwsS3v2($client, $opt['aws_s3_v2']['bucket_name'], $opt['aws_s3_v2']['optional_prefix']));
break;
case 'aws_s3_v3':
$client = S3Client::factory([
'key' => $opt['aws_s3_v3']['key'],
'secret' => $opt['aws_s3_v3']['secret'],
'region' => $opt['aws_s3_v3']['region'],
'version' => $opt['aws_s3_v3']['version']
]);
$filesystem = new Filesystem(new AwsS3v3($client, $opt['aws_s3_v3']['bucket_name'], $opt['aws_s3_v3']['optional_prefix']));
break;
case 'copy_com':
$client = new API(
$opt['copy_com']['consumer_key'],
$opt['copy_com']['consumer_secret'],
$opt['copy_com']['access_token'],
$opt['copy_com']['token_secret']
);
$filesystem = new Filesystem(new CopyAdapter($client, $opt['copy_com']['optional_prefix']));
break;
case 'gridfs':
$mongoClient = new MongoClient();
$gridFs = $mongoClient->selectDB($opt['gridfs']['db_name'])->getGridFS();
$filesystem = new Filesystem(new GridFSAdapter($gridFs));
break;
case 'zip':
$filesystem = new Filesystem(new ZipArchiveAdapter($opt['zip']['path']));
break;
case 'dropbox':
$filesystem = new Filesystem(new DropboxAdapter(new Client($opt['dropbox']['token'],$opt['dropbox']['app'])));
break;
}
return $filesystem;
}

/**
* @param array $parameter
* @return array
Expand Down
21 changes: 11 additions & 10 deletions Controller/ElFinderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public function showAction(Request $request, $instance, $homeFolder)
}

/**
* @param array $parameters
* @param array $parameters
* @param string $instance
* @param string $homeFolder
* @param null $formTypeId
* @return array
* @throws Exception
*/
private function selectEditor($parameters, $instance, $homeFolder, $formTypeId = null)
{
Expand Down Expand Up @@ -99,23 +100,23 @@ private function selectEditor($parameters, $instance, $homeFolder, $formTypeId =
case 'form':
$result['template'] = 'FMElfinderBundle:Elfinder:elfinder_type.html.twig';
$result['params'] = array(
'locale' => $locale,
'fullscreen' => $fullscreen,
'locale' => $locale,
'fullscreen' => $fullscreen,
'includeAssets' => $includeAssets,
'instance' => $instance,
'homeFolder' => $homeFolder,
'id' => $formTypeId,
'instance' => $instance,
'homeFolder' => $homeFolder,
'id' => $formTypeId,
'relative_path' => $relativePath
);
return $result;
default:
$result['template'] = 'FMElfinderBundle:Elfinder:simple.html.twig';
$result['params'] = array(
'locale' => $locale,
'fullscreen' => $fullscreen,
'locale' => $locale,
'fullscreen' => $fullscreen,
'includeAssets' => $includeAssets,
'instance' => $instance,
'homeFolder' => $homeFolder
'instance' => $instance,
'homeFolder' => $homeFolder
);
return $result;
}
Expand Down
95 changes: 90 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function getConfigTreeBuilder()
->booleanNode('fullscreen')->defaultTrue()->end()
->booleanNode('include_assets')->defaultTrue()->end()
->scalarNode('tinymce_popup_path')->defaultValue('')->end()
->booleanNode('enableUserIntegration')->defaultFalse()->end()
->booleanNode('relative_path')->defaultTrue()->end()
->arrayNode('connector')
->addDefaultsIfNotSet()
Expand All @@ -56,17 +55,25 @@ public function getConfigTreeBuilder()
->scalarNode('driver')
->isRequired()
->defaultValue('LocalFileSystem')->end()
->arrayNode('flysystem')
->canBeEnabled()
->children()
->scalarNode('type')->defaultValue('')->end()
->append($this->createFlysystemNode())
->end()
->end()
->scalarNode('glide_url')->defaultValue('')->end()
->scalarNode('glide_key')->defaultValue('')->end()
->arrayNode('disabled_commands')
->prototype('scalar')->end()
->defaultValue(array())
->end()
->scalarNode('path')->defaultValue('')->end()
->scalarNode('url')->end()
->append($this->createPluginsNode())
->booleanNode('showhidden')->defaultFalse()->end()
->booleanNode('show_hidden')->defaultFalse()->end()
->scalarNode('alias')->defaultValue('')->end()
->integerNode('treeDeep')->defaultValue(0)->end()
->scalarNode('accessControl')->defaultNull()->end()
->integerNode('tree_deep')->defaultValue(0)->end()
->arrayNode('upload_allow')
->prototype('scalar')->end()
->defaultValue(array('image'))
Expand Down Expand Up @@ -115,6 +122,84 @@ public function getConfigTreeBuilder()
return $treeBuilder;
}

/**
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The Flysystem node.
*/
private function createFlysystemNode()
{
return $this->createNode('options')
->children()
->arrayNode('local')
->canBeEnabled()
->children()
->scalarNode('path')->defaultvalue('')->end()
->end()
->end()
->arrayNode('ftp')
->canBeEnabled()
->children()
->booleanNode('sftp')->defaultFalse()->end()
->scalarNode('host')->defaultValue('')->end()
->scalarNode('username')->defaultValue('')->end()
->scalarNode('password')->defaultValue('')->end()
->integerNode('port')->defaultValue('')->end()
->booleanNode('passive')->defaultTrue()->end()
->booleanNode('ssl')->defaultTrue()->end()
->integerNode('timeout')->defaultValue(30)->end()
->end()
->end()
->arrayNode('aws_s3_v2')
->canBeEnabled()
->children()
->scalarNode('key')->defaultvalue('')->end()
->scalarNode('secret')->defaultvalue('')->end()
->scalarNode('region')->defaultvalue('')->end()
->scalarNode('bucket_name')->defaultvalue('')->end()
->scalarNode('optional_prefix')->defaultvalue('')->end()
->end()
->end()
->arrayNode('aws_s3_v3')
->canBeEnabled()
->children()
->scalarNode('key')->defaultvalue('')->end()
->scalarNode('secret')->defaultvalue('')->end()
->scalarNode('region')->defaultvalue('')->end()
->scalarNode('version')->defaultvalue('')->end()
->scalarNode('bucket_name')->defaultvalue('')->end()
->scalarNode('optional_prefix')->defaultvalue('')->end()
->end()
->end()
->arrayNode('copy_com')
->canBeEnabled()
->children()
->scalarNode('consumer_key')->defaultvalue('')->end()
->scalarNode('consumer_secret')->defaultvalue('')->end()
->scalarNode('access_token')->defaultvalue('')->end()
->scalarNode('token_secret')->defaultvalue('')->end()
->scalarNode('optional_prefix')->defaultvalue('')->end()
->end()
->end()
->arrayNode('gridfs')
->canBeEnabled()
->children()
->scalarNode('db_name')->defaultvalue('')->end()
->end()
->end()
->arrayNode('zip')
->canBeEnabled()
->children()
->scalarNode('path')->defaultvalue('')->end()
->end()
->end()
->arrayNode('dropbox')
->canBeEnabled()
->children()
->scalarNode('app')->defaultvalue('')->end()
->scalarNode('token')->defaultvalue('')->end()
->end()
->end()->end();
}

/**
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The plugins node.
*/
Expand All @@ -129,7 +214,7 @@ private function createPluginsNode()
}

/**
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The plugins node.
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition The bind node.
*/
private function createBindNode()
{
Expand Down
Loading

0 comments on commit 18d20e8

Please sign in to comment.