雰囲気こんな感じ。
/**
* @Route("/media/{id}.{_format}", name="hoge_fuga_admin_media_show", defaults={"_format" = "json"})
* @View(serializerEnableMaxDepthChecks=true, serializerGroups={"adminShow"})
* @ParamConverter("entity", class="HogeFugaBundle:Image")
* @Method("GET")
*/
public function showAction(Image $entity, Request $request, $id)
{
$path = $this->getStragePath($entity->getPath()).'/'.$entity->getPath();
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$response = new Response();
$response->headers->set('Content-type', $finfo->file($path));
$response->sendHeaders();
$response->setContent(readfile($path));
return $response;
}
ちなみにgetStragePath()の実装はこんな感じ。
※upload_root_dirをparameters.ymlで定義しとく。
/**
* create dir and get file path by file name
*
* @param unknown $fileName
* @throws \Symfony\Component\HttpFoundation\File\Exception\UploadException
* @return string
*/
private function getStragePath($fileName)
{
$fs = new Filesystem();
$path = substr($fileName, 0, 1).'/'.substr($fileName, 0, 2);
$dir = $this->get('kernel')->getRootDir().$this->container->getParameter('upload_root_dir').'/'.$path;
if (!$fs->exists($dir)) {
try {
$fs->mkdir($dir, 0777);
} catch (IOException $e) {
throw new \Symfony\Component\HttpFoundation\File\Exception\UploadException('Cannot create directory.');
}
}
return $dir;
}
ちなみにImageエンティティはこんな感じ。
namespace Hoge\FugaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Image
*
* @ORM\Table(name="images")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
* @ORM\Entity(repositoryClass="Hoge\FugaBundle\Repository\ImageRepository")
*/
class Image
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Groups({"adminIndex", "adminShow"})
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @JMS\Groups({"adminIndex", "adminShow"})
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
* @JMS\Groups({"adminIndex", "adminShow"})
*/
private $path;
/**
* @var integer
*
* @ORM\Column(name="size", type="integer")
* @JMS\Groups({"adminIndex", "adminShow"})
*/
private $size;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Image
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path
*
* @param string $path
* @return Image
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set size
*
* @param integer $size
* @return Image
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size
*
* @return integer
*/
public function getSize()
{
return $this->size;
}
}