<?php
declare(strict_types=1);
namespace Crea\MediaBundle\Entity;
use Crea\MediaBundle\Entity\Enum\MediaFileUploadModeEnum;
use Crea\MediaBundle\Repository\MediaFileRepository;
use Crea\ToolsBundle\Entity\LifecycleUserIdentifierTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Stringable;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=MediaFileRepository::class)
*
* @ORM\Table(name="media_file")
*/
class MediaFile implements Stringable, FileConstraintInterface
{
use LifecycleUserIdentifierTrait;
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*
* @Groups({"media_list"})
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $extension = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $mimeType = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $dir = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $filename = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $originalFilename = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $customFilename = null;
/**
* folder + name + extension.
*
* @ORM\Column(type="string", nullable=false, unique=true)
*
* @Groups({"media_list"})
*/
private ?string $path = null;
/**
* domain + folder + name + extension.
*
* @ORM\Column(type="string", nullable=false, unique=true)
*
* @Groups({"media_list"})
*/
private ?string $url = null;
/**
* Non mappé, rempli uniquement à la volée lorsque le CDN est configuré.
*/
private ?string $cdnUrl = null;
/**
* folder + name + extension.
*
* @ORM\Column(type="string", nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $thumbnailPath = null;
/**
* folder + name + extension.
*
* @ORM\Column(type="string", nullable=true)
*
* @Groups({"media_list"})
*/
private ?string $thumbnailUrl = null;
/**
* Unpersisted property.
*/
private ?File $uploadedFile = null;
/**
* Unit : Octet.
*
* @ORM\Column(type="integer", nullable=true)
*
* @Groups({"media_list"})
*/
private ?int $size = null;
/**
* Unit : pixel.
*
* @ORM\Column(type="integer", nullable=true)
*
* @Groups({"media_list"})
*/
private ?int $height = null;
/**
* Unit : pixel.
*
* @ORM\Column(type="integer", nullable=true)
*
* @Groups({"media_list"})
*/
private ?int $width = null;
/**
* Unit : seconds.
*
* @ORM\Column(type="integer", nullable=true)
*
* @Groups({"media_list"})
*/
private ?int $duration = null;
/**
* @ORM\ManyToOne(targetEntity=MediaType::class, inversedBy="mediaFiles")
*/
private ?MediaType $mediaType = null;
/**
* @var Collection<int, MediaTag>
*
* @ORM\ManyToMany(targetEntity=MediaTag::class, mappedBy="mediaFiles", cascade={"persist"})
*/
private Collection $mediaTags;
/**
* @ORM\Column(type="string", enumType=MediaFileUploadModeEnum::class, options={"default": "MANUAL_UPLOAD"})
*/
private MediaFileUploadModeEnum $uploadMode = MediaFileUploadModeEnum::MANUAL_UPLOAD;
public function __construct()
{
$this->mediaTags = new ArrayCollection();
}
public function __toString(): string
{
return $this->filename ?? '';
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): MediaFile
{
$this->id = $id;
return $this;
}
public function getExtension(): ?string
{
return $this->extension;
}
public function setExtension(?string $extension): MediaFile
{
$this->extension = $extension;
return $this;
}
public function getMimeType(): ?string
{
return $this->mimeType;
}
public function setMimeType(?string $mimeType): MediaFile
{
$this->mimeType = $mimeType;
return $this;
}
public function getDir(): ?string
{
return $this->dir;
}
public function setDir(?string $dir): MediaFile
{
$this->dir = $dir;
return $this;
}
public function getFilename(): ?string
{
return $this->filename;
}
public function setFilename(?string $filename): MediaFile
{
$this->filename = $filename;
return $this;
}
public function getFullName(): ?string
{
return implode('.', array_filter([$this->getFilename(), $this->getExtension()]));
}
public function getOriginalFilename(): ?string
{
return $this->originalFilename;
}
public function setOriginalFilename(?string $originalFilename): MediaFile
{
$this->originalFilename = $originalFilename;
return $this;
}
public function getCustomFilename(): ?string
{
return $this->customFilename;
}
public function getDisplayName(): ?string
{
return $this->getCustomFilename() ? $this->getCustomFilename().'.'.$this->getExtension() : $this->getOriginalFilename() ?? $this->getFullName();
}
public function setCustomFilename(?string $customFilename): MediaFile
{
$this->customFilename = $customFilename;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): MediaFile
{
$this->path = $path;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): MediaFile
{
$this->url = $url;
return $this;
}
public function getCdnUrl(): ?string
{
return $this->cdnUrl;
}
public function setCdnUrl(?string $cdnUrl): MediaFile
{
$this->cdnUrl = $cdnUrl;
return $this;
}
public function getDisplayUrl(): ?string
{
return $this->getCdnUrl() ?? $this->getUrl();
}
public function getUploadedFile(): ?File
{
return $this->uploadedFile;
}
public function setUploadedFile(?File $uploadedFile): MediaFile
{
$this->uploadedFile = $uploadedFile;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(?int $size): MediaFile
{
$this->size = $size;
return $this;
}
public function getHeight(): ?int
{
return $this->height;
}
public function setHeight(?int $height): MediaFile
{
$this->height = $height;
return $this;
}
public function getWidth(): ?int
{
return $this->width;
}
public function setWidth(?int $width): MediaFile
{
$this->width = $width;
return $this;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): MediaFile
{
$this->duration = $duration;
return $this;
}
public function getThumbnailPath(): ?string
{
return $this->thumbnailPath;
}
public function setThumbnailPath(?string $thumbnailPath): MediaFile
{
$this->thumbnailPath = $thumbnailPath;
return $this;
}
public function getThumbnailUrl(): ?string
{
return $this->thumbnailUrl;
}
public function setThumbnailUrl(?string $thumbnailUrl): MediaFile
{
$this->thumbnailUrl = $thumbnailUrl;
return $this;
}
public function getMediaType(): ?MediaType
{
return $this->mediaType;
}
public function setMediaType(?MediaType $mediaType): MediaFile
{
$this->mediaType = $mediaType;
return $this;
}
/**
* @return Collection<int, MediaTag>
*/
public function getMediaTags(): Collection
{
return $this->mediaTags;
}
public function addMediaTag(MediaTag $mediaTag): self
{
if (!$this->mediaTags->contains($mediaTag)) {
$this->mediaTags[] = $mediaTag;
$mediaTag->addMediaFile($this);
}
return $this;
}
public function removeMediaTag(MediaTag $mediaTag): self
{
if ($this->mediaTags->removeElement($mediaTag)) {
$mediaTag->removeMediaFile($this);
}
return $this;
}
public function getUploadMode(): MediaFileUploadModeEnum
{
return $this->uploadMode;
}
public function setUploadMode(MediaFileUploadModeEnum $uploadMode): MediaFile
{
$this->uploadMode = $uploadMode;
return $this;
}
}