Skip to content

Commit

Permalink
Adding Filesystem to FileFormSubmissionField
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorick van Klinken committed Oct 9, 2020
1 parent 85575de commit 4c28fd8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -30,6 +31,10 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('deletable_formsubmissions')->defaultFalse()->end()
->scalarNode('web_root')
->defaultValue(SymfonyVersion::getRootWebPath())
->cannotBeEmpty()
->end()
->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Kunstmaan/FormBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
}

0 comments on commit 4c28fd8

Please sign in to comment.