<?php
namespace CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Table("file")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class File
{
const DEFAULT_UPLOAD_DIRECTORY = 'uploads';
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var User
*
* @ORM\OneToOne(targetEntity="\UserBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
**/
private $user;
/**
* @var int
*
* @ORM\Column(name="size", type="integer")
*/
private $size;
/**
* @var string
*
* @ORM\Column(name="md5", type="string", length=255)
*/
private $md5;
/**
* @var string
*
* @ORM\Column(name="fullpath", type="string", length=255)
*/
private $fullPath;
/**
* @var UploadedFile
*/
private $uploadedFile;
/**
* Used in case we are overwriting a file, to store the old filepath.
*
* @var string
*/
private $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);
}
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name.
*
* @param string $name
*
* @return File
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set size.
*
* @param int $size
*
* @return File
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size.
*
* @return int
*/
public function getSize()
{
return $this->size;
}
/**
* @param string $md5
*/
public function setMd5($md5)
{
$this->md5 = $md5;
}
/**
* @return string
*/
public function getMd5()
{
return $this->md5;
}
/**
* @param UploadedFile|null $file
*/
public function setUploadedFile(?UploadedFile $file = null)
{
$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();
}
}
/**
* @return UploadedFile
*/
public function getUploadedFile()
{
return $this->uploadedFile;
}
/**
* @param string $fullPath
*/
public function setFullPath($fullPath)
{
$this->fullPath = $fullPath;
}
/**
* @return string
*/
public function getFullPath()
{
return $this->fullPath;
}
/**
* @param User $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @return string
*/
public function getExtension()
{
return pathinfo($this->name, PATHINFO_EXTENSION);
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getUploadedFile()) {
$this->md5 = md5_file($this->getUploadedFile()->getPathname());
$this->fullPath = $this->getAbsolutePath();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
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()
{
$this->temp = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
/**
* @return null|string
*/
public function getAbsolutePath()
{
return null === $this->md5
? null
: '/' . self::DEFAULT_UPLOAD_DIRECTORY . '/' . $this->getNewFileName();
}
/**
* @return null|string
*/
public function getNewFileName()
{
return null === $this->md5
? null
: $this->md5 . '.' . $this->getExtension();
}
}