src/Entity/OSFile.php line 15

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