src/Entity/Core/File.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Entity\User\User;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. #[ORM\Table('file')]
  7. #[ORM\Entity]
  8. #[ORM\HasLifecycleCallbacks]
  9. class File
  10. {
  11. public const string DEFAULT_UPLOAD_DIRECTORY = 'uploads';
  12. #[ORM\Column(name: 'id', type: 'integer')]
  13. #[ORM\Id]
  14. #[ORM\GeneratedValue(strategy: 'AUTO')]
  15. private int $id;
  16. #[ORM\Column(name: 'name', type: 'string', length: 255)]
  17. private string $name;
  18. #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
  19. #[ORM\OneToOne(targetEntity: User::class)]
  20. private User $user;
  21. #[ORM\Column(name: 'size', type: 'integer')]
  22. private int $size;
  23. #[ORM\Column(name: 'md5', type: 'string', length: 255)]
  24. private string $md5;
  25. #[ORM\Column(name: 'fullpath', type: 'string', length: 255)]
  26. private string $fullPath;
  27. private ?UploadedFile $uploadedFile;
  28. /**
  29. * Used in case we are overwriting a file, to store the old filepath.
  30. */
  31. private string $temp;
  32. public function __construct()
  33. {
  34. /* Making sure the directory exists */
  35. if (!is_dir(__DIR__ . '/../../../public/' . self::DEFAULT_UPLOAD_DIRECTORY)) {
  36. mkdir(__DIR__ . '/../../../' . self::DEFAULT_UPLOAD_DIRECTORY, 0755, true);
  37. }
  38. }
  39. public function getId(): int
  40. {
  41. return $this->id;
  42. }
  43. public function setName(string $name): File
  44. {
  45. $this->name = $name;
  46. return $this;
  47. }
  48. public function getName(): string
  49. {
  50. return $this->name;
  51. }
  52. public function setSize(int $size): File
  53. {
  54. $this->size = $size;
  55. return $this;
  56. }
  57. public function getSize(): int
  58. {
  59. return $this->size;
  60. }
  61. public function setMd5(string $md5)
  62. {
  63. $this->md5 = $md5;
  64. }
  65. public function getMd5(): string
  66. {
  67. return $this->md5;
  68. }
  69. public function setUploadedFile(?UploadedFile $file = null): void
  70. {
  71. $this->uploadedFile = $file;
  72. $this->size = $file->getSize();
  73. $this->name = $file->getClientOriginalName();
  74. // Check if we have an old file
  75. if (is_file($this->getAbsolutePath())) {
  76. // store the old name to delete after the update
  77. $this->temp = $this->getAbsolutePath();
  78. }
  79. }
  80. public function getUploadedFile(): ?UploadedFile
  81. {
  82. return $this->uploadedFile;
  83. }
  84. public function setFullPath(string $fullPath): void
  85. {
  86. $this->fullPath = $fullPath;
  87. }
  88. public function getFullPath(): string
  89. {
  90. return $this->fullPath;
  91. }
  92. public function setUser(User $user): void
  93. {
  94. $this->user = $user;
  95. }
  96. public function getUser(): User
  97. {
  98. return $this->user;
  99. }
  100. public function getExtension(): string
  101. {
  102. return pathinfo($this->name, PATHINFO_EXTENSION);
  103. }
  104. #[ORM\PrePersist]
  105. #[ORM\PreUpdate]
  106. public function preUpload(): void
  107. {
  108. if (null !== $this->getUploadedFile()) {
  109. $this->md5 = md5_file($this->getUploadedFile()->getPathname());
  110. $this->fullPath = $this->getAbsolutePath();
  111. }
  112. }
  113. #[ORM\PostPersist]
  114. #[ORM\PostUpdate]
  115. public function upload(): void
  116. {
  117. if (null === $this->getUploadedFile()) {
  118. return;
  119. }
  120. // check if we have an old image
  121. if (isset($this->temp)) {
  122. // delete the old image
  123. unlink($this->temp);
  124. // clear the temp image path
  125. $this->temp = null;
  126. }
  127. // you must throw an exception here if the file cannot be moved
  128. // so that the entity is not persisted to the database
  129. // which the UploadedFile move() method does
  130. $this->getUploadedFile()->move(
  131. self::DEFAULT_UPLOAD_DIRECTORY,
  132. $this->getNewFileName()
  133. );
  134. // Set full path
  135. $this->fullPath = $this->getAbsolutePath();
  136. $this->uploadedFile = null;
  137. }
  138. #[ORM\PreRemove]
  139. public function storeFilenameForRemove(): void
  140. {
  141. $this->temp = $this->getAbsolutePath();
  142. }
  143. #[ORM\PostRemove]
  144. public function removeUpload(): void
  145. {
  146. if (isset($this->temp)) {
  147. unlink($this->temp);
  148. }
  149. }
  150. public function getAbsolutePath(): ?string
  151. {
  152. return null === $this->md5
  153. ? null
  154. : '/' . self::DEFAULT_UPLOAD_DIRECTORY . '/' . $this->getNewFileName();
  155. }
  156. public function getNewFileName(): ?string
  157. {
  158. return null === $this->md5
  159. ? null
  160. : $this->md5 . '.' . $this->getExtension();
  161. }
  162. }