Created
June 9, 2012 17:03
-
-
Save enricostano/2901810 to your computer and use it in GitHub Desktop.
Attachment Entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace StanoSas\Bundle\DocumentBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
use Symfony\Component\HttpFoundation\File\File; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
/** | |
* @ORM\Entity | |
* @ORM\HasLifecycleCallbacks | |
*/ | |
class Attachment | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\Column(name="name", type="string", length=255) | |
* @Assert\NotBlank | |
*/ | |
private $name; | |
/** | |
* @Gedmo\Timestampable(on="create") | |
* @ORM\Column(name="created", type="datetime") | |
*/ | |
private $created; | |
/** | |
* @ORM\Column(name="updated", type="datetime") | |
* @Gedmo\Timestampable(on="update") | |
*/ | |
private $updated; | |
/** | |
* @ORM\Column(type="string", length=255, nullable=true) | |
*/ | |
private $path; | |
public function getAbsolutePath() | |
{ | |
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; | |
} | |
public function getWebPath() | |
{ | |
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; | |
} | |
protected function getUploadRootDir() | |
{ | |
// the absolute directory path where uploaded documents should be saved | |
// la cartella fuori dalla webroot è inclusa in opendir in php.ini | |
//return __DIR__.'/../../../../web/'.$this->getUploadDir(); | |
return '/srv/'.$this->getUploadDir(); | |
} | |
protected function getUploadDir() | |
{ | |
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view. | |
return 'uploads/documents'; | |
} | |
/** | |
* @Assert\File(maxSize="10000000", | |
* mimeTypes = {"application/pdf", "application/x-pdf"}, | |
* mimeTypesMessage = "Please upload a valid PDF" | |
* ) | |
*/ | |
private $file; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set path | |
* | |
* @param string $path | |
*/ | |
public function setPath($path) | |
{ | |
$this->path = $path; | |
} | |
/** | |
* Get path | |
* | |
* @return string | |
*/ | |
public function getPath() | |
{ | |
return $this->path; | |
} | |
/** | |
* @ORM\PrePersist() | |
* @ORM\PreUpdate() | |
*/ | |
public function preUpload() | |
{ | |
if (null !== $this->file) { | |
// do whatever you want to generate a unique name | |
$this->path = $this->file->getClientOriginalName(); | |
// add suffix _n to filename in order to avoid duplicates | |
$count = 0; | |
while (file_exists($this->getAbsolutePath())) { | |
$count++; | |
$filename = substr($this->file->getClientOriginalName(), 0, -4); | |
$this->path = $filename.'_'.$count.'.'.$this->file->guessExtension(); | |
} | |
} | |
} | |
/** | |
* @ORM\PostPersist() | |
* @ORM\PostUpdate() | |
*/ | |
public function upload() | |
{ | |
if (null === $this->file) { | |
return; | |
} | |
// if there is an error when moving the file, an exception will | |
// be automatically thrown by move(). This will properly prevent | |
// the entity from being persisted to the database on error | |
$this->file->move($this->getUploadRootDir(), $this->path); | |
unset($this->file); | |
} | |
/** | |
* @ORM\PostRemove() | |
*/ | |
public function removeUpload() | |
{ | |
if ($file = $this->getAbsolutePath()) { | |
unlink($file); | |
} | |
} | |
/** | |
* Set created | |
* | |
* @param datetime $created | |
*/ | |
public function setCreated($created) | |
{ | |
$this->created = $created; | |
} | |
/** | |
* Get created | |
* | |
* @return datetime | |
*/ | |
public function getCreated() | |
{ | |
return $this->created; | |
} | |
/** | |
* Set updated | |
* | |
* @param datetime $updated | |
*/ | |
public function setUpdated($updated) | |
{ | |
$this->updated = $updated; | |
} | |
/** | |
* Get updated | |
* | |
* @return datetime | |
*/ | |
public function getUpdated() | |
{ | |
return $this->updated; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* Get name | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment