Skip to content

Commit

Permalink
add limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Seerus authored and Seerus committed Aug 20, 2015
1 parent 4579cd0 commit bf4e2a9
Show file tree
Hide file tree
Showing 21 changed files with 3,394 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function handleUpload(File $file, ResponseInterface $response, Request
if (!($file instanceof FileInterface)) {
$file = new FilesystemFile($file);
}

// validate
$this->validate($file, $request, $context);

Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function getConfigTreeBuilder()
->defaultValue(\PHP_INT_MAX)
->info('Set max_size to -1 for gracefully downgrade this number to the systems max upload size.')
->end()
->scalarNode('max_files')
->defaultValue(\PHP_INT_MAX)
->info('Set max files.')
->end()
->booleanNode('use_orphanage')->defaultFalse()->end()
->booleanNode('enable_progress')->defaultFalse()->end()
->booleanNode('enable_cancelation')->defaultFalse()->end()
Expand Down
86 changes: 86 additions & 0 deletions Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Glavweb\UploaderBundle\Driver;

use Doctrine\Common\Annotations\Reader as AnnotationReader;
use Glavweb\UploaderBundle\Exception\ClassNotUploadableException;
use Glavweb\UploaderBundle\Exception\ValueEmptyException;

/**
* Annotation driver
*
*/
class AnnotationDriver
{
const UPLOADABLE_ANNOTATION = 'Glavweb\UploaderBundle\Mapping\Annotation\Uploadable';
const UPLOADABLE_FIELD_ANNOTATION = 'Glavweb\UploaderBundle\Mapping\Annotation\UploadableField';

protected $reader;

public function __construct(AnnotationReader $reader)
{
$this->reader = $reader;
}

public function loadDataForClass(\ReflectionClass $class)
{
if (!$this->isUploadable($class)) {
throw new ClassNotUploadableException();
}

$data = array();

foreach ($class->getProperties() as $property) {
$uploadableField = $this->reader->getPropertyAnnotation($property, self::UPLOADABLE_FIELD_ANNOTATION);
if ($uploadableField === null) {
continue;
}

$fieldData = array(
'mapping' => $uploadableField->getMapping(),
'nameAddFunction' => $uploadableField->getNameAddFunction(),
'nameGetFunction' => $uploadableField->getNameGetFunction()
);

// $metadata->fields[$property->getName()] = $fieldData;
$data[$property->getName()] = $fieldData;
}

// return $metadata;
return $data;
}

protected function isUploadable(\ReflectionClass $class)
{
return $this->reader->getClassAnnotation($class, self::UPLOADABLE_ANNOTATION) !== null;
}

public function getDataByFieldName(\ReflectionClass $class, $fieldName)
{
if (!$this->isUploadable($class)) {
throw new ClassNotUploadableException();
}

if (!$fieldName) {
throw new ValueEmptyException();
}

$property = $class->getProperty($fieldName);

if (!$property) {
return false;
}

$uploadableField = $this->reader->getPropertyAnnotation($property, self::UPLOADABLE_FIELD_ANNOTATION);

if (!$uploadableField) {
return false;
}

return array(
'mapping' => $uploadableField->getMapping(),
'nameAddFunction' => $uploadableField->getNameAddFunction(),
'nameGetFunction' => $uploadableField->getNameGetFunction()
);
}
}
42 changes: 42 additions & 0 deletions EventListener/MaxFilesValidationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Glavweb\UploaderBundle\EventListener;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Glavweb\UploaderBundle\Event\ValidationEvent;
use Glavweb\UploaderBundle\Exception\ValidationException;

class MaxFilesValidationListener
{
private $doctrine;

/**
* @param Registry $doctrine
*/
public function __construct(Registry $doctrine)
{
$this->doctrine = $doctrine;
}

public function onValidate(ValidationEvent $event)
{
// $config = $event->getConfig();
//
// if (!isset($config['max_files'])) {
// return;
// }
//
// $request = $event->getRequest();
// $requestId = $request->get('_glavweb_uploader_request_id');
// $qb = $this->doctrine->getEntityManager()->createQueryBuilder();
// $qb->select('COUNT(m.id)')
// ->from('GlavwebUploaderBundle:Media', 'm')
// ->where('m.requestId = :requestId')
// ->setParameter('requestId', $requestId);
// $result = $qb->getQuery()->getSingleScalarResult();
//
// if ($result >= $config['max_files']) {
// throw new ValidationException('error.maxfiles');
// }
}
}
2 changes: 1 addition & 1 deletion EventListener/MaxSizeValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function onValidate(ValidationEvent $event)
$config = $event->getConfig();
$file = $event->getFile();

if ($file->getSize() > $config['max_size']) {
if ($file->getSize() > $config['max_size']*(1024*1024)) {
throw new ValidationException('error.maxsize');
}
}
Expand Down
12 changes: 12 additions & 0 deletions Exception/ClassNotUploadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Glavweb\UploaderBundle\Exception;

use Glavweb\UploaderBundle\Exception\Exception;
/**
* Class Exception
* @package Glavweb\UploaderBundle\Exception
*/
class ClassNotUploadableException extends Exception
{}
12 changes: 12 additions & 0 deletions Exception/MappingNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Glavweb\UploaderBundle\Exception;

use Glavweb\UploaderBundle\Exception\Exception;
/**
* Class Exception
* @package Glavweb\UploaderBundle\Exception
*/
class MappingNotSetException extends Exception
{}
12 changes: 12 additions & 0 deletions Exception/NotFoundPropertiesInAnnotationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Glavweb\UploaderBundle\Exception;

use Glavweb\UploaderBundle\Exception\Exception;
/**
* Class Exception
* @package Glavweb\UploaderBundle\Exception
*/
class NotFoundPropertiesInAnnotationException extends Exception
{}
12 changes: 12 additions & 0 deletions Exception/RequestEmptyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Glavweb\UploaderBundle\Exception;

use Glavweb\UploaderBundle\Exception\Exception;
/**
* Class Exception
* @package Glavweb\UploaderBundle\Exception
*/
class RequestEmptyException extends Exception
{}
12 changes: 12 additions & 0 deletions Exception/ValueEmptyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Glavweb\UploaderBundle\Exception;

use Glavweb\UploaderBundle\Exception\Exception;
/**
* Class Exception
* @package Glavweb\UploaderBundle\Exception
*/
class ValueEmptyException extends Exception
{}
76 changes: 60 additions & 16 deletions Form/Type/DropzoneType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Glavweb\UploaderBundle\Form\Type;

use Glavweb\UploaderBundle\Driver\AnnotationDriver;
use Glavweb\UploaderBundle\Exception\MappingNotSetException;
use Glavweb\UploaderBundle\Exception\NotFoundPropertiesInAnnotationException;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
Expand All @@ -15,6 +18,11 @@
*/
class DropzoneType extends AbstractType
{
/**
* @var array
*/
protected $config;

/**
* @var \Symfony\Bundle\FrameworkBundle\Routing\Router
*/
Expand All @@ -26,47 +34,76 @@ class DropzoneType extends AbstractType
protected $mediaHelper;

/**
* @param Router $router
* @var \Glavweb\UploaderBundle\Driver\AnnotationDriver;
*/
protected $driverAnnotation;

/**
* @param Router $router
* @param MediaHelper $mediaHelper
* @param array $config
* @param AnnotationDriver $driverAnnotation
*/
public function __construct(Router $router, MediaHelper $mediaHelper)
public function __construct(Router $router, MediaHelper $mediaHelper, array $config, AnnotationDriver $driverAnnotation)
{
$this->router = $router;
$this->mediaHelper = $mediaHelper;
$this->router = $router;
$this->mediaHelper = $mediaHelper;
$this->config = $config;
$this->driverAnnotation = $driverAnnotation;
}

/**
* @param FormView $view
* @param FormView $view
* @param FormInterface $form
* @param array $options
* @param array $options
* @throws MappingNotSetException
* @throws NotFoundPropertiesInAnnotationException
* @throws \Glavweb\UploaderBundle\Exception\ClassNotUploadableException
* @throws \Glavweb\UploaderBundle\Exception\ValueEmptyException
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (!isset($options['files'])) {
$options['files'] = array();
}
$entity = $form->getParent()->getData();
$fieldName = $form->getConfig()->getName();

if (!isset($options['requestId'])) {
$options['requestId'] = uniqid();
}

$files = $this->prepareFiles($options['files']);
$dataPropertyAnnotation = $this->driverAnnotation->getDataByFieldName(new \ReflectionClass($entity), $fieldName);

if (!$dataPropertyAnnotation) {
throw new NotFoundPropertiesInAnnotationException();
}

$files = $entity->$dataPropertyAnnotation['nameGetFunction']();
$context = $dataPropertyAnnotation['mapping'];

if (!$context) {
throw new MappingNotSetException();
}

$config = $this->getConfigByContext($context);
$maxFiles = $config['max_files'];

$view->vars['uploadedFilesTpl'] = $options['uploadedFilesTpl'];
$view->vars['uploadItemTpl'] = $options['uploadItemTpl'];
$view->vars['viewFormTpl'] = $options['viewFormTpl'];
$view->vars['type'] = $options['type'];
$view->vars['type'] = $context;
$view->vars['previewImg'] = $options['previewImg'];
$view->vars['useLink'] = $options['useLink'];
$view->vars['useForm'] = $options['useForm'];
$view->vars['showMark'] = $options['showMark'];
$view->vars['showUploadButton'] = $options['showUploadButton'];
$view->vars['files'] = $files;
$view->vars['countFiles'] = $files->count();
$view->vars['showLabel'] = $options['showLabel'];
$view->vars['requestId'] = $options['requestId'];
$view->vars['uploadDir'] = $this->mediaHelper->getUploadDirectoryUrl($options['type']);
$view->vars['deleteUrl'] = $this->router->generate('glavweb_uploader_delete', array('context' => $options['type']));
$view->vars['renameUrl'] = $this->router->generate('glavweb_uploader_rename', array('context' => $options['type']));
$view->vars['uploadDir'] = $this->mediaHelper->getUploadDirectoryUrl($context);
$view->vars['deleteUrl'] = $this->router->generate('glavweb_uploader_delete', array('context' => $context));
$view->vars['renameUrl'] = $this->router->generate('glavweb_uploader_rename', array('context' => $context));
$view->vars['maxFiles'] = $maxFiles;
$view->vars['maxSize'] = $config['max_size'];
}

/**
Expand All @@ -75,14 +112,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'files' => array(),
'previewImg' => null,
'requestId' => null,
'useLink' => true,
'useForm' => true,
'showMark' => true,
'showUploadButton' => true,
'type' => null,
'uploadedFilesTpl' => 'GlavwebUploaderBundle:Form:base_upload_item_tpl.html.twig',
'uploadItemTpl' => 'GlavwebUploaderBundle:Form:base_uploaded_files.html.twig',
'viewFormTpl' => 'GlavwebUploaderBundle:Form:view-form.html.twig',
Expand Down Expand Up @@ -125,4 +160,13 @@ protected function prepareFiles($files)

return $files;
}

/**
* @param string $context
* @return array
*/
protected function getConfigByContext($context)
{
return $this->config['mappings'][$context];
}
}
Loading

0 comments on commit bf4e2a9

Please sign in to comment.