src/Entity/Media.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MediaRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use InvalidArgumentException;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12.  * @ORM\Entity(repositoryClass=MediaRepository::class)
  13.  * @Vich\Uploadable
  14.  */
  15. class Media
  16. {
  17.     const UPLOAD_MAX_SIZE "500M";
  18.     const IMAGE_MIMES = [
  19.         "image/png",
  20.         "image/jpeg",
  21.         "image/svg+xml"
  22.     ];
  23.     const AUDIO_MIMES = [
  24.         "audio/x-wav",
  25.         "audio/mpeg",
  26.         "audio/mp4a-latm",
  27.     ];
  28.     const VIDEO_MIMES = [
  29.         "video/mp4",
  30.         "video/quicktime",
  31.     ];
  32.     const DOCUMENT_MIMES = [
  33.         "application/pdf",
  34.         "application/msword",
  35.         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  36.         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  37.         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  38.         "text/csv",
  39.         "application/vnd.ms-excel.sheet.macroEnabled.12",
  40.         "application/vnd.ms-excel",
  41.     ];
  42.     const UPLOAD_MIMES = [
  43.         "image/png",
  44.         "image/jpeg",
  45.         "audio/mpeg",
  46.         "audio/x-wav",
  47.         "audio/mp4a-latm",
  48.         "video/mp4",
  49.         "video/quicktime",
  50.         "application/pdf",
  51.         "application/msword",
  52.         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  53.         "image/svg+xml",
  54.         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  55.         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  56.         "text/csv",
  57.         "application/vnd.ms-excel.sheet.macroEnabled.12",
  58.         "application/vnd.ms-excel",
  59.     ];
  60.     const AUDIO_TYPE 0;
  61.     const VIDEO_TYPE 1;
  62.     const IMAGE_TYPE 2;
  63.     const DOCUMENT_TYPE 3;
  64.     const NONE_TYPE 4;
  65.     const TEMPLATE_TYPE 5;
  66.     const TYPES = [
  67.         self::AUDIO_TYPE,
  68.         self::VIDEO_TYPE,
  69.         self::IMAGE_TYPE,
  70.         self::DOCUMENT_TYPE,
  71.         self::NONE_TYPE,
  72.         self::TEMPLATE_TYPE,
  73.     ];
  74.     /**
  75.      * @ORM\Id
  76.      * @ORM\GeneratedValue
  77.      * @ORM\Column(type="integer")
  78.      */
  79.     private $id;
  80.     /**
  81.      * @Vich\UploadableField(mapping="media", fileNameProperty="fileName")
  82.      */
  83.     #[Assert\File(
  84.         maxSizeself::UPLOAD_MAX_SIZE,
  85.         mimeTypesself::UPLOAD_MIMES,
  86.         mimeTypesMessage'Wrong file type {{ type }}, allowed types: {{ types }}',
  87.     )]
  88.     private ?File $file null;
  89.     /**
  90.      * @ORM\Column(type="string", length=255, nullable=true)
  91.      */
  92.     private $fileName;
  93.     /**
  94.      * @ORM\Column(type="smallint")
  95.      */
  96.     #[Assert\Choice(self::TYPES)]
  97.     private $type;
  98.     /**
  99.      * @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
  100.      */
  101.     private $createdAt;
  102.     /**
  103.      * @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
  104.      */
  105.     private $updatedAt;
  106.     /**
  107.      * @ORM\ManyToOne(targetEntity=Contributor::class, inversedBy="media", cascade={"persist"})
  108.      */
  109.     private $contributor;
  110.     /**
  111.      * @ORM\ManyToOne(targetEntity=Instance::class)
  112.      */
  113.     #[Assert\NotNull(message'L’organisation est obligatoire.')]
  114.     private $instance;
  115.     public function __construct()
  116.     {
  117.         // Only set dates if they are not already set (when creating new entity, not loading from DB)
  118.         if (!isset($this->createdAt)) {
  119.             $this->createdAt = new \DateTimeImmutable();
  120.         }
  121.         if (!isset($this->updatedAt)) {
  122.             $this->updatedAt = new \DateTimeImmutable();
  123.         }
  124.     }
  125.     /**
  126.      * @ORM\Column(type="text", nullable=true)
  127.      */
  128.     private $vignetteConfig;
  129.     public function getId(): ?int
  130.     {
  131.         return $this->id;
  132.     }
  133.     public function getFile(): ?File
  134.     {
  135.         return $this->file;
  136.     }
  137.     public function setFile(?File $file): self
  138.     {
  139.         $this->file $file;
  140.     
  141.         $this->updatedAt = new \DateTimeImmutable('now');
  142.         
  143.         // Only auto-detect type if no type is already set OR if the current type is TEMPLATE_TYPE
  144.         // Guard: skip MIME detection if the file doesn't exist on disk (e.g. inject_on_load on orphaned files)
  145.         if ($file && is_file($file->getPathname()) && (!isset($this->type) || $this->type !== self::TEMPLATE_TYPE)) {
  146.             $fileMime $file->getMimeType();
  147.             if (in_array($fileMimeself::IMAGE_MIMES)) {
  148.                 $this->type self::IMAGE_TYPE;
  149.             } elseif (in_array($fileMimeself::AUDIO_MIMES)) {
  150.                 $this->type self::AUDIO_TYPE;
  151.             } elseif (in_array($fileMimeself::VIDEO_MIMES)) {
  152.                 $this->type self::VIDEO_TYPE;
  153.             } elseif (in_array($fileMimeself::DOCUMENT_MIMES)) {
  154.                 $this->type self::DOCUMENT_TYPE;
  155.             } else {
  156.                 throw new InvalidArgumentException('Wrong file type');
  157.             }
  158.         }
  159.     
  160.         return $this;
  161.     }
  162.     public function getFileName(): ?string
  163.     {
  164.         return $this->fileName;
  165.     }
  166.     public function setFileName(?string $fileName): self
  167.     {
  168.         $this->fileName $fileName;
  169.         return $this;
  170.     }
  171.     public function getType(): ?int
  172.     {
  173.         return $this->type;
  174.     }
  175.     public function setType(int $type): self
  176.     {
  177.         $this->type $type;
  178.         return $this;
  179.     }
  180.     public function getCreatedAt(): ?\DateTimeImmutable
  181.     {
  182.         return $this->createdAt;
  183.     }
  184.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  185.     {
  186.         $this->createdAt $createdAt;
  187.         return $this;
  188.     }
  189.     public function getUpdatedAt(): ?\DateTimeImmutable
  190.     {
  191.         return $this->updatedAt;
  192.     }
  193.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  194.     {
  195.         $this->updatedAt $updatedAt;
  196.         return $this;
  197.     }
  198.     public function toString(): string
  199.     {
  200.         return '';
  201.     }
  202.     public function __toString()
  203.     {
  204.         return $this->fileName;
  205.     }
  206.     public function getContributor(): ?Contributor
  207.     {
  208.         return $this->contributor;
  209.     }
  210.     public function setContributor(?Contributor $contributor): self
  211.     {
  212.         $this->contributor $contributor;
  213.         return $this;
  214.     }
  215.     public function getInstance(): ?Instance
  216.     {
  217.         return $this->instance;
  218.     }
  219.     public function setInstance(?Instance $instance): self
  220.     {
  221.         $this->instance $instance;
  222.         return $this;
  223.     }
  224.     public function getVignetteConfig(): ?string
  225.     {
  226.         return $this->vignetteConfig;
  227.     }
  228.     public function setVignetteConfig(?string $vignetteConfig): self
  229.     {
  230.         $this->vignetteConfig $vignetteConfig;
  231.         return $this;
  232.     }
  233. }