From 7e49d89d3b516bc8fb95fba2092d4e941e69be97 Mon Sep 17 00:00:00 2001 From: Yorick van Klinken Date: Fri, 9 Oct 2020 10:04:21 +0000 Subject: [PATCH] Adding Filesystem to FileFormSubmissionField --- .../DependencyInjection/Configuration.php | 5 +++++ .../KunstmaanFormExtension.php | 1 + .../FileFormSubmissionField.php | 16 ++++++++-------- .../FormBundle/Resources/config/services.yml | 12 ++++++++++++ .../FileFormSubmissionFieldTest.php | 17 +++++++++++++---- 5 files changed, 39 insertions(+), 12 deletions(-) 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 diff --git a/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php b/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php index 4448320d02..11dd3bb615 100644 --- a/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php +++ b/src/Kunstmaan/FormBundle/Tests/unit/Entity/FormSubmissionFieldTypes/FileFormSubmissionFieldTest.php @@ -2,6 +2,7 @@ namespace Kunstmaan\FormBundle\Tests\Entity\FormSubmissionFieldTypes; +use Gaufrette\Filesystem; use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\FileFormSubmissionField; use Kunstmaan\FormBundle\Form\FileFormSubmissionType; use PHPUnit\Framework\TestCase; @@ -81,18 +82,16 @@ public function testGetSubmissionTemplate() public function testUpload() { $object = $this->object; - $this->assertNull($object->upload('..', '..')); $file = $this->getMockBuilder(UploadedFile::class) ->disableOriginalConstructor() ->getMock(); $file->expects($this->any()) - ->method('move') - ->willReturn(true); + ->method('getPathname') + ->willReturn(__FILE__); $object->file = $file; - $object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/'); $form = $this->getMockBuilder(Form::class) ->disableOriginalConstructor() @@ -112,6 +111,16 @@ public function testUpload() ->method('getParameter') ->willReturn('whatever'); + $filesystem = $this->getMockBuilder(Filesystem::class) + ->disableOriginalConstructor() + ->getMock(); + + $container->expects($this->any()) + ->method('get') + ->willReturn($filesystem); + $object->onValidPost($form, $builder, $request, $container); + $this->assertNull($object->upload('..', $filesystem)); + $object->upload(__DIR__ . '/../../Resources/assets/', $filesystem); } }