ZipResponse.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Kate Döen <kate.doeen@nextcloud.com>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCP\AppFramework\Http;
  29. use OC\Streamer;
  30. use OCP\AppFramework\Http;
  31. use OCP\IRequest;
  32. /**
  33. * Public library to send several files in one zip archive.
  34. *
  35. * @since 15.0.0
  36. * @template S of int
  37. * @template H of array<string, mixed>
  38. * @template-extends Response<int, array<string, mixed>>
  39. */
  40. class ZipResponse extends Response implements ICallbackResponse {
  41. /** @var array{internalName: string, resource: resource, size: int, time: int}[] Files to be added to the zip response */
  42. private array $resources = [];
  43. /** @var string Filename that the zip file should have */
  44. private string $name;
  45. private IRequest $request;
  46. /**
  47. * @param S $status
  48. * @param H $headers
  49. * @since 15.0.0
  50. */
  51. public function __construct(IRequest $request, string $name = 'output', int $status = Http::STATUS_OK, array $headers = []) {
  52. parent::__construct($status, $headers);
  53. $this->name = $name;
  54. $this->request = $request;
  55. }
  56. /**
  57. * @since 15.0.0
  58. */
  59. public function addResource($r, string $internalName, int $size, int $time = -1) {
  60. if (!\is_resource($r)) {
  61. throw new \InvalidArgumentException('No resource provided');
  62. }
  63. $this->resources[] = [
  64. 'resource' => $r,
  65. 'internalName' => $internalName,
  66. 'size' => $size,
  67. 'time' => $time,
  68. ];
  69. }
  70. /**
  71. * @since 15.0.0
  72. */
  73. public function callback(IOutput $output) {
  74. $size = 0;
  75. $files = count($this->resources);
  76. foreach ($this->resources as $resource) {
  77. $size += $resource['size'];
  78. }
  79. $zip = new Streamer($this->request, $size, $files);
  80. $zip->sendHeaders($this->name);
  81. foreach ($this->resources as $resource) {
  82. $zip->addFileFromStream($resource['resource'], $resource['internalName'], $resource['size'], $resource['time']);
  83. }
  84. $zip->finalize();
  85. }
  86. }