From d275bdf4d36f90ff61f1e6a714c9ef64d210596f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 22 Oct 2015 16:57:56 +0200 Subject: form to upload file --- app/config/config.yml | 2 + src/Wallabag/CoreBundle/Command/ImportCommand.php | 3 +- .../CoreBundle/Controller/ImportController.php | 56 +++++++++++++++------- .../DependencyInjection/Configuration.php | 13 +++++ .../DependencyInjection/WallabagCoreExtension.php | 1 + .../CoreBundle/Form/Type/UploadImportType.php | 29 +++++++++++ .../views/themes/material/Import/index.html.twig | 23 +++++++++ 7 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/UploadImportType.php create mode 100644 src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig diff --git a/app/config/config.yml b/app/config/config.yml index 8403a458..421b2db5 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -30,6 +30,8 @@ wallabag_core: en: 'English' fr: 'Français' de: 'Deutsch' + import: + allow_mimetypes: ['application/octet-stream', 'application/json', 'text/plain'] # Twig Configuration twig: diff --git a/src/Wallabag/CoreBundle/Command/ImportCommand.php b/src/Wallabag/CoreBundle/Command/ImportCommand.php index 16c84229..6be6f5e1 100644 --- a/src/Wallabag/CoreBundle/Command/ImportCommand.php +++ b/src/Wallabag/CoreBundle/Command/ImportCommand.php @@ -51,7 +51,7 @@ class ImportCommand extends ContainerAwareCommand $batchSize = 20; $i = 1; - $user = $em->getRepository('WallabagCoreBundle:User') + $user = $em->getRepository('WallabagUserBundle:User') ->findOneById($input->getArgument('userId')); if (!is_object($user)) { @@ -116,6 +116,7 @@ class ImportCommand extends ContainerAwareCommand protected function get(InputInterface $input, OutputInterface $output) { $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json'; + $data = $this->convert($filename); return $data; diff --git a/src/Wallabag/CoreBundle/Controller/ImportController.php b/src/Wallabag/CoreBundle/Controller/ImportController.php index 53211eec..33087710 100644 --- a/src/Wallabag/CoreBundle/Controller/ImportController.php +++ b/src/Wallabag/CoreBundle/Controller/ImportController.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\HttpFoundation\Request; use Wallabag\CoreBundle\Command\ImportCommand; +use Wallabag\CoreBundle\Form\Type\UploadImportType; class ImportController extends Controller { @@ -18,23 +19,46 @@ class ImportController extends Controller */ public function importAction(Request $request) { - $command = new ImportCommand(); - $command->setContainer($this->container); - $input = new ArrayInput(array('userId' => $this->getUser()->getId())); - $return = $command->run($input, new NullOutput()); - - if ($return == 0) { - $this->get('session')->getFlashBag()->add( - 'notice', - 'Import successful' - ); - } else { - $this->get('session')->getFlashBag()->add( - 'warning', - 'Import failed' - ); + $importForm = $this->createForm(new UploadImportType()); + $importForm->handleRequest($request); + $user = $this->getUser(); + $importConfig = $this->container->getParameter('wallabag_core.import'); + + if ($importForm->isValid()) { + $file = $importForm->get('file')->getData(); + $name = $user->getId().'.json'; + $dir = __DIR__.'/../../../../web/uploads/import'; + + if (in_array($file->getMimeType(), $importConfig['allow_mimetypes']) && $file->move($dir, $name)) { + $command = new ImportCommand(); + $command->setContainer($this->container); + $input = new ArrayInput(array('userId' => $user->getId())); + $return = $command->run($input, new NullOutput()); + + if ($return == 0) { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Import successful' + ); + } else { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Import failed' + ); + } + + return $this->redirect('/'); + } else { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Error while processing import. Please verify your import file.' + ); + } } - return $this->redirect('/'); + return $this->render('WallabagCoreBundle:Import:index.html.twig', array( + 'form' => array( + 'import' => $importForm->createView(), ), + )); } } diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php index 32acd1f1..fb1941b8 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php @@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; class Configuration implements ConfigurationInterface { @@ -17,9 +18,21 @@ class Configuration implements ConfigurationInterface ->arrayNode('languages') ->prototype('scalar')->end() ->end() + ->arrayNode('import') + ->append($this->getAllowMimetypes()) + ->end() ->end() ; return $treeBuilder; } + + private function getAllowMimetypes() + { + $node = new ArrayNodeDefinition('allow_mimetypes'); + + $node->prototype('scalar')->end(); + + return $node; + } } diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php index 330cc957..e2a3ad55 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php @@ -14,6 +14,7 @@ class WallabagCoreExtension extends Extension $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $container->setParameter('wallabag_core.languages', $config['languages']); + $container->setParameter('wallabag_core.import', $config['import']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php b/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php new file mode 100644 index 00000000..b9a9c465 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php @@ -0,0 +1,29 @@ +add('file', 'file') + ->add('save', 'submit') + ; + } + + public function getDefaultOptions(array $options) + { + return array( + 'csrf_protection' => false, + ); + } + + public function getName() + { + return 'upload_import_file'; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig new file mode 100644 index 00000000..2c8c158d --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig @@ -0,0 +1,23 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{% trans %}import{% endtrans %}{% endblock %} + +{% block content %} + +
+
+ {{ form_errors(form.import) }} +
+
+ {{ form_errors(form.import.file) }} + {{ form_widget(form.import.file) }} + +
+
+ + +
+
+{% endblock %} -- cgit v1.2.3