allow passing a stream to StreamResponse
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
0ee958595e
commit
4235b18a88
|
@ -48,13 +48,18 @@ class Output implements IOutput {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string|resource $path or file handle
|
||||||
*
|
*
|
||||||
* @return bool false if an error occurred
|
* @return bool false if an error occurred
|
||||||
*/
|
*/
|
||||||
public function setReadfile($path) {
|
public function setReadfile($path) {
|
||||||
|
if (is_resource($path)) {
|
||||||
|
$output = fopen('php://output', 'w');
|
||||||
|
return stream_copy_to_stream($path, $output) > 0;
|
||||||
|
} else {
|
||||||
return @readfile($path);
|
return @readfile($path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $header
|
* @param string $header
|
||||||
|
|
|
@ -39,7 +39,7 @@ interface IOutput {
|
||||||
public function setOutput($out);
|
public function setOutput($out);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string|resource $path or file handle
|
||||||
*
|
*
|
||||||
* @return bool false if an error occurred
|
* @return bool false if an error occurred
|
||||||
* @since 8.1.0
|
* @since 8.1.0
|
||||||
|
|
|
@ -37,7 +37,7 @@ class StreamResponse extends Response implements ICallbackResponse {
|
||||||
private $filePath;
|
private $filePath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filePath the path to the file which should be streamed
|
* @param string|resource $filePath the path to the file or a file handle which should be streamed
|
||||||
* @since 8.1.0
|
* @since 8.1.0
|
||||||
*/
|
*/
|
||||||
public function __construct ($filePath) {
|
public function __construct ($filePath) {
|
||||||
|
@ -54,7 +54,7 @@ class StreamResponse extends Response implements ICallbackResponse {
|
||||||
public function callback (IOutput $output) {
|
public function callback (IOutput $output) {
|
||||||
// handle caching
|
// handle caching
|
||||||
if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
|
if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
|
||||||
if (!file_exists($this->filePath)) {
|
if (!(file_exists($this->filePath) || is_resource($this->filePath))) {
|
||||||
$output->setHttpResponseCode(Http::STATUS_NOT_FOUND);
|
$output->setHttpResponseCode(Http::STATUS_NOT_FOUND);
|
||||||
} elseif ($output->setReadfile($this->filePath) === false) {
|
} elseif ($output->setReadfile($this->filePath) === false) {
|
||||||
$output->setHttpResponseCode(Http::STATUS_BAD_REQUEST);
|
$output->setHttpResponseCode(Http::STATUS_BAD_REQUEST);
|
||||||
|
|
Loading…
Reference in New Issue