id; } /** * Set authorAvatarPath * * @param string $authorAvatarPath * @return User */ public function setAuthorAvatarPath($authorAvatarPath) { $this->authorAvatarPath = $authorAvatarPath; return $this; } /** * Get authorAvatarPath * * @return string */ public function getAuthorAvatarPath() { return $this->authorAvatarPath; } public function getAvatarFile() { return $this->avatarFile; } public function setAvatarFile(UploadedFile $avatarFile) { $this->avatarFile = $avatarFile; return $this; } // Chemin absolu du fichier public function getAbsolutePath() { return null === $this->authorAvatarPath ? null: $this->getUploadRootDir().'/'.$this->authorAvatarPath; } // Récupérer le chemin relatif d'un fichier uploadé via les templates Twig public function getWebPath() { return null === $this->authorAvatarPath ? null: $this->getUploadDir().'/'.$this->authorAvatarPath; } protected function getUploadRootDir() { return __DIR__.'/../../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { return 'uploads/avatars'; } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { // On crypte en sha1 le nom du fichier avant de le déplacer dans le rep // d'upload final if(null !== $this->avatarFile) { $this->authorAvatarPath = sha1(uniqid(mt_rand(),true)).'.'.$this->avatarFile->guessExtension(); } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if(null === $this->avatarFile) { return; } $this->avatarFile->move($this->getUploadRootDir(),$this->authorAvatarPath); unset($this->avatarFile); } /** * @ORM\PostRemove() */ public function removeUpload() { if($file = $this->getAbsolutePath()) { unlink($file); } } }