vendor/vich/uploader-bundle/src/Storage/AbstractStorage.php line 128

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\Storage;
  3. use Symfony\Component\HttpFoundation\File\UploadedFile;
  4. use Vich\UploaderBundle\Exception\MappingNotFoundException;
  5. use Vich\UploaderBundle\Mapping\PropertyMapping;
  6. use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
  7. /**
  8.  * FileSystemStorage.
  9.  *
  10.  * @author Dustin Dobervich <ddobervich@gmail.com>
  11.  */
  12. abstract class AbstractStorage implements StorageInterface
  13. {
  14.     /**
  15.      * @var PropertyMappingFactory
  16.      */
  17.     protected $factory;
  18.     public function __construct(PropertyMappingFactory $factory)
  19.     {
  20.         $this->factory $factory;
  21.     }
  22.     /**
  23.      * @return mixed
  24.      */
  25.     abstract protected function doUpload(PropertyMapping $mappingUploadedFile $file, ?string $dirstring $name);
  26.     public function upload($objPropertyMapping $mapping): void
  27.     {
  28.         $file $mapping->getFile($obj);
  29.         if (null === $file || !($file instanceof UploadedFile)) {
  30.             throw new \LogicException('No uploadable file found');
  31.         }
  32.         $name $mapping->getUploadName($obj);
  33.         $mapping->setFileName($obj$name);
  34.         $mimeType $file->getMimeType();
  35.         $mapping->writeProperty($obj'size'$file->getSize());
  36.         $mapping->writeProperty($obj'mimeType'$mimeType);
  37.         $mapping->writeProperty($obj'originalName'$file->getClientOriginalName());
  38.         if (false !== \strpos($mimeType'image/') && 'image/svg+xml' !== $mimeType && false !== $dimensions = @\getimagesize($file)) {
  39.             $mapping->writeProperty($obj'dimensions'\array_splice($dimensions02));
  40.         }
  41.         $dir $mapping->getUploadDir($obj);
  42.         $this->doUpload($mapping$file$dir$name);
  43.     }
  44.     abstract protected function doRemove(PropertyMapping $mapping, ?string $dirstring $name): ?bool;
  45.     public function remove($objPropertyMapping $mapping): ?bool
  46.     {
  47.         $name $mapping->getFileName($obj);
  48.         if (empty($name)) {
  49.             return false;
  50.         }
  51.         return $this->doRemove($mapping$mapping->getUploadDir($obj), $name);
  52.     }
  53.     /**
  54.      * Do resolve path.
  55.      *
  56.      * @param PropertyMapping $mapping  The mapping representing the field
  57.      * @param string|null     $dir      The directory in which the file is uploaded
  58.      * @param string          $name     The file name
  59.      * @param bool            $relative Whether the path should be relative or absolute
  60.      */
  61.     abstract protected function doResolvePath(PropertyMapping $mapping, ?string $dirstring $name, ?bool $relative false): string;
  62.     public function resolvePath($obj, ?string $fieldName null, ?string $className null, ?bool $relative false): ?string
  63.     {
  64.         [$mapping$filename] = $this->getFilename($obj$fieldName$className);
  65.         if (empty($filename)) {
  66.             return null;
  67.         }
  68.         return $this->doResolvePath($mapping$mapping->getUploadDir($obj), $filename$relative);
  69.     }
  70.     public function resolveUri($obj, ?string $fieldName null, ?string $className null): ?string
  71.     {
  72.         [$mapping$filename] = $this->getFilename($obj$fieldName$className);
  73.         if (empty($filename)) {
  74.             return null;
  75.         }
  76.         $dir $mapping->getUploadDir($obj);
  77.         $path = !empty($dir) ? $dir.'/'.$filename $filename;
  78.         return $mapping->getUriPrefix().'/'.$path;
  79.     }
  80.     public function resolveStream($objstring $fieldName, ?string $className null)
  81.     {
  82.         $path $this->resolvePath($obj$fieldName$className);
  83.         if (empty($path)) {
  84.             return null;
  85.         }
  86.         return \fopen($path'rb');
  87.     }
  88.     /**
  89.      * note: extension point.
  90.      *
  91.      * @param object $obj
  92.      *
  93.      * @throws MappingNotFoundException
  94.      * @throws \RuntimeException
  95.      * @throws \Vich\UploaderBundle\Exception\NotUploadableException
  96.      */
  97.     protected function getFilename($obj, ?string $fieldName null, ?string $className null): array
  98.     {
  99.         $mapping null === $fieldName ?
  100.             $this->factory->fromFirstField($obj$className) :
  101.             $this->factory->fromField($obj$fieldName$className)
  102.         ;
  103.         if (null === $mapping) {
  104.             throw new MappingNotFoundException(\sprintf('Mapping not found for field "%s"'$fieldName));
  105.         }
  106.         return [$mapping$mapping->getFileName($obj)];
  107.     }
  108. }