vendor/roothirsch/dam-bundle/Entity/File.php line 19

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\Entity;
  3. use Roothirsch\CoreBundle\Translation\Entity\Language;
  4. use Roothirsch\DamBundle\Repository\FileRepository;
  5. use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use \Roothirsch\DamBundle\Entity\Asset;
  8. use ApiPlatform\Core\Annotation\ApiResource;
  9. use Symfony\Component\Finder\SplFileInfo;
  10. /**
  11. * @ORM\Entity(repositoryClass=FileRepository::class)
  12. * @ORM\Table(name="dam_file")
  13. * @ApiResource( shortName="Dam/File")
  14. * @ORM\HasLifecycleCallbacks()
  15. */
  16. class File
  17. {
  18. /**
  19. * @ORM\Id
  20. * @ORM\GeneratedValue
  21. * @ORM\Column(type="integer")
  22. */
  23. private $id;
  24. /**
  25. * @ORM\Column(type="string", length=255)
  26. */
  27. private $name;
  28. /**
  29. * @ORM\Column(type="string", length=255)
  30. */
  31. private $title;
  32. /**
  33. * @ORM\Column(type="text")
  34. */
  35. private $contentSummary = '';
  36. /**
  37. * @ORM\Column(type="text")
  38. */
  39. private $filepath;
  40. /**
  41. */
  42. private $oldFilepath;
  43. /**
  44. * @ORM\Column(type="integer")
  45. */
  46. private $size;
  47. /**
  48. * @ORM\Column(type="string", length=255)
  49. */
  50. private $extension;
  51. /**
  52. * @var \DateTime $created
  53. * @ORM\Column(type="datetime")
  54. */
  55. private $createdAt;
  56. /**
  57. * @var \DateTime $updated
  58. * @ORM\Column(type="datetime")
  59. */
  60. private $updatedAt;
  61. /**
  62. * @ORM\ManyToOne(targetEntity=Asset::class, inversedBy="files")
  63. * @ORM\JoinColumn(nullable=false)
  64. */
  65. private $asset;
  66. /**
  67. * @ORM\ManyToOne(targetEntity=Language::class)
  68. */
  69. private $language;
  70. public function __construct(SplFileInfo $fileInfo = null)
  71. {
  72. if ($fileInfo instanceof \SplFileInfo) {
  73. $this->name = $fileInfo->getFilename();
  74. $this->title = str_replace('_', ' ', $fileInfo->getFilenameWithoutExtension());
  75. $this->filepath = $fileInfo->getRelativePathname();
  76. $this->size = $fileInfo->getSize();
  77. $this->extension = $fileInfo->getExtension();
  78. $this->createdAt = new \DateTime('@' . $fileInfo->getMTime());
  79. $this->updatedAt = new \DateTime('@' . $fileInfo->getCTime());
  80. }
  81. }
  82. public function __toString()
  83. {
  84. return strval($this->name);
  85. }
  86. /**
  87. * @ORM\PostLoad
  88. */
  89. public function cacheFilepath()
  90. {
  91. $this->oldFilepath = $this->filepath;
  92. }
  93. /**
  94. * @ORM\PrePersist
  95. * @ORM\PreUpdate
  96. */
  97. public function updateFileInformation($copyInsteadOfRename = false)
  98. {
  99. if (!file_exists(getcwd() . '/uploads/dam/' . $this->filepath)) {
  100. return;
  101. }
  102. $fileInfo = new SplFileInfo(
  103. getcwd() . '/uploads/dam/' . $this->filepath,
  104. getcwd() . '/uploads/dam/',
  105. $this->filepath
  106. );
  107. $this->name = $fileInfo->getFilename();
  108. $this->title = str_replace('_', ' ', $fileInfo->getFilenameWithoutExtension());
  109. $this->size = $fileInfo->getSize();
  110. $this->extension = $fileInfo->getExtension();
  111. $this->createdAt = new \DateTime('@' . $fileInfo->getMTime());
  112. $updatedAt = new \DateTime('@' . $fileInfo->getCTime());
  113. if ($updatedAt > $this->updatedAt || empty($this->contentSummary)) {
  114. switch ($this->extension) {
  115. case 'pdf':
  116. try {
  117. $parser = new \Smalot\PdfParser\Parser();
  118. $pdf = $parser->parseFile(getcwd() . '/uploads/dam/' . $this->filepath);
  119. $this->contentSummary = $pdf->getText();
  120. } catch(\Exception $e) {
  121. }
  122. }
  123. }
  124. $this->updatedAt = $updatedAt;
  125. }
  126. public function getId(): ?int
  127. {
  128. return $this->id;
  129. }
  130. public function getName(): ?string
  131. {
  132. return $this->name;
  133. }
  134. public function setName(string $name): self
  135. {
  136. $this->name = $name;
  137. return $this;
  138. }
  139. public function getTitle(): ?string
  140. {
  141. return $this->title;
  142. }
  143. public function setTitle(string $title): self
  144. {
  145. $this->title = $title;
  146. return $this;
  147. }
  148. public function getFilepath(): ?string
  149. {
  150. return $this->filepath;
  151. }
  152. public function setFilepath($filepath): self
  153. {
  154. $this->filepath = $filepath;
  155. return $this;
  156. }
  157. public function getSize(): ?int
  158. {
  159. return $this->size;
  160. }
  161. public function setSize(int $size): self
  162. {
  163. $this->size = $size;
  164. return $this;
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function getExtension(): string
  170. {
  171. return $this->extension;
  172. }
  173. /**
  174. * @param string $extension
  175. */
  176. public function setExtension(string $extension): void
  177. {
  178. $this->extension = $extension;
  179. }
  180. public function getAsset(): ?Asset
  181. {
  182. return $this->asset;
  183. }
  184. public function setAsset(?Asset $asset): self
  185. {
  186. $this->asset = $asset;
  187. return $this;
  188. }
  189. /**
  190. * @return \DateTime
  191. */
  192. public function getCreatedAt(): \DateTime
  193. {
  194. return $this->createdAt;
  195. }
  196. /**
  197. * @param \DateTime $createdAt
  198. */
  199. public function setCreatedAt(\DateTime $createdAt): void
  200. {
  201. $this->createdAt = $createdAt;
  202. }
  203. /**
  204. * @return \DateTime
  205. */
  206. public function getUpdatedAt(): \DateTime
  207. {
  208. return $this->updatedAt;
  209. }
  210. /**
  211. * @param \DateTime $updatedAt
  212. */
  213. public function setUpdatedAt(\DateTime $updatedAt): void
  214. {
  215. $this->updatedAt = $updatedAt;
  216. }
  217. /**
  218. * @return mixed
  219. */
  220. public function getLanguage()
  221. {
  222. return $this->language;
  223. }
  224. /**
  225. * @param mixed $language
  226. */
  227. public function setLanguage($language): void
  228. {
  229. $this->language = $language;
  230. }
  231. /**
  232. * @return mixed
  233. */
  234. public function getContentSummary()
  235. {
  236. return $this->contentSummary;
  237. }
  238. /**
  239. * @param mixed $contentSummary
  240. */
  241. public function setContentSummary($contentSummary): void
  242. {
  243. $this->contentSummary = $contentSummary;
  244. }
  245. }