src/Entity/OSFile.php line 15
<?phpnamespace App\Entity;use Doctrine\ORM\Mapping as ORM;use Doctrine\ORM\Mapping\MappedSuperclass;use Symfony\Component\HttpFoundation\File\File as SymfonyFile;use Symfony\Component\Validator\Constraints as Assert;use OlaSoft\Common;/*** @ORM\MappedSuperclass()* @ORM\HasLifecycleCallbacks()*/class OSFile implements \Serializable{protected $file;protected $filename;protected $autoResize = true;protected $previousTarget;/*** @ORM\Column(name="id", type="integer")* @ORM\Id* @ORM\GeneratedValue(strategy="AUTO")*/protected $id;/*** @ORM\Column(name="target", type="string", length=255,nullable=true)*/protected $target;/*** @ORM\Column(type="integer", nullable=true)*/protected $pages;/*** @ORM\Column(name="downloads", type="integer", nullable=true)*/protected $downloads;/*** @ORM\Column(name="reading", type="integer", nullable=true)*/protected $reading;/*** @ORM\Column(name="name", type="string", nullable=true)*/protected $name;/*** @ORM\Column(name="dir", type="string", length=255,nullable=true)*/protected $dir;/*** @ORM\Column(name="source", type="string", length=255,nullable=true)*/protected $source;/*** @ORM\Column(name="size", type="integer",nullable=true)*/protected $size;/*** @ORM\Column(type="boolean", nullable=true)*/protected $isEnabled;/*** @ORM\Column(type="boolean", nullable=true)*/protected $isDeleted;/*** @ORM\Column(type="datetime", nullable=true)*/protected $lastUpdate;/*** @ORM\Column(type="datetime", nullable=true)*/protected $createdAt;/*** @ORM\Column(name="formatSize", type="string", length=255,nullable=true)*/protected $formatSize;/*** @ORM\Column(name="mimetype", type="string", length=255,nullable=true)*/protected $mimetype;protected $validsMimetypes;public function __construct(?string $path = null){$this->file = null;$this->filename = null;$this->previousTarget = null;$this->validsMimetypes = ["*"];$this->autoResize = true;if($path){$t = explode("/", $path);$filename = $t[count($t)-1];$this->target = $filename;$this->dir = preg_replace('/\b\/'.$filename.'$\b/', '', $path);}else {$this->dir = 'upload/files/';}}public function getId(): ?int{return $this->id;}protected function getRoot(){return __DIR__.'/../../public/';}protected function getRootDir(){return $this->getRoot().$this->getDir();}public function getFile(){return $this->getDir().'/'.$this->target;}public function getRootFile(){return $this->getRootDir().'/'.$this->target;}public function getPages(): ?int{return $this->pages;}public function setPages(?int $pages): self{$this->pages = $pages;return $this;}public function getDownloads(): ?int{return $this->downloads;}public function setDownloads(?int $downloads): self{$this->downloads = $downloads;return $this;}public function getReading(): ?int{return $this->reading;}public function setReading(?int $reading): self{$this->reading = $reading;return $this;}public function getName(): ?string{return $this->name;}public function setName(?string $name): self{$this->name = $name;return $this;}public function getDir(): ?string{return $this->dir;}public function setDir(?string $dir): self{$this->dir = $dir;return $this;}public function setAutoResize(?bool $autoResize = true){$this->autoResize = $autoResize;}public function getSource(): ?string{return $this->source;}public function setSource(?string $source): self{$this->source = $source;return $this;}public function getIsEnabled(): ?bool{return $this->isEnabled;}public function setIsEnabled(?bool $isEnabled): self{$this->isEnabled = $isEnabled;return $this;}public function getIsDeleted(): ?bool{return $this->isDeleted;}public function setIsDeleted(?bool $isDeleted): self{$this->isDeleted = $isDeleted;return $this;}public function getLastUpdate(): ?\DateTimeInterface{return $this->lastUpdate;}public function setLastUpdate(?\DateTimeInterface $lastUpdate): self{$this->lastUpdate = $lastUpdate;return $this;}public function getCreatedAt(): ?\DateTimeInterface{return $this->createdAt;}public function setCreatedAt(\DateTimeInterface $createdAt): self{$this->createdAt = $createdAt;return $this;}/*** @ORM\PrePersist()*/public function setDateOnPersist(){$this->createdAt= new \DateTime();}/*** @ORM\PreUpdate()*/public function setDateOnUpdate(){$this->lastUpdate= new \DateTime();}public function getCreatedBy(): ?Users{return $this->createdBy;}public function setCreatedBy(?Users $createdBy): self{$this->createdBy = $createdBy;return $this;}public function getTarget(): ?SymfonyFile{if($this->target){$file = new SymfonyFile($this->target, false);return $file;}return null;}public function setTarget($target){if ($target){$this->file = $target;$this->source = $target->getClientOriginalName();$fileName = $this->filename ?? Common::generateCode().Common::generateName().'.'. ( $target->guessExtension() ? $target->guessExtension() : $target->getClientOriginalExtension() );$this->size = $target->getSize();$this->mimetype = $target->getMimeType();$this->previousTarget = $this->target;$this->target = $fileName;$this->formatSize = Common::prettySize($this->size);}return $this;}protected function getPrevFile(){return $this->getRootDir().'/'.$this->previousTarget;}public function setFilename($filename){$this->filename = $filename;return $this;}public function getSize(): ?int{return $this->size;}public function setSize(?int $size): self{$this->size = $size;return $this;}public function getFormatSize(): ?string{return $this->formatSize;}public function setFormatSize(?string $formatSize): self{$this->formatSize = $formatSize;return $this;}public function getMimetype(): ?string{return $this->mimetype;}public function setMimetype(?string $mimetype): self{$this->mimetype = $mimetype;return $this;}/*** @ORM\PrePersist()*/public function persistFile(){if ($this->file){$this->file->move($this->getRootDir(), $this->target);if($this->previousTarget && $this->previousTarget != $this->target){$previousFile = $this->getPrevFile();if(is_file($previousFile))unlink($previousFile);}}}/*** @ORM\PostUpdate()*/public function updateFile(){$this->persistFile();}/*** @ORM\PreRemove()*/public function deleteFile(){if(is_file($this->getFile()))unlink($this->getFile());}/*** @Assert\IsTrue(message = "Veuillez choisir des fichiers valides s'il vous plaît. Vérifiez également le MimeType.")*/public function isValidMimetype(){return (!is_array($this->validsMimetypes) && (empty($this->validsMimetypes) || $this->validsMimetypes == '*'))|| (is_array($this->validsMimetypes) && !count($this->validsMimetypes))|| in_array($this->mimetype, $this->validsMimetypes);}public function setValidsMimetypes(?array $mimetypes): self{$this->validsMimetypes = $mimetypes;return $this;}public function getValidsMimetypes(): ?array{return $this->validsMimetypes;}public function __toString(){return $this->getFile() ?? "";}public function serialize(){return serialize(array($this->id,$this->name,$this->target,));}public function unserialize($serialized){list ($this->id,$this->name,$this->target,) = unserialize($serialized);}}