1
0

DownloadResponse.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Kate Döen <kate.doeen@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http;
  29. /**
  30. * Prompts the user to download the a file
  31. * @since 7.0.0
  32. * @template S of int
  33. * @template C of string
  34. * @template H of array<string, mixed>
  35. * @template-extends Response<int, array<string, mixed>>
  36. */
  37. class DownloadResponse extends Response {
  38. /**
  39. * Creates a response that prompts the user to download the file
  40. * @param string $filename the name that the downloaded file should have
  41. * @param C $contentType the mimetype that the downloaded file should have
  42. * @param S $status
  43. * @param H $headers
  44. * @since 7.0.0
  45. */
  46. public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
  47. parent::__construct($status, $headers);
  48. $filename = strtr($filename, ['"' => '\\"', '\\' => '\\\\']);
  49. $this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  50. $this->addHeader('Content-Type', $contentType);
  51. }
  52. }