1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- declare(strict_types=1);
- namespace OCP\AppFramework\Http;
- use OC\Streamer;
- use OCP\AppFramework\Http;
- use OCP\IRequest;
- class ZipResponse extends Response implements ICallbackResponse {
-
- private array $resources = [];
-
- private string $name;
- private IRequest $request;
-
- public function __construct(IRequest $request, string $name = 'output', int $status = Http::STATUS_OK, array $headers = []) {
- parent::__construct($status, $headers);
- $this->name = $name;
- $this->request = $request;
- }
-
- public function addResource($r, string $internalName, int $size, int $time = -1) {
- if (!\is_resource($r)) {
- throw new \InvalidArgumentException('No resource provided');
- }
- $this->resources[] = [
- 'resource' => $r,
- 'internalName' => $internalName,
- 'size' => $size,
- 'time' => $time,
- ];
- }
-
- public function callback(IOutput $output) {
- $size = 0;
- $files = count($this->resources);
- foreach ($this->resources as $resource) {
- $size += $resource['size'];
- }
- $zip = new Streamer($this->request, $size, $files);
- $zip->sendHeaders($this->name);
- foreach ($this->resources as $resource) {
- $zip->addFileFromStream($resource['resource'], $resource['internalName'], $resource['size'], $resource['time']);
- }
- $zip->finalize();
- }
- }
|