vendor/vich/uploader-bundle/src/Storage/FileSystemStorage.php line 53

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\Mapping\PropertyMapping;
  6. /**
  7.  * FileSystemStorage.
  8.  *
  9.  * @author Dustin Dobervich <ddobervich@gmail.com>
  10.  * @final
  11.  */
  12. class FileSystemStorage extends AbstractStorage
  13. {
  14.     protected function doUpload(PropertyMapping $mappingFile $file, ?string $dirstring $name): ?File
  15.     {
  16.         $uploadDir $mapping->getUploadDestination().\DIRECTORY_SEPARATOR.$dir;
  17.         if ($file instanceof UploadedFile) {
  18.             return $file->move($uploadDir$name);
  19.         } else {
  20.             $targetPathname $uploadDir.\DIRECTORY_SEPARATOR.$name;
  21.             if (!\copy($file->getPathname(), $targetPathname)) {
  22.                 throw new \Exception('Could not copy file');
  23.             }
  24.             return new File($targetPathname);
  25.         }
  26.     }
  27.     protected function doRemove(PropertyMapping $mapping, ?string $dirstring $name): ?bool
  28.     {
  29.         $file $this->doResolvePath($mapping$dir$name);
  30.         return \file_exists($file) && \unlink($file);
  31.     }
  32.     protected function doResolvePath(PropertyMapping $mapping, ?string $dirstring $name, ?bool $relative false): string
  33.     {
  34.         $path = !empty($dir) ? $dir.\DIRECTORY_SEPARATOR.$name $name;
  35.         if ($relative) {
  36.             return $path;
  37.         }
  38.         return $mapping->getUploadDestination().\DIRECTORY_SEPARATOR.$path;
  39.     }
  40.     public function resolveUri($obj, ?string $fieldName null, ?string $className null): ?string
  41.     {
  42.         [$mapping$name] = $this->getFilename($obj$fieldName$className);
  43.         if (empty($name)) {
  44.             return null;
  45.         }
  46.         $uploadDir $this->convertWindowsDirectorySeparator($mapping->getUploadDir($obj));
  47.         $uploadDir = empty($uploadDir) ? '' $uploadDir.'/';
  48.         return \sprintf('%s/%s'$mapping->getUriPrefix(), $uploadDir.$name);
  49.     }
  50.     private function convertWindowsDirectorySeparator(string $string): string
  51.     {
  52.         return \str_replace('\\''/'$string);
  53.     }
  54. }