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

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