1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 14:00:31 +02:00

[ticket/14285] Use StreamedResponse

PHPBB3-14285
This commit is contained in:
Rubén Calvo
2018-07-04 23:42:09 +02:00
committed by rubencm
parent 7b678fdbfa
commit 05c44e74b3
3 changed files with 60 additions and 47 deletions

View File

@@ -17,10 +17,10 @@ use phpbb\cache\service;
use phpbb\db\driver\driver_interface;
use phpbb\exception\http_exception;
use phpbb\storage\storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class controller
{
/** @var service */
protected $cache;
@@ -30,11 +30,15 @@ class controller
/** @var storage */
protected $storage;
/** @var StreamedResponse */
protected $response;
public function __construct(service $cache, driver_interface $db, storage $storage)
{
$this->cache = $cache;
$this->db = $db;
$this->storage = $storage;
$this->response = new StreamedResponse();
}
public function handle($file)
@@ -50,6 +54,8 @@ class controller
}
$this->send($file);
return $this->response->send();
}
protected function is_allowed($file)
@@ -64,44 +70,43 @@ class controller
protected function send($file)
{
if (!headers_sent())
$this->response->setPublic();
$file_info = $this->storage->file_info($file);
try
{
header('Cache-Control: public');
$this->response->headers->set('Content-Type', $file_info->mimetype);
}
catch (\phpbb\storage\exception\exception $e)
{
// Just don't send this header
}
$file_info = $this->storage->file_info($file);
try
{
$this->response->headers->set('Content-Length', $file_info->size);
}
catch (\phpbb\storage\exception\exception $e)
{
// Just don't send this header
}
try
{
header('Content-Type: ' . $file_info->mimetype);
}
catch (\phpbb\storage\exception\exception $e)
{
// Just don't send this header
}
@set_time_limit(0);
try
{
header('Content-Length: ' . $file_info->size);
}
catch (\phpbb\storage\exception\exception $e)
{
// Just don't send this header
}
$fp = $this->storage->read_stream($file);
$fp = $this->storage->read_stream($file);
// Close db connection
$this->file_gc();
// Close db connection
$this->file_gc();
$output = fopen('php://output', 'w+b');
$output = fopen('php://output', 'w+b');
$this->response->setCallback(function () use ($fp, $output) {
stream_copy_to_stream($fp, $output);
fclose($fp);
fclose($output);
flush();
}
});
}
/**