<?php
namespace Suzuki\MediaBundle\Provider;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\ImageProvider;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class CustomImageProvider extends ImageProvider
{
private const MAX_SIZE = 10240000;
/**
* @param ErrorElement $errorElement
* @param MediaInterface $media
*/
public function validate(ErrorElement $errorElement, MediaInterface $media)
{
parent::validate($errorElement, $media);
$size = 0;
if ($media->getBinaryContent() instanceof UploadedFile) {
$size = $media->getBinaryContent()->getClientSize();
} elseif ($media->getBinaryContent() instanceof File) {
$size = $media->getBinaryContent()->getSize();
}
if ($size > self::MAX_SIZE) {
$errorElement
->with('binaryContent')
->addViolation('The file is too big, max size: ' . (self::MAX_SIZE / 1024000) . 'MB')
->end();
}
}
}