Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Repo\RepoBundle\Entity;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- use Symfony\Component\Validator\Constraints as Assert;
- /**
- * RepoFile
- * @ORM\Entity(repositoryClass="Repo\RepoBundle\Entity\Repository\RepoFileRepository")
- * @ORM\Table(name="repo_files")
- * @ORM\HasLifecycleCallbacks()
- */
- class RepoFile {
- // a property used temporarily while deleting
- private $filenameForRemove;
- /**
- * @Assert\File(maxSize="6000000")
- */
- public $file;
- /**
- * @var integer
- *
- * @ORM\Column(name="id", type="integer", nullable=false)
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="IDENTITY")
- */
- private $id;
- /**
- * @var string
- *
- * @ORM\Column(name="filename", type="string", length=255, nullable=false)
- */
- private $filename;
- /**
- * @ORM\ManyToOne(targetEntity="User", inversedBy="files")
- * @ORM\JoinColumn(name="author", referencedColumnName="id")
- */
- private $user;
- /**
- * @var string
- *
- * @ORM\Column(name="sha1", type="string", length=50, nullable=false)
- */
- private $sha1;
- /**
- * @var integer
- *
- * @ORM\Column(name="author", type="integer", nullable=false)
- */
- public $author;
- /**
- * @var integer
- *
- * @ORM\Column(name="downloads", type="integer", nullable=false)
- */
- private $downloads;
- /**
- * @var integer
- *
- * @ORM\Column(name="views", type="integer", nullable=false, options={"default" = 0})
- */
- private $views;
- /**
- * @var integer
- *
- * @ORM\Column(name="created", type="integer", nullable=false, options={"default" = 0})
- */
- private $created;
- /**
- * @var integer
- *
- * @ORM\Column(name="updated", type="integer", nullable=false)
- */
- private $updated;
- /**
- * @var integer
- *
- * @ORM\Column(name="votes", type="integer", nullable=false, options={"default" = 0})
- */
- private $votes;
- /**
- * Get id
- *
- * @return integer
- */
- public function getId() {
- return $this->id;
- }
- /**
- * Set filename
- *
- * @param string $filename
- * @return RepoFile
- */
- public function setFilename($filename) {
- $this->filename = $filename;
- return $this;
- }
- /**
- * Get filename
- *
- * @return string
- */
- public function getFilename() {
- return $this->filename;
- }
- /**
- * Set sha1
- *
- * @param string $sha1
- * @return RepoFile
- */
- public function setSha1($sha1) {
- $this->sha1 = $sha1;
- return $this;
- }
- /**
- * Get sha1
- *
- * @return string
- */
- public function getSha1() {
- return $this->sha1;
- }
- /**
- * Set author
- *
- * @param integer $author
- * @return RepoFile
- */
- public function setAuthor($author) {
- $this->author = $author;
- return $this;
- }
- /**
- * Get author
- *
- * @return integer
- */
- public function getAuthor() {
- return $this->author;
- }
- /**
- * Set downloads
- *
- * @param integer $downloads
- * @return RepoFile
- */
- public function setDownloads($downloads) {
- $this->downloads = $downloads;
- return $this;
- }
- /**
- * Get downloads
- *
- * @return integer
- */
- public function getDownloads() {
- return $this->downloads;
- }
- /**
- * Set views
- *
- * @param integer $views
- * @return RepoFile
- */
- public function setViews($views) {
- $this->views = $views;
- return $this;
- }
- /**
- * Get views
- *
- * @return integer
- */
- public function getViews() {
- return $this->views;
- }
- /**
- * Set created
- *
- * @param integer $created
- * @return RepoFile
- */
- public function setCreated($created) {
- $this->created = $created;
- return $this;
- }
- /**
- * Get created
- *
- * @return integer
- */
- public function getCreated() {
- return $this->created;
- }
- /**
- * Set updated
- *
- * @param integer $updated
- * @return RepoFile
- */
- public function setUpdated($updated) {
- $this->updated = $updated;
- return $this;
- }
- /**
- * Get updated
- *
- * @return integer
- */
- public function getUpdated() {
- return $this->updated;
- }
- /**
- * Set votes
- *
- * @param integer $votes
- * @return RepoFile
- */
- public function setVotes($votes) {
- $this->votes = $votes;
- return $this;
- }
- /**
- * Get votes
- *
- * @return integer
- */
- public function getVotes() {
- return $this->votes;
- }
- /**
- * Set user
- *
- * @param \Repo\RepoBundle\Entity\User $user
- * @return RepoFile
- */
- public function setUser(\Repo\RepoBundle\Entity\User $user = null) {
- $this->user = $user;
- return $this;
- }
- /**
- * Get user
- *
- * @return \Repo\RepoBundle\Entity\User
- */
- public function getUser() {
- return $this->user;
- }
- ////////////////////////////
- protected function getUploadRootDir() {
- // the absolute directory path where uploaded
- // documents should be saved
- return __DIR__ . '/../../../../web/' . $this->getUploadDir();
- }
- protected function getUploadDir() {
- // get rid of the __DIR__ so it doesn't screw up
- // when displaying uploaded doc/image in the view.
- return 'uploads/documents';
- }
- // public function __set($name, $value) {
- //
- // echo 'setting ' . $name . ' to ' . $value;
- // $this->$name = $value;
- // return;
- // }
- // public function __unset($name) {
- // echo 'unsetting ' . $name ;
- // unset($this->$name);
- // return;
- // }
- /**
- * @ORM\PrePersist()
- * @ORM\PreUpdate()
- */
- public function preUpload() {
- if (null !== $this->file) {
- $this->path = $this->file->guessExtension();
- $filename = sha1(uniqid(mt_rand(), true));
- $this->filename = $filename . '.' . $this->file->guessExtension();
- $this->sha1 = sha1($this->path);
- }
- $this->updated = date('Y-m-d H:m:s');
- return;
- }
- /**
- * @ORM\PrePersist()
- */
- public function fixValues() {
- $this->created = date('Y-m-d H:m:s');
- return;
- }
- /**
- * @ORM\PostPersist()
- * @ORM\PostUpdate()
- */
- public function upload() {
- if (null === $this->file) {
- return;
- }
- // you must throw an exception here if the file cannot be moved
- // so that the entity is not persisted to the database
- // which the UploadedFile move() method does
- $this->file->move(
- $this->getUploadRootDir(), $this->id . '.' . $this->file->guessExtension()
- );
- unset($this->file);
- return;
- }
- /**
- * @ORM\PreRemove()
- */
- public function storeFilenameForRemove() {
- $this->filenameForRemove = $this->getAbsolutePath();
- }
- /**
- * @ORM\PostRemove()
- */
- public function removeUpload() {
- if ($this->filenameForRemove) {
- unlink($this->filenameForRemove);
- }
- }
- public function getAbsolutePath() {
- return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement