Skip to content

Instantly share code, notes, and snippets.

@frak
Created November 28, 2012 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frak/4164820 to your computer and use it in GitHub Desktop.
Save frak/4164820 to your computer and use it in GitHub Desktop.
Handling file uploads with lifecycle callbacks
<?php
namespace Click\SiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Click\SiteBundle\Entity\Episode
* Cruft has been removed for clarity
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Click\SiteBundle\Repository\EpisodeRepository")
* @ORM\HasLifecycleCallbacks
*/
class Episode
{
/**
* @var string
*/
private $audioFileForRemove;
/**
* @var string
*/
private $imageFileForRemove;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $enclosure
*
* @ORM\Column(name="enclosure", type="string", length=255)
*/
private $enclosure;
/**
* @var string $image
*
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @var integer $length
*
* @ORM\Column(name="length", type="integer")
*/
private $length;
/**
* @var string $duration
*
* @ORM\Column(name="duration", type="string", length=45)
*/
private $duration;
/**
* @var UploadedFile
*
* @Assert\File(maxSize="314572800")
*/
public $audioUpload;
/**
* @var UploadedFile
*
* @Assert\File(maxSize="2097152")
*/
public $imageUpload;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set enclosure
*
* @param string $enclosure
*
* @return Episode
*/
public function setEnclosure($enclosure)
{
$this->enclosure = $enclosure;
return $this;
}
/**
* Get enclosure
*
* @return string
*/
public function getEnclosure()
{
return $this->enclosure;
}
/**
* Set image
*
* @param string $image
*
* @return Episode
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set length
*
* @param integer $length
*
* @return Episode
*/
public function setLength($length)
{
$this->length = $length;
return $this;
}
/**
* Get length
*
* @return integer
*/
public function getLength()
{
return $this->length;
}
/**
* Set duration
*
* @param string $duration
*
* @return Episode
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
/**
* Get duration
*
* @return string
*/
public function getDuration()
{
return $this->duration;
}
/**
* Absolute path to the uploaded audio file
*
* @return string
*/
public function getAbsoluteAudioPath()
{
return $this->getUploadRootDir().'/'.$this->getId().'.mp3';
}
/**
* Absolute path to the uploaded image file
*
* @return string
*/
public function getAbsoluteImagePath()
{
return $this->getUploadRootDir().'/'.$this->getId().'.jpg';
}
/**
* Download URL, same as the enclosure
*
* @return string
*/
public function getWebAudioPath()
{
return $this->getEnclosure();
}
/**
* Download URL, same as the enclosure
*
* @return string
*/
public function getWebImagePath()
{
return '/std-dev/images/' . $this->getId();
}
/**
* Where to upload new files to
*
* @return string
*/
protected function getUploadRootDir()
{
return __DIR__.'/../../../../'.$this->getUploadDir();
}
/**
* Folder suffix
*
* @return string
*/
protected function getUploadDir()
{
return 'media';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (!is_null($this->audioUpload)) {
$this->enclosure = $this->audioUpload->guessExtension();
$this->length = $this->audioUpload->getSize();
$cmd = "/usr/bin/mp3info -p '%S' {$this->audioUpload->getRealPath()}";
$duration = `$cmd`;
$this->duration = gmdate("H:i:s", $duration);
}
if (!is_null($this->imageUpload)) {
$this->image = $this->imageUpload->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (!is_null($this->audioUpload)) {
$this->audioUpload->move($this->getUploadRootDir(), $this->id.'.mp3');
unset($this->audioUpload);
}
if (!is_null($this->imageUpload)) {
$this->imageUpload->move($this->getUploadRootDir(), $this->id.'.jpg');
unset($this->imageUpload);
}
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->imageFileForRemove = $this->getAbsoluteImagePath();
$this->audioFileForRemove = $this->getAbsoluteAudioPath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->imageFileForRemove) {
unlink($this->imageFileForRemove);
}
if ($this->audioFileForRemove) {
unlink($this->audioFileForRemove);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment