src/Entity/File/OSFile.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Entity\File;
  3. use App\Entity\Certificate;
  4. use App\Entity\Users;
  5. use App\EventListener\OSFile\OSFileEntityListener;
  6. use App\Repository\FileRepository;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
  9. use Symfony\Component\Uid\Uuid;
  10. use Symfony\Component\Uid\UuidV4;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use OlaSoft\Common;
  13. #[ORM\Entity(repositoryClassFileRepository::class)]
  14. #[ORM\Table(name"File")]
  15. #[ORM\InheritanceType("JOINED")]
  16. #[ORM\DiscriminatorColumn(name"type"type"string")]
  17. #[ORM\DiscriminatorMap([
  18.     "file" => OSFile::class,
  19.     "picture" => Picture::class,
  20.     "certificate" => Certificate::class,
  21. ])]
  22. #[ORM\HasLifecycleCallbacks]
  23. #[ORM\EntityListeners([OSFileEntityListener::class])]
  24. class OSFile implements \Serializable
  25. {
  26.     public const ACCESS_PRIVATE 'private';
  27.     public const ACCESS_PROTECTED 'protected';
  28.     public const ACCESS_PUBLIC 'public';
  29.     protected $file;
  30.     public function getUploadedFile()
  31.     {
  32.         return $this->file;
  33.     }
  34.     protected ?string $filenamenull;
  35.     protected bool $autoResize true;
  36.     protected ?string $previousTargetnull;
  37.     #[ORM\Id]
  38.     #[ORM\GeneratedValue]
  39.     #[ORM\Column(type'integer')]
  40.     protected ?int $id;
  41.     #[ORM\Column(type'string'length255nullabletrue)]
  42.     #[Assert\NotNull]
  43.     protected ?string $targetnull;
  44.     #[ORM\Column(type'integer'nullabletrue)]
  45.     protected ?int $pages;
  46.     #[ORM\Column(type'integer'nullabletrue)]
  47.     protected ?int $downloads;
  48.     #[ORM\Column(type'integer'nullabletrue)]
  49.     protected ?int $reading;
  50.     #[ORM\Column(type'string'nullabletrue)]
  51.     protected ?string $namenull;
  52.     #[ORM\Column(type'string'length255nullabletrue)]
  53.     protected ?string $dir;
  54.     #[ORM\Column(type'string'length255nullabletrue)]
  55.     protected ?string $source;
  56.     #[ORM\Column(type'integer'nullabletrue)]
  57.     protected ?int $size;
  58.     #[ORM\Column(type'boolean'nullabletrue)]
  59.     protected ?bool $isEnabled;
  60.     #[ORM\Column(type'boolean'nullabletrue)]
  61.     protected ?bool $isDeleted;
  62.     #[ORM\Column(type'datetime'nullabletrue)]
  63.     protected ?\DateTimeInterface $lastUpdate;
  64.     #[ORM\Column(type'datetime'nullabletrue)]
  65.     protected ?\DateTimeInterface $createdAt;
  66.     #[ORM\Column(type'string'length255nullabletrue)]
  67.     protected ?string $formatSizenull;
  68.     #[ORM\Column(type'string'length255nullabletrue)]
  69.     protected ?string $mimetypenull;
  70.     protected array $validsMimetypes= [];
  71.     #[ORM\ManyToOne(targetEntityUsers::class)]
  72.     private ?Users $createdBy;
  73.     #[ORM\Column(type'uuid'nullabletrue)]
  74.     private ?UuidV4 $uuid;
  75.     #[ORM\Column(type'array'nullabletrue)]
  76.     private ?array $viewStrategy= [];
  77.     public function __construct(?string $path null)
  78.     {
  79.         $this->file null;
  80.         $this->filename null;
  81.         $this->previousTarget null;
  82.         $this->validsMimetypes = ["*"];
  83.         $this->autoResize true;
  84.         $this->uuid Uuid::v4();
  85.         if($path){
  86.             $t explode("/"$path);
  87.             $filename $t[count($t)-1];
  88.             $this->target $filename;
  89.             $this->dir preg_replace('/\b\/'.$filename.'$\b/'''$path);
  90.         }
  91.         else {
  92.             $this->dir 'upload/files/';
  93.         }
  94.     }
  95.     public function getId(): ?int
  96.     {
  97.         return $this->id;
  98.     }
  99.     protected function getRoot(){
  100.         return __DIR__.'/../../../public/';
  101.     }
  102.     protected function getRootDir(){
  103.         return $this->getRoot().$this->getDir();
  104.     }
  105.     public function getFile(){
  106.         return $this->getDir().'/'.$this->target;
  107.     }
  108.     public function getRootFile(){
  109.         return $this->getRootDir().'/'.$this->target;
  110.     }
  111.     public function getPages(): ?int
  112.     {
  113.         return $this->pages;
  114.     }
  115.     public function setPages(?int $pages): self
  116.     {
  117.         $this->pages $pages;
  118.         return $this;
  119.     }
  120.     public function getDownloads(): ?int
  121.     {
  122.         return $this->downloads;
  123.     }
  124.     public function setDownloads(?int $downloads): self
  125.     {
  126.         $this->downloads $downloads;
  127.         return $this;
  128.     }
  129.     public function getReading(): ?int
  130.     {
  131.         return $this->reading;
  132.     }
  133.     public function setReading(?int $reading): self
  134.     {
  135.         $this->reading $reading;
  136.         return $this;
  137.     }
  138.     public function getName(): ?string
  139.     {
  140.         return $this->name;
  141.     }
  142.     public function setName(?string $name): self
  143.     {
  144.         $this->name $name;
  145.         return $this;
  146.     }
  147.     public function getDir(): ?string
  148.     {
  149.         return $this->dir;
  150.     }
  151.     public function setDir(?string $dir): self
  152.     {
  153.         $this->dir $dir;
  154.         return $this;
  155.     }
  156.     public function setAutoResize(?bool $autoResize true){
  157.         $this->autoResize $autoResize;
  158.     }
  159.     public function getSource(): ?string
  160.     {
  161.         return $this->source;
  162.     }
  163.     public function setSource(?string $source): self
  164.     {
  165.         $this->source $source;
  166.         return $this;
  167.     }
  168.     public function getIsEnabled(): ?bool
  169.     {
  170.         return $this->isEnabled;
  171.     }
  172.     public function setIsEnabled(?bool $isEnabled): self
  173.     {
  174.         $this->isEnabled $isEnabled;
  175.         return $this;
  176.     }
  177.     public function getIsDeleted(): ?bool
  178.     {
  179.         return $this->isDeleted;
  180.     }
  181.     public function setIsDeleted(?bool $isDeleted): self
  182.     {
  183.         $this->isDeleted $isDeleted;
  184.         return $this;
  185.     }
  186.     public function getLastUpdate(): ?\DateTimeInterface
  187.     {
  188.         return $this->lastUpdate;
  189.     }
  190.     public function setLastUpdate(?\DateTimeInterface $lastUpdate): self
  191.     {
  192.         $this->lastUpdate $lastUpdate;
  193.         return $this;
  194.     }
  195.     public function getCreatedAt(): ?\DateTimeInterface
  196.     {
  197.         return $this->createdAt;
  198.     }
  199.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  200.     {
  201.         $this->createdAt $createdAt;
  202.         return $this;
  203.     }
  204.     #[ORM\PrePersist]
  205.     public  function setDateOnPersist(){
  206.         $this->createdAt= new \DateTime();
  207.     }
  208.     #[ORM\PreUpdate]
  209.     public  function setDateOnUpdate(){
  210.         $this->lastUpdate= new \DateTime();
  211.     }
  212.     public function getTarget(): ?SymfonyFile
  213.     {
  214.         if($this->target){
  215.             return new SymfonyFile($this->targetfalse);
  216.         }
  217.         return null;
  218.     }
  219.     public function setTarget($target)
  220.     {
  221.         if ($target){
  222.             $this->file $target;
  223.             $this->source $target->getClientOriginalName();
  224.             $fileName $this->filename ?? Common::generateCode().Common::generateName().'.'. ($target->guessExtension() ?: $target->getClientOriginalExtension());
  225.             $this->size $target->getSize();
  226.             $this->mimetype $target->getMimeType();
  227.             $this->previousTarget $this->target;
  228.             $this->target $fileName;
  229.             $this->formatSize Common::prettySize($this->size);
  230.         }
  231.         return $this;
  232.     }
  233.     protected function getPrevFile(){
  234.         return $this->getRootDir().'/'.$this->previousTarget;
  235.     }
  236.     public function setFilename($filename){
  237.         $this->filename $filename;
  238.         return $this;
  239.     }
  240.     public function getSize(): ?int
  241.     {
  242.         return $this->size;
  243.     }
  244.     public function setSize(?int $size): self
  245.     {
  246.         $this->size $size;
  247.         return $this;
  248.     }
  249.     public function getFormatSize(): ?string
  250.     {
  251.         return $this->formatSize;
  252.     }
  253.     public function setFormatSize(?string $formatSize): self
  254.     {
  255.         $this->formatSize $formatSize;
  256.         return $this;
  257.     }
  258.     public function getMimetype(): ?string
  259.     {
  260.         return $this->mimetype;
  261.     }
  262.     public function setMimetype(?string $mimetype): self
  263.     {
  264.         $this->mimetype $mimetype;
  265.         return $this;
  266.     }
  267.     #[ORM\PrePersist]
  268.     #[ORM\PostUpdate]
  269.     public function persistFile()
  270.     {
  271.         if ($this->file){
  272.             $this->file->move($this->getRootDir(), $this->target);
  273.             if($this->previousTarget && $this->previousTarget != $this->target){
  274.                 $previousFile $this->getPrevFile();
  275.                 if(is_file($previousFile))
  276.                     unlink($previousFile);
  277.             }
  278.         }
  279.     }
  280.     #[ORM\PreRemove]
  281.     public function deleteFile()
  282.     {
  283.         if(is_file($this->getFile()))
  284.             unlink($this->getFile());
  285.     }
  286.     #[Assert\IsTrue(message "Veuillez choisir des fichiers valides s'il vous plaît. Vérifiez également le MimeType.")]
  287.     public function isValidMimetype()
  288.     {
  289.         if(!$this->validsMimetypes || in_array('*'$this->validsMimetypes))
  290.             return true;
  291.         return  in_array($this->mimetype$this->validsMimetypes);
  292.     }
  293.     public function setValidsMimetypes(?array $mimetypes): self
  294.     {
  295.         $this->validsMimetypes $mimetypes;
  296.         return $this;
  297.     }
  298.     public function getValidsMimetypes(): ?array
  299.     {
  300.         return $this->validsMimetypes;
  301.     }
  302.     public function __toString(): string{
  303.         return $this->getFile() ?? "";
  304.     }
  305.     public function serialize()
  306.     {
  307.         return serialize(array(
  308.             $this->id,
  309.             $this->name,
  310.             $this->target,
  311.         ));
  312.     }
  313.     public function unserialize($serialized)
  314.     {
  315.         list (
  316.             $this->id,
  317.             $this->name,
  318.             $this->target,
  319.             ) = unserialize($serialized);
  320.     }
  321.     public function getCreatedBy(): ?Users
  322.     {
  323.         return $this->createdBy;
  324.     }
  325.     public function setCreatedBy(?Users $createdBy): self
  326.     {
  327.         $this->createdBy $createdBy;
  328.         return $this;
  329.     }
  330.     public function getUuid(): ?UuidV4
  331.     {
  332.         return $this->uuid;
  333.     }
  334.     public function getViewStrategy(): array
  335.     {
  336.         return $this->viewStrategy ?? [];
  337.     }
  338.     public function makeAsPrivate(): static
  339.     {
  340.         // add strategy to make file private
  341.         if(!$this->viewStrategy){
  342.             $this->viewStrategy = [];
  343.         }
  344.         $this->viewStrategy[] = self::ACCESS_PRIVATE;
  345.         return $this;
  346.     }
  347.     public function makeAsPublic(): static
  348.     {
  349.         // add strategy to make file public
  350.         if(!$this->viewStrategy){
  351.             $this->viewStrategy = [];
  352.         }
  353.         $this->viewStrategy[] = self::ACCESS_PUBLIC;
  354.         return $this;
  355.     }
  356.     public function makeAsProtected(): static
  357.     {
  358.         // add strategy to make file protected
  359.         if(!$this->viewStrategy){
  360.             $this->viewStrategy = [];
  361.         }
  362.         $this->viewStrategy[] = self::ACCESS_PROTECTED;
  363.         return $this;
  364.     }
  365.     public function __serialize(): array
  366.     {
  367.         return array(
  368.             $this->id,
  369.             $this->name,
  370.             $this->target,
  371.         );
  372.     }
  373.     public function __unserialize(array $data): void
  374.     {
  375.         list (
  376.             $this->id,
  377.             $this->name,
  378.             $this->target,
  379.             ) = $data;
  380.     }
  381. }