Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Lddt\UserBundle\Entity;
- use Doctrine\ORM\Mapping as ORM;
- use FOS\UserBundle\Entity\User as BaseUser;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- /**
- * User
- *
- * @ORM\Table()
- * @ORM\Entity(repositoryClass="Lddt\UserBundle\Entity\UserRepository")
- * @ORM\HasLifecycleCallbacks
- */
- class User extends BaseUser
- {
- /**
- * @var integer
- *
- * @ORM\Column(name="id", type="integer")
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * @var string
- *
- * @ORM\Column(name="authorAvatarPath", type="string", length=255, nullable=true)
- */
- private $authorAvatarPath;
- /**
- *
- * @Assert\File(
- * maxSize="1024k",
- * maxSizeMessage="Votre fichier ne doit pas dépasser 1Mo",
- * mimeTypes ={"image/jpeg","image/png","image/gif"},
- * mimeTypesMessage = "Merci d'uploader un fichier jpg, png ou gif"
- * )
- */
- private $avatarFile;
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement