<?php
namespace App\Entity;
use App\Repository\MediaRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=MediaRepository::class)
* @Vich\Uploadable
*/
class Media
{
const UPLOAD_MAX_SIZE = "500M";
const IMAGE_MIMES = [
"image/png",
"image/jpeg",
"image/svg+xml"
];
const AUDIO_MIMES = [
"audio/x-wav",
"audio/mpeg",
"audio/mp4a-latm",
];
const VIDEO_MIMES = [
"video/mp4",
"video/quicktime",
];
const DOCUMENT_MIMES = [
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text/csv",
"application/vnd.ms-excel.sheet.macroEnabled.12",
"application/vnd.ms-excel",
];
const UPLOAD_MIMES = [
"image/png",
"image/jpeg",
"audio/mpeg",
"audio/x-wav",
"audio/mp4a-latm",
"video/mp4",
"video/quicktime",
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"image/svg+xml",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text/csv",
"application/vnd.ms-excel.sheet.macroEnabled.12",
"application/vnd.ms-excel",
];
const AUDIO_TYPE = 0;
const VIDEO_TYPE = 1;
const IMAGE_TYPE = 2;
const DOCUMENT_TYPE = 3;
const NONE_TYPE = 4;
const TYPES = [
self::AUDIO_TYPE,
self::VIDEO_TYPE,
self::IMAGE_TYPE,
self::DOCUMENT_TYPE,
self::NONE_TYPE,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Vich\UploadableField(mapping="media", fileNameProperty="fileName")
*/
#[Assert\File(
maxSize: self::UPLOAD_MAX_SIZE,
mimeTypes: self::UPLOAD_MIMES,
mimeTypesMessage: 'Wrong file type {{ type }}, allowed types: {{ types }}',
)]
private ?File $file = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fileName;
/**
* @ORM\Column(type="smallint")
*/
#[Assert\Choice(self::TYPES)]
private $type;
/**
* @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=Contributor::class, inversedBy="media", cascade={"persist"})
*/
private $contributor;
/**
* @ORM\ManyToOne(targetEntity=Instance::class)
*/
#[Assert\NotNull(message: 'L’organisation est obligatoire.')]
private $instance;
public function __construct()
{
// Only set dates if they are not already set (when creating new entity, not loading from DB)
if (!isset($this->createdAt)) {
$this->createdAt = new \DateTimeImmutable();
}
if (!isset($this->updatedAt)) {
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getId(): ?int
{
return $this->id;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): self
{
$this->file = $file;
$this->updatedAt = new \DateTimeImmutable('now');
if ($file) {
$fileMime = $file->getMimeType();
if (in_array($fileMime, self::IMAGE_MIMES)) {
$this->type = self::IMAGE_TYPE;
} elseif (in_array($fileMime, self::AUDIO_MIMES)) {
$this->type = self::AUDIO_TYPE;
} elseif (in_array($fileMime, self::VIDEO_MIMES)) {
$this->type = self::VIDEO_TYPE;
} elseif (in_array($fileMime, self::DOCUMENT_MIMES)) {
$this->type = self::DOCUMENT_TYPE;
} elseif (in_array($fileMime, self::AUDIO_MIMES)) {
$this->type = self::AUDIO_TYPE;
} else {
throw new InvalidArgumentException('Wrong file type');
}
}
return $this;
}
public function getFileName(): ?string
{
return $this->fileName;
}
public function setFileName(?string $fileName): self
{
$this->fileName = $fileName;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function toString(): string
{
return '';
}
public function __toString()
{
return $this->fileName;
}
public function getContributor(): ?Contributor
{
return $this->contributor;
}
public function setContributor(?Contributor $contributor): self
{
$this->contributor = $contributor;
return $this;
}
public function getInstance(): ?Instance
{
return $this->instance;
}
public function setInstance(?Instance $instance): self
{
$this->instance = $instance;
return $this;
}
}