StreamResponse.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http;
  26. /**
  27. * Class StreamResponse
  28. *
  29. * @package OCP\AppFramework\Http
  30. * @since 8.1.0
  31. */
  32. class StreamResponse extends Response implements ICallbackResponse {
  33. /** @var string */
  34. private $filePath;
  35. /**
  36. * @param string|resource $filePath the path to the file or a file handle which should be streamed
  37. * @since 8.1.0
  38. */
  39. public function __construct ($filePath) {
  40. $this->filePath = $filePath;
  41. }
  42. /**
  43. * Streams the file using readfile
  44. *
  45. * @param IOutput $output a small wrapper that handles output
  46. * @since 8.1.0
  47. */
  48. public function callback (IOutput $output) {
  49. // handle caching
  50. if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
  51. if (!(is_resource($this->filePath) || file_exists($this->filePath))) {
  52. $output->setHttpResponseCode(Http::STATUS_NOT_FOUND);
  53. } elseif ($output->setReadfile($this->filePath) === false) {
  54. $output->setHttpResponseCode(Http::STATUS_BAD_REQUEST);
  55. }
  56. }
  57. }
  58. }