<?php
namespace App\Entity\File;
use App\Entity\Certificate;
use App\Entity\Users;
use App\EventListener\OSFile\OSFileEntityListener;
use App\Repository\FileRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Validator\Constraints as Assert;
use OlaSoft\Common;
#[ORM\Entity(repositoryClass: FileRepository::class)]
#[ORM\Table(name: "File")]
#[ORM\InheritanceType("JOINED")]
#[ORM\DiscriminatorColumn(name: "type", type: "string")]
#[ORM\DiscriminatorMap([
"file" => OSFile::class,
"picture" => Picture::class,
"certificate" => Certificate::class,
])]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([OSFileEntityListener::class])]
class OSFile implements \Serializable
{
public const ACCESS_PRIVATE = 'private';
public const ACCESS_PROTECTED = 'protected';
public const ACCESS_PUBLIC = 'public';
protected $file;
public function getUploadedFile()
{
return $this->file;
}
protected ?string $filename= null;
protected bool $autoResize = true;
protected ?string $previousTarget= null;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
protected ?int $id;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Assert\NotNull]
protected ?string $target= null;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $pages;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $downloads;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $reading;
#[ORM\Column(type: 'string', nullable: true)]
protected ?string $name= null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
protected ?string $dir;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
protected ?string $source;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $size;
#[ORM\Column(type: 'boolean', nullable: true)]
protected ?bool $isEnabled;
#[ORM\Column(type: 'boolean', nullable: true)]
protected ?bool $isDeleted;
#[ORM\Column(type: 'datetime', nullable: true)]
protected ?\DateTimeInterface $lastUpdate;
#[ORM\Column(type: 'datetime', nullable: true)]
protected ?\DateTimeInterface $createdAt;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
protected ?string $formatSize= null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
protected ?string $mimetype= null;
protected array $validsMimetypes= [];
#[ORM\ManyToOne(targetEntity: Users::class)]
private ?Users $createdBy;
#[ORM\Column(type: 'uuid', nullable: true)]
private ?UuidV4 $uuid;
#[ORM\Column(type: 'array', nullable: true)]
private ?array $viewStrategy= [];
public function __construct(?string $path = null)
{
$this->file = null;
$this->filename = null;
$this->previousTarget = null;
$this->validsMimetypes = ["*"];
$this->autoResize = true;
$this->uuid = Uuid::v4();
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 getTarget(): ?SymfonyFile
{
if($this->target){
return new SymfonyFile($this->target, false);
}
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->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]
#[ORM\PostUpdate]
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\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()
{
if(!$this->validsMimetypes || in_array('*', $this->validsMimetypes))
return true;
return 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(): string{
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);
}
public function getCreatedBy(): ?Users
{
return $this->createdBy;
}
public function setCreatedBy(?Users $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getUuid(): ?UuidV4
{
return $this->uuid;
}
public function getViewStrategy(): array
{
return $this->viewStrategy ?? [];
}
public function makeAsPrivate(): static
{
// add strategy to make file private
if(!$this->viewStrategy){
$this->viewStrategy = [];
}
$this->viewStrategy[] = self::ACCESS_PRIVATE;
return $this;
}
public function makeAsPublic(): static
{
// add strategy to make file public
if(!$this->viewStrategy){
$this->viewStrategy = [];
}
$this->viewStrategy[] = self::ACCESS_PUBLIC;
return $this;
}
public function makeAsProtected(): static
{
// add strategy to make file protected
if(!$this->viewStrategy){
$this->viewStrategy = [];
}
$this->viewStrategy[] = self::ACCESS_PROTECTED;
return $this;
}
public function __serialize(): array
{
return array(
$this->id,
$this->name,
$this->target,
);
}
public function __unserialize(array $data): void
{
list (
$this->id,
$this->name,
$this->target,
) = $data;
}
}