diff --git a/src/Kunstmaan/FormBundle/DependencyInjection/Configuration.php b/src/Kunstmaan/FormBundle/DependencyInjection/Configuration.php index e8ea4e2c66..298700ff03 100644 --- a/src/Kunstmaan/FormBundle/DependencyInjection/Configuration.php +++ b/src/Kunstmaan/FormBundle/DependencyInjection/Configuration.php @@ -2,6 +2,7 @@ namespace Kunstmaan\FormBundle\DependencyInjection; +use Kunstmaan\MediaBundle\Utils\SymfonyVersion; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -30,6 +31,10 @@ public function getConfigTreeBuilder() $rootNode ->children() ->booleanNode('deletable_formsubmissions')->defaultFalse()->end() + ->scalarNode('web_root') + ->defaultValue(SymfonyVersion::getRootWebPath()) + ->cannotBeEmpty() + ->end() ->end() ; diff --git a/src/Kunstmaan/FormBundle/DependencyInjection/KunstmaanFormExtension.php b/src/Kunstmaan/FormBundle/DependencyInjection/KunstmaanFormExtension.php index 68a1b91868..a073b22071 100644 --- a/src/Kunstmaan/FormBundle/DependencyInjection/KunstmaanFormExtension.php +++ b/src/Kunstmaan/FormBundle/DependencyInjection/KunstmaanFormExtension.php @@ -25,6 +25,7 @@ public function load(array $configs, ContainerBuilder $container) $config = $this->processConfiguration($configuration, $configs); $container->setParameter('kunstmaan_form.deletable_formsubmissions', $config['deletable_formsubmissions']); + $container->setParameter('kunstmaan_form.web_root', $config['web_root']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php b/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php index 2c1afdca73..76db8b3d2e 100644 --- a/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php +++ b/src/Kunstmaan/FormBundle/Entity/FormSubmissionFieldTypes/FileFormSubmissionField.php @@ -4,6 +4,7 @@ use Behat\Transliterator\Transliterator; use Doctrine\ORM\Mapping as ORM; +use Gaufrette\Filesystem; use Kunstmaan\FormBundle\Entity\FormSubmissionField; use Kunstmaan\FormBundle\Form\FileFormSubmissionType; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -79,10 +80,8 @@ public function isNull() /** * Move the file to the given uploadDir and save the filename - * - * @param string $uploadDir */ - public function upload($uploadDir, $webDir) + public function upload($webDir, Filesystem $fileSystem) { // the file property can be empty if the field is not required if (null === $this->file) { @@ -95,14 +94,15 @@ public function upload($uploadDir, $webDir) $uuid = uniqid(); $this->setUuid($uuid); - // move takes the target directory and then the target filename to move to - $this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName); + $url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName); + $content = \file_get_contents($this->file->getPathname()); + $fileSystem->write($url, $content); // set the path property to the filename where you'ved saved the file $this->fileName = $safeFileName; // set the url to the uuid directory inside the web dir - $this->setUrl(sprintf('%s%s/', $webDir, $uuid) . $safeFileName); + $this->setUrl($url); // clean up the file property as you won't need it anymore $this->file = null; @@ -118,9 +118,9 @@ public function upload($uploadDir, $webDir) */ public function onValidPost(Form $form, FormBuilderInterface $formBuilder, Request $request, ContainerInterface $container) { - $uploadDir = $container->getParameter('form_submission_rootdir'); $webDir = $container->getParameter('form_submission_webdir'); - $this->upload($uploadDir, $webDir); + $fileSystem = $container->get('kunstmaan_form.filesystem'); + $this->upload($webDir, $fileSystem); } /** diff --git a/src/Kunstmaan/FormBundle/Resources/config/services.yml b/src/Kunstmaan/FormBundle/Resources/config/services.yml index a5da381b99..c39466e9ab 100644 --- a/src/Kunstmaan/FormBundle/Resources/config/services.yml +++ b/src/Kunstmaan/FormBundle/Resources/config/services.yml @@ -30,3 +30,15 @@ services: arguments: ['@kunstmaan_form.form_mailer'] tags: - { name: 'kernel.event_listener', event: 'kunstmaan_form.add_submission', method: 'onSubmission' } + + kunstmaan_form.filesystem_adapter: + class: Gaufrette\Adapter\Local + arguments: + - '%kunstmaan_form.web_root%' + - true + + kunstmaan_form.filesystem: + class: Gaufrette\Filesystem + arguments: + - '@kunstmaan_form.filesystem_adapter' + public: true