src/CoreBundle/Entity/File.php line 15

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use UserBundle\Entity\User;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. /**
  7. * @ORM\Table("file")
  8. * @ORM\Entity
  9. * @ORM\HasLifecycleCallbacks
  10. */
  11. class File
  12. {
  13. const DEFAULT_UPLOAD_DIRECTORY = 'uploads';
  14. /**
  15. * @var int
  16. *
  17. * @ORM\Column(name="id", type="integer")
  18. * @ORM\Id
  19. * @ORM\GeneratedValue(strategy="AUTO")
  20. */
  21. private $id;
  22. /**
  23. * @var string
  24. *
  25. * @ORM\Column(name="name", type="string", length=255)
  26. */
  27. private $name;
  28. /**
  29. * @var User
  30. *
  31. * @ORM\OneToOne(targetEntity="\UserBundle\Entity\User")
  32. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  33. **/
  34. private $user;
  35. /**
  36. * @var int
  37. *
  38. * @ORM\Column(name="size", type="integer")
  39. */
  40. private $size;
  41. /**
  42. * @var string
  43. *
  44. * @ORM\Column(name="md5", type="string", length=255)
  45. */
  46. private $md5;
  47. /**
  48. * @var string
  49. *
  50. * @ORM\Column(name="fullpath", type="string", length=255)
  51. */
  52. private $fullPath;
  53. /**
  54. * @var UploadedFile
  55. */
  56. private $uploadedFile;
  57. /**
  58. * Used in case we are overwriting a file, to store the old filepath.
  59. *
  60. * @var string
  61. */
  62. private $temp;
  63. public function __construct()
  64. {
  65. /* Making sure the directory exists */
  66. if (!is_dir(__DIR__ . '/../../../public/' . self::DEFAULT_UPLOAD_DIRECTORY)) {
  67. mkdir(__DIR__ . '/../../../' . self::DEFAULT_UPLOAD_DIRECTORY, 0755, true);
  68. }
  69. }
  70. /**
  71. * Get id.
  72. *
  73. * @return int
  74. */
  75. public function getId()
  76. {
  77. return $this->id;
  78. }
  79. /**
  80. * Set name.
  81. *
  82. * @param string $name
  83. *
  84. * @return File
  85. */
  86. public function setName($name)
  87. {
  88. $this->name = $name;
  89. return $this;
  90. }
  91. /**
  92. * Get name.
  93. *
  94. * @return string
  95. */
  96. public function getName()
  97. {
  98. return $this->name;
  99. }
  100. /**
  101. * Set size.
  102. *
  103. * @param int $size
  104. *
  105. * @return File
  106. */
  107. public function setSize($size)
  108. {
  109. $this->size = $size;
  110. return $this;
  111. }
  112. /**
  113. * Get size.
  114. *
  115. * @return int
  116. */
  117. public function getSize()
  118. {
  119. return $this->size;
  120. }
  121. /**
  122. * @param string $md5
  123. */
  124. public function setMd5($md5)
  125. {
  126. $this->md5 = $md5;
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getMd5()
  132. {
  133. return $this->md5;
  134. }
  135. /**
  136. * @param UploadedFile|null $file
  137. */
  138. public function setUploadedFile(?UploadedFile $file = null)
  139. {
  140. $this->uploadedFile = $file;
  141. $this->size = $file->getSize();
  142. $this->name = $file->getClientOriginalName();
  143. // Check if we have an old file
  144. if (is_file($this->getAbsolutePath())) {
  145. // store the old name to delete after the update
  146. $this->temp = $this->getAbsolutePath();
  147. }
  148. }
  149. /**
  150. * @return UploadedFile
  151. */
  152. public function getUploadedFile()
  153. {
  154. return $this->uploadedFile;
  155. }
  156. /**
  157. * @param string $fullPath
  158. */
  159. public function setFullPath($fullPath)
  160. {
  161. $this->fullPath = $fullPath;
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function getFullPath()
  167. {
  168. return $this->fullPath;
  169. }
  170. /**
  171. * @param User $user
  172. */
  173. public function setUser($user)
  174. {
  175. $this->user = $user;
  176. }
  177. /**
  178. * @return User
  179. */
  180. public function getUser()
  181. {
  182. return $this->user;
  183. }
  184. /**
  185. * @return string
  186. */
  187. public function getExtension()
  188. {
  189. return pathinfo($this->name, PATHINFO_EXTENSION);
  190. }
  191. /**
  192. * @ORM\PrePersist()
  193. * @ORM\PreUpdate()
  194. */
  195. public function preUpload()
  196. {
  197. if (null !== $this->getUploadedFile()) {
  198. $this->md5 = md5_file($this->getUploadedFile()->getPathname());
  199. $this->fullPath = $this->getAbsolutePath();
  200. }
  201. }
  202. /**
  203. * @ORM\PostPersist()
  204. * @ORM\PostUpdate()
  205. */
  206. public function upload()
  207. {
  208. if (null === $this->getUploadedFile()) {
  209. return;
  210. }
  211. // check if we have an old image
  212. if (isset($this->temp)) {
  213. // delete the old image
  214. unlink($this->temp);
  215. // clear the temp image path
  216. $this->temp = null;
  217. }
  218. // you must throw an exception here if the file cannot be moved
  219. // so that the entity is not persisted to the database
  220. // which the UploadedFile move() method does
  221. $this->getUploadedFile()->move(
  222. self::DEFAULT_UPLOAD_DIRECTORY,
  223. $this->getNewFileName()
  224. );
  225. // Set full path
  226. $this->fullPath = $this->getAbsolutePath();
  227. $this->uploadedFile = null;
  228. }
  229. /**
  230. * @ORM\PreRemove()
  231. */
  232. public function storeFilenameForRemove()
  233. {
  234. $this->temp = $this->getAbsolutePath();
  235. }
  236. /**
  237. * @ORM\PostRemove()
  238. */
  239. public function removeUpload()
  240. {
  241. if (isset($this->temp)) {
  242. unlink($this->temp);
  243. }
  244. }
  245. /**
  246. * @return null|string
  247. */
  248. public function getAbsolutePath()
  249. {
  250. return null === $this->md5
  251. ? null
  252. : '/' . self::DEFAULT_UPLOAD_DIRECTORY . '/' . $this->getNewFileName();
  253. }
  254. /**
  255. * @return null|string
  256. */
  257. public function getNewFileName()
  258. {
  259. return null === $this->md5
  260. ? null
  261. : $this->md5 . '.' . $this->getExtension();
  262. }
  263. }