<?php
namespace App\Entity\Core;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User\User;
use Symfony\Component\HttpFoundation\File\UploadedFile;
#[ORM\Table('file')]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class File
{
public const string DEFAULT_UPLOAD_DIRECTORY = 'uploads';
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private int $id;
#[ORM\Column(name: 'name', type: 'string', length: 255)]
private string $name;
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
#[ORM\OneToOne(targetEntity: User::class)]
private User $user;
#[ORM\Column(name: 'size', type: 'integer')]
private int $size;
#[ORM\Column(name: 'md5', type: 'string', length: 255)]
private string $md5;
#[ORM\Column(name: 'fullpath', type: 'string', length: 255)]
private string $fullPath;
private ?UploadedFile $uploadedFile;
/**
* Used in case we are overwriting a file, to store the old filepath.
*/
private string $temp;
public function __construct()
{
/* Making sure the directory exists */
if (!is_dir(__DIR__ . '/../../../public/' . self::DEFAULT_UPLOAD_DIRECTORY)) {
mkdir(__DIR__ . '/../../../' . self::DEFAULT_UPLOAD_DIRECTORY, 0755, true);
}
}
public function getId(): int
{
return $this->id;
}
public function setName(string $name): File
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setSize(int $size): File
{
$this->size = $size;
return $this;
}
public function getSize(): int
{
return $this->size;
}
public function setMd5(string $md5)
{
$this->md5 = $md5;
}
public function getMd5(): string
{
return $this->md5;
}
public function setUploadedFile(?UploadedFile $file = null): void
{
$this->uploadedFile = $file;
$this->size = $file->getSize();
$this->name = $file->getClientOriginalName();
// Check if we have an old file
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
}
}
public function getUploadedFile(): ?UploadedFile
{
return $this->uploadedFile;
}
public function setFullPath(string $fullPath): void
{
$this->fullPath = $fullPath;
}
public function getFullPath(): string
{
return $this->fullPath;
}
public function setUser(User $user): void
{
$this->user = $user;
}
public function getUser(): User
{
return $this->user;
}
public function getExtension(): string
{
return pathinfo($this->name, PATHINFO_EXTENSION);
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function preUpload(): void
{
if (null !== $this->getUploadedFile()) {
$this->md5 = md5_file($this->getUploadedFile()->getPathname());
$this->fullPath = $this->getAbsolutePath();
}
}
#[ORM\PostPersist]
#[ORM\PostUpdate]
public function upload(): void
{
if (null === $this->getUploadedFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getUploadedFile()->move(
self::DEFAULT_UPLOAD_DIRECTORY,
$this->getNewFileName()
);
// Set full path
$this->fullPath = $this->getAbsolutePath();
$this->uploadedFile = null;
}
#[ORM\PreRemove]
public function storeFilenameForRemove(): void
{
$this->temp = $this->getAbsolutePath();
}
#[ORM\PostRemove]
public function removeUpload(): void
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
public function getAbsolutePath(): ?string
{
return null === $this->md5
? null
: '/' . self::DEFAULT_UPLOAD_DIRECTORY . '/' . $this->getNewFileName();
}
public function getNewFileName(): ?string
{
return null === $this->md5
? null
: $this->md5 . '.' . $this->getExtension();
}
}