1
0

FileDisplayResponse.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCP\AppFramework\Http;
  7. use OCP\AppFramework\Http;
  8. use OCP\Files\File;
  9. use OCP\Files\SimpleFS\ISimpleFile;
  10. /**
  11. * Class FileDisplayResponse
  12. *
  13. * @since 11.0.0
  14. * @template S of int
  15. * @template H of array<string, mixed>
  16. * @template-extends Response<int, array<string, mixed>>
  17. */
  18. class FileDisplayResponse extends Response implements ICallbackResponse {
  19. /** @var File|ISimpleFile */
  20. private $file;
  21. /**
  22. * FileDisplayResponse constructor.
  23. *
  24. * @param File|ISimpleFile $file
  25. * @param S $statusCode
  26. * @param H $headers
  27. * @since 11.0.0
  28. */
  29. public function __construct(File|ISimpleFile $file, int $statusCode = Http::STATUS_OK, array $headers = []) {
  30. parent::__construct($statusCode, $headers);
  31. $this->file = $file;
  32. $this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
  33. $this->setETag($file->getEtag());
  34. $lastModified = new \DateTime();
  35. $lastModified->setTimestamp($file->getMTime());
  36. $this->setLastModified($lastModified);
  37. }
  38. /**
  39. * @param IOutput $output
  40. * @since 11.0.0
  41. */
  42. public function callback(IOutput $output) {
  43. if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
  44. $output->setHeader('Content-Length: ' . $this->file->getSize());
  45. $output->setOutput($this->file->getContent());
  46. }
  47. }
  48. }