Files
Phraseanet/lib/Alchemy/Phrasea/Http/ServeFileResponseFactory.php
Romain Neutron d7f7e1c92c Fix CS
2014-01-14 12:04:38 +01:00

82 lines
2.1 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Http;
use Alchemy\Phrasea\Application;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ServeFileResponseFactory implements DeliverDataInterface
{
private $unicode;
public function __construct(\unicode $unicode)
{
$this->unicode = $unicode;
}
/**
* @param Application $app
* @return self
*/
public static function create(Application $app)
{
return new self(
$app['unicode']
);
}
/**
* {@inheritdoc}
*/
public function deliverFile($file, $filename = '', $disposition = self::DISPOSITION_INLINE, $mimeType = null ,$cacheDuration = 0)
{
$response = new BinaryFileResponse($file);
$response->setContentDisposition($disposition, $this->sanitizeFilename($filename), $this->sanitizeFilenameFallback($filename));
$response->setMaxAge($cacheDuration);
$response->setPrivate();
if (null !== $mimeType) {
$response->headers->set('Content-Type', $mimeType);
}
return $response;
}
/**
* {@inheritdoc}
*/
public function deliverData($data, $filename, $mimeType, $disposition = self::DISPOSITION_INLINE, $cacheDuration = 0)
{
$response = new Response($data);
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
$disposition,
$this->sanitizeFilename($filename),
$this->sanitizeFilenameFallback($filename
)));
$response->headers->set('Content-Type', $mimeType);
$response->setMaxAge($cacheDuration);
return $response;
}
private function sanitizeFilename($filename)
{
return str_replace(['/', '\\'], '', $filename);
}
private function sanitizeFilenameFallback($filename)
{
return $this->unicode->remove_nonazAZ09($filename, true, true, true);
}
}