<?php
namespace Roothirsch\DamBundle\Entity;
use Roothirsch\CoreBundle\Translation\Entity\Language;
use Roothirsch\DamBundle\Repository\FileRepository;
use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
use Doctrine\ORM\Mapping as ORM;
use \Roothirsch\DamBundle\Entity\Asset;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Finder\SplFileInfo;
/**
* @ORM\Entity(repositoryClass=FileRepository::class)
* @ORM\Table(name="dam_file")
* @ApiResource( shortName="Dam/File")
* @ORM\HasLifecycleCallbacks()
*/
class File
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $contentSummary = '';
/**
* @ORM\Column(type="text")
*/
private $filepath;
/**
*/
private $oldFilepath;
/**
* @ORM\Column(type="integer")
*/
private $size;
/**
* @ORM\Column(type="string", length=255)
*/
private $extension;
/**
* @var \DateTime $created
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @var \DateTime $updated
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=Asset::class, inversedBy="files")
* @ORM\JoinColumn(nullable=false)
*/
private $asset;
/**
* @ORM\ManyToOne(targetEntity=Language::class)
*/
private $language;
public function __construct(SplFileInfo $fileInfo = null)
{
if ($fileInfo instanceof \SplFileInfo) {
$this->name = $fileInfo->getFilename();
$this->title = str_replace('_', ' ', $fileInfo->getFilenameWithoutExtension());
$this->filepath = $fileInfo->getRelativePathname();
$this->size = $fileInfo->getSize();
$this->extension = $fileInfo->getExtension();
$this->createdAt = new \DateTime('@' . $fileInfo->getMTime());
$this->updatedAt = new \DateTime('@' . $fileInfo->getCTime());
}
}
public function __toString()
{
return strval($this->name);
}
/**
* @ORM\PostLoad
*/
public function cacheFilepath()
{
$this->oldFilepath = $this->filepath;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updateFileInformation($copyInsteadOfRename = false)
{
if (!file_exists(getcwd() . '/uploads/dam/' . $this->filepath)) {
return;
}
$fileInfo = new SplFileInfo(
getcwd() . '/uploads/dam/' . $this->filepath,
getcwd() . '/uploads/dam/',
$this->filepath
);
$this->name = $fileInfo->getFilename();
$this->title = str_replace('_', ' ', $fileInfo->getFilenameWithoutExtension());
$this->size = $fileInfo->getSize();
$this->extension = $fileInfo->getExtension();
$this->createdAt = new \DateTime('@' . $fileInfo->getMTime());
$updatedAt = new \DateTime('@' . $fileInfo->getCTime());
if ($updatedAt > $this->updatedAt || empty($this->contentSummary)) {
switch ($this->extension) {
case 'pdf':
try {
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile(getcwd() . '/uploads/dam/' . $this->filepath);
$this->contentSummary = $pdf->getText();
} catch(\Exception $e) {
}
}
}
$this->updatedAt = $updatedAt;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getFilepath(): ?string
{
return $this->filepath;
}
public function setFilepath($filepath): self
{
$this->filepath = $filepath;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
/**
* @return string
*/
public function getExtension(): string
{
return $this->extension;
}
/**
* @param string $extension
*/
public function setExtension(string $extension): void
{
$this->extension = $extension;
}
public function getAsset(): ?Asset
{
return $this->asset;
}
public function setAsset(?Asset $asset): self
{
$this->asset = $asset;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
*/
public function setUpdatedAt(\DateTime $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* @return mixed
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param mixed $language
*/
public function setLanguage($language): void
{
$this->language = $language;
}
/**
* @return mixed
*/
public function getContentSummary()
{
return $this->contentSummary;
}
/**
* @param mixed $contentSummary
*/
public function setContentSummary($contentSummary): void
{
$this->contentSummary = $contentSummary;
}
}