src/Entity/File.php line 12

Open in your IDE?
  1. <?php
  2.     namespace App\Entity;
  3.     use DateTimeInterface;
  4.     use Doctrine\ORM\Mapping as ORM;
  5.     use Symfony\Component\Validator\Constraints as Assert;
  6.     /**
  7.      * @ORM\MappedSuperclass
  8.      */
  9.     class File
  10.     {
  11.         /**
  12.          * @ORM\Id
  13.          * @ORM\GeneratedValue
  14.          * @ORM\Column(type="integer")
  15.          */
  16.         private ?int $id NULL;
  17.         /**
  18.          * @ORM\Column(type="string", length=255)
  19.          */
  20.         private string $name;
  21.         /**
  22.          * @ORM\Column(type="string", length=255, nullable=true)
  23.          */
  24.         private ?string $path;
  25.         /**
  26.          * @ORM\Column(type="datetime", nullable=true)
  27.          */
  28.         private $uploadDate;
  29.         /**
  30.          * @Assert\File(maxSize="6000000")
  31.          */
  32.         protected $file;
  33.         protected $kernelRootDir;
  34.         /*
  35.          * ============================================================================================
  36.          * =============================== FONCTIONS CUSTOM ===========================================
  37.          * ============================================================================================
  38.          */
  39.         public function getWebPath()
  40.         {
  41.             return NULL === $this->path NULL $this->getUploadDir() . '/' $this->path;
  42.         }
  43.         protected function getUploadDir()
  44.         {
  45.             // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  46.             // le document/image dans la vue.
  47.             return 'uploads/files';
  48.         }
  49.         public function preUpload()
  50.         {
  51.             if (NULL !== $this->file) {
  52.                 // faites ce que vous voulez pour générer un nom unique
  53.                 $this->path sha1(uniqid(mt_rand(), TRUE)) . '.' $this->file->guessExtension();
  54.             }
  55.         }
  56.         public function upload()
  57.         {
  58.             if (NULL === $this->file) {
  59.                 return;
  60.             }
  61.             // s'il y a une erreur lors du déplacement du fichier, une exception
  62.             // va automatiquement être lancée par la méthode move(). Cela va empêcher
  63.             // proprement l'entité d'être persistée dans la base de données si
  64.             // erreur il y a
  65.             $this->file->move($this->getUploadRootDir(), $this->path);
  66.             unset($this->file);
  67.         }
  68.         /**
  69.          * @return string
  70.          * @deprecated
  71.          */
  72.         public function getUploadRootDir()
  73.         {
  74.             // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  75.             return $this->getKernelRootDir() . '/public/' $this->getUploadDir();
  76.         }
  77.         /**
  78.          * @return mixed
  79.          * @deprecated
  80.          */
  81.         public function getKernelRootDir()
  82.         {
  83.             return $this->kernelRootDir;
  84.         }
  85.         /**
  86.          * @param mixed $kernelRootDir
  87.          *
  88.          * @deprecated
  89.          */
  90.         public function setKernelRootDir($kernelRootDir)
  91.         {
  92.             $this->kernelRootDir $kernelRootDir;
  93.             return $this;
  94.         }
  95.         /**
  96.          * @ORM\PostRemove()
  97.          */
  98.         public function removeUpload()
  99.         {
  100.             if ($file $this->getAbsolutePath()) {
  101.                 unlink($file);
  102.             }
  103.         }
  104.         public function getAbsolutePath()
  105.         {
  106.             return NULL === $this->path NULL $this->getUploadRootDir() . '/' $this->path;
  107.         }
  108.         /*
  109.          * ============================================================================================
  110.          * ============================== FIN FONCTIONS CUSTOM ========================================
  111.          * ============================================================================================
  112.          */
  113.         public function getId(): ?int
  114.         {
  115.             return $this->id;
  116.         }
  117.         public function getName(): string
  118.         {
  119.             return $this->name;
  120.         }
  121.         public function setName(string $name): self
  122.         {
  123.             $this->name $name;
  124.             return $this;
  125.         }
  126.         public function getPath(): ?string
  127.         {
  128.             return $this->path;
  129.         }
  130.         public function setPath(?string $path): self
  131.         {
  132.             $this->path $path;
  133.             return $this;
  134.         }
  135.         public function getUploadDate(): ?DateTimeInterface
  136.         {
  137.             return $this->uploadDate;
  138.         }
  139.         public function setUploadDate(?DateTimeInterface $uploadDate): self
  140.         {
  141.             $this->uploadDate $uploadDate;
  142.             return $this;
  143.         }
  144.     }