Skip to content

Instantly share code, notes, and snippets.

@roniemicro
Last active December 5, 2015 08:46
Show Gist options
  • Save roniemicro/bb18bc0d5632b2a37ba0 to your computer and use it in GitHub Desktop.
Save roniemicro/bb18bc0d5632b2a37ba0 to your computer and use it in GitHub Desktop.
Uploadable extention example
stof_doctrine_extensions:
default_locale: en_US
uploadable: ~
class:
uploadable: Core\UserBundle\Listener\UploadableListener
orm:
default:
uploadable: true
<?php
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
*
* @ORM\Table()
* @ORM\Entity()
* @Gedmo\Uploadable(pathMethod="getUploadRootDir", allowOverwrite=false, appendNumber=true)
*/
class Entity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Gedmo\UploadableFileName
* @ORM\Column(name="path", type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="8388608")
*/
protected $file;
/**
* Sets file.
*
* @param Page $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir(). DIRECTORY_SEPARATOR . $this->path;
}
protected function getUploadRootDir()
{
return WEB_PATH . DIRECTORY_SEPARATOR . $this->getUploadDir();
}
protected function getUploadDir()
{
return 'uploads/files';
}
}
<?php
namespace AppBundle\Listener;
use Gedmo\Uploadable\UploadableListener as BaseListener,
Gedmo\Uploadable\FileInfo\FileInfoInterface;
class UploadableListener extends BaseListener
{
public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false, $object)
{
$info = parent::moveFile($fileInfo, $path, $filenameGeneratorClass, $overwrite, $appendNumber, $object);
$info['fileName'] = basename($info['filePath']);
return $info;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment