ZipResponse.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  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
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCP\AppFramework\Http;
  28. use OC\Streamer;
  29. use OCP\IRequest;
  30. /**
  31. * Public library to send several files in one zip archive.
  32. *
  33. * @since 15.0.0
  34. */
  35. class ZipResponse extends Response implements ICallbackResponse {
  36. /** @var array{internalName: string, resource: resource, size: int, time: int}[] Files to be added to the zip response */
  37. private array $resources = [];
  38. /** @var string Filename that the zip file should have */
  39. private string $name;
  40. private IRequest $request;
  41. /**
  42. * @since 15.0.0
  43. */
  44. public function __construct(IRequest $request, string $name = 'output') {
  45. parent::__construct();
  46. $this->name = $name;
  47. $this->request = $request;
  48. }
  49. /**
  50. * @since 15.0.0
  51. */
  52. public function addResource($r, string $internalName, int $size, int $time = -1) {
  53. if (!\is_resource($r)) {
  54. throw new \InvalidArgumentException('No resource provided');
  55. }
  56. $this->resources[] = [
  57. 'resource' => $r,
  58. 'internalName' => $internalName,
  59. 'size' => $size,
  60. 'time' => $time,
  61. ];
  62. }
  63. /**
  64. * @since 15.0.0
  65. */
  66. public function callback(IOutput $output) {
  67. $size = 0;
  68. $files = count($this->resources);
  69. foreach ($this->resources as $resource) {
  70. $size += $resource['size'];
  71. }
  72. $zip = new Streamer($this->request, $size, $files);
  73. $zip->sendHeaders($this->name);
  74. foreach ($this->resources as $resource) {
  75. $zip->addFileFromStream($resource['resource'], $resource['internalName'], $resource['size'], $resource['time']);
  76. }
  77. $zip->finalize();
  78. }
  79. }