src/EventSubscriber/FileUploadSubscriber.php line 128

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-05-02
  5.  * Time: 13:27
  6.  */
  7. namespace App\EventSubscriber;
  8. use App\Service\Features;
  9. use Intervention\Image\ImageManagerStatic as Image;
  10. use League\Flysystem\FilesystemOperator;
  11. use Oneup\UploaderBundle\Event\PostUploadEvent;
  12. use Oneup\UploaderBundle\Event\PreUploadEvent;
  13. use Oneup\UploaderBundle\Event\ValidationEvent;
  14. use Oneup\UploaderBundle\Uploader\Exception\ValidationException;
  15. use Oneup\UploaderBundle\Uploader\File\FileInterface;
  16. use Oneup\UploaderBundle\UploadEvents;
  17. use Symfony\Component\Asset\Packages;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\File\File;
  21. class FileUploadSubscriber implements EventSubscriberInterface
  22. {
  23.     private static array $allowedPhotoExtensions = ['jpg''jpeg''png'];
  24.     private static array $allowedVideoExtensions = ['mp4''flv''mov'];
  25.     private int $minAvatarWidth;
  26.     private int $minAvatarHeight;
  27.     private int $maxImageSideSize;
  28.     public function __construct(
  29.         private Packages $assetPackage,
  30.         ParameterBagInterface $parameterBag,
  31.         private Features $features,
  32.         private FilesystemOperator $tmpFilesystem
  33.     )
  34.     {
  35.         $this->minAvatarWidth = (int)$parameterBag->get('app.profile.avatar.min_width');
  36.         $this->minAvatarHeight = (int)$parameterBag->get('app.profile.avatar.min_height');
  37.         $this->maxImageSideSize = (int)$parameterBag->get('app.profile.image.max_side_size');
  38.     }
  39.     public function returnFilename(PostUploadEvent $event)
  40.     {
  41.         /** @var File $file */
  42.         $file $event->getFile();
  43.         $response $event->getResponse();
  44.         /*
  45.         // Path manipulations for orphanage
  46.         $fullPath = $file->getPath();
  47.         $firstLevelDirectory = basename($fullPath);
  48.         $secondLevelDirectory = basename(dirname($fullPath));
  49.         $filename = $file->getFilename();
  50.         $response['filename'] = "$secondLevelDirectory/$firstLevelDirectory/$filename";
  51.         */
  52.         $response['filename'] = $this->getPathname($file);
  53.         return $response['filename'];
  54.     }
  55.     protected function getPathname(FileInterface $file)
  56.     {
  57.         $filename $file->getPathname();
  58.         if('png' === $file->getExtension()) {
  59.             $filename str_replace('.' $file->getExtension(), '.jpg'$filename);
  60.         }
  61.         return $filename;
  62.     }
  63.     public function validateAvatar(ValidationEvent $event): void
  64.     {
  65.         $file $event->getFile();
  66.         list($width$height) = getimagesize($file->getPathname());
  67.         if($width $this->minAvatarWidth || $height $this->minAvatarHeight)
  68.             throw new ValidationException('Image too small.');
  69.         $this->validatePhotoFile($event);
  70.     }
  71.     public function validatePhotoFile(ValidationEvent $event): void
  72.     {
  73.         $file $event->getFile();
  74.         $extension strtolower($file->getExtension());
  75.         if (!in_array($extensionself::$allowedPhotoExtensionstrue)) {
  76.             throw new ValidationException('This kind of files is not allowed.');
  77.         }
  78.     }
  79.     public function validateVideoFile(ValidationEvent $event): void
  80.     {
  81.         $file $event->getFile();
  82.         $extension strtolower($file->getExtension());
  83.         if (!in_array($extensionself::$allowedVideoExtensionstrue)) {
  84.             throw new ValidationException('This kind of files is not allowed.');
  85.         }
  86.     }
  87.     public function validateApprovalMediaFile(ValidationEvent $event): void
  88.     {
  89.         $file $event->getFile();
  90.         $extension strtolower($file->getExtension());
  91.         if (!in_array($extensiontrue === $this->features->approval_by_video() ? self::$allowedVideoExtensions self::$allowedPhotoExtensionstrue)) {
  92.             throw new ValidationException('This kind of files is not allowed.');
  93.         }
  94.         if($event->getFile()->getSize() > 209715200) {
  95.             throw new ValidationException('Video is too big.');
  96.         }
  97.     }
  98.     public function postUploadUserAvatar(PostUploadEvent $event): void
  99.     {
  100.         $response $event->getResponse();
  101.         $pathname $this->returnFilename($event);
  102.         $response['path'] = $this->assetPackage->getUrl($pathname'user_media');
  103.     }
  104.     public function postUploadProfileAvatar(PostUploadEvent $event): void
  105.     {
  106.         $response $event->getResponse();
  107.         $pathname $this->returnFilename($event);
  108.         $response['path'] = $this->assetPackage->getUrl($pathname'profile_media_avatar');
  109.     }
  110.     public function postUploadSaloonThumb(PostUploadEvent $event): void
  111.     {
  112.         $response $event->getResponse();
  113.         $pathname $this->returnFilename($event);
  114.         $response['path'] = $this->assetPackage->getUrl($pathname'saloon_media_thumb');
  115.     }
  116.     protected function processFileParameters(File $file)
  117.     {
  118.         if(str_starts_with($file->getMimeType(), 'video/'))
  119.             return;
  120.         $image Image::make($this->tmpFilesystem->read($file->getFilename()));
  121.         $maxSideSize max($image->width(), $image->height());
  122.         if($maxSideSize $this->maxImageSideSize) {
  123.             if ($maxSideSize == $image->width()) {
  124.                 $newWidth $this->maxImageSideSize;
  125.                 $newHeight $newWidth $image->height() / $image->width();
  126.             } else {
  127.                 $newHeight $this->maxImageSideSize;
  128.                 $newWidth $newHeight $image->width() / $image->height();
  129.             }
  130.             $image->resize($newWidth$newHeight);
  131.         }
  132.         $this->tmpFilesystem->write($file->getFilename(), $image->encode('jpg'));
  133.     }
  134.     public function preUpload(PreUploadEvent $event): void
  135.     {
  136.         /** @var File $file */
  137.         $file $event->getFile();
  138.         $this->processFileParameters($file);
  139.     }
  140.     /**
  141.      * @inheritDoc
  142.      */
  143.     public static function getSubscribedEvents()
  144.     {
  145.         return [
  146.             PreUploadEvent::NAME => 'preUpload',
  147.             UploadEvents::postUpload('profile_media_avatar') => 'postUploadProfileAvatar',
  148.             UploadEvents::postUpload('profile_photo') => 'returnFilename',
  149.             UploadEvents::postUpload('profile_selfie') => 'returnFilename',
  150.             UploadEvents::postUpload('profile_video') => 'returnFilename',
  151.             UploadEvents::postUpload('profile_media_approval') => 'returnFilename',
  152.             UploadEvents::postUpload('saloon_media') => 'returnFilename',
  153.             UploadEvents::postUpload('saloon_media_thumb') => 'postUploadSaloonThumb',
  154.             UploadEvents::postUpload('saloon_media_video') => 'returnFilename',
  155.             UploadEvents::postUpload('user_media') => 'postUploadUserAvatar',
  156.             UploadEvents::postUpload('tmp_media') => 'returnFilename',
  157.             UploadEvents::postUpload('process_queue') => 'returnFilename',
  158.             UploadEvents::validation('profile_media_avatar') => 'validateAvatar',
  159.             UploadEvents::validation('profile_photo') => 'validatePhotoFile',
  160.             UploadEvents::validation('profile_selfie') => 'validatePhotoFile',
  161.             UploadEvents::validation('profile_video') => 'validateVideoFile',
  162.             UploadEvents::validation('profile_media_approval') => 'validateApprovalMediaFile',
  163.             UploadEvents::validation('saloon_media') => 'validatePhotoFile',
  164.             UploadEvents::validation('saloon_media_thumb') => 'validateAvatar',
  165.             UploadEvents::validation('saloon_media_video') => 'validateVideoFile',
  166.             UploadEvents::validation('user_media') => 'validateAvatar',
  167.             UploadEvents::validation('tmp_media') => 'validateVideoFile',
  168.             UploadEvents::validation('process_queue') => 'validateVideoFile',
  169.         ];
  170.     }
  171. }