src/Entity/File/OSFile.php line 26

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