95239ad21e
First stab at the StreamResponse, see #12988 The idea is to use an interface ICallbackResponse (I'm not 100% happy with the name yet, suggestions?) that allow the response to output things in its own way, for instance stream the file using readfile Unittests are atm lacking, plan is to check if a mock of ICallbackResponse will be used by calling its callback (also unhappy with this name) method Usage is: $response = new StreamResponse('path/to/file'); rename io to output, add additional methods and handle error and not modified cases when using StreamResponse fix indention and uppercasing, also handle forbidden cases fix indention fix indention no forbidden, figuring out if a file is really readable is too complicated to get to work across OSes and streams remove useless import remove useless import fix intendation
48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
/**
|
|
* @author Bernhard Posselt
|
|
* @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.com>
|
|
*
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
* later.
|
|
* See the COPYING-README file.
|
|
*/
|
|
|
|
namespace OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
/**
|
|
* Class StreamResponse
|
|
*
|
|
* @package OCP\AppFramework\Http
|
|
*/
|
|
class StreamResponse extends Response implements ICallbackResponse {
|
|
/** @var string */
|
|
private $filePath;
|
|
|
|
/**
|
|
* @param string $filePath the path to the file which should be streamed
|
|
*/
|
|
public function __construct ($filePath) {
|
|
$this->filePath = $filePath;
|
|
}
|
|
|
|
|
|
/**
|
|
* Streams the file using readfile
|
|
*
|
|
* @param IOutput a small wrapper that handles output
|
|
*/
|
|
public function callback (IOutput $output) {
|
|
// handle caching
|
|
if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
|
|
if (!file_exists($this->filePath)) {
|
|
$output->setHttpResponseCode(Http::STATUS_NOT_FOUND);
|
|
} elseif ($output->setReadfile($this->filePath) === false) {
|
|
$output->setHttpResponseCode(Http::STATUS_BAD_REQUEST);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|