2018-09-02 09:48:13 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-10-02 02:16:28 -04:00
|
|
|
declare(strict_types=1);
|
2018-09-02 09:48:13 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-09-02 09:48:13 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
|
|
use OC\Streamer;
|
2023-06-14 02:56:42 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2025-08-14 04:01:33 -04:00
|
|
|
use OCP\IDateTimeZone;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IRequest;
|
2018-09-02 09:48:13 -04:00
|
|
|
|
2018-10-02 16:35:31 -04:00
|
|
|
/**
|
|
|
|
|
* Public library to send several files in one zip archive.
|
|
|
|
|
*
|
|
|
|
|
* @since 15.0.0
|
2024-12-16 10:20:48 -05:00
|
|
|
* @template S of Http::STATUS_*
|
2023-06-14 02:56:42 -04:00
|
|
|
* @template H of array<string, mixed>
|
2024-12-16 10:20:48 -05:00
|
|
|
* @template-extends Response<Http::STATUS_*, array<string, mixed>>
|
2018-10-02 16:35:31 -04:00
|
|
|
*/
|
2018-09-02 09:48:13 -04:00
|
|
|
class ZipResponse extends Response implements ICallbackResponse {
|
2022-05-18 17:01:21 -04:00
|
|
|
/** @var array{internalName: string, resource: resource, size: int, time: int}[] Files to be added to the zip response */
|
2022-05-13 05:10:42 -04:00
|
|
|
private array $resources = [];
|
2018-09-02 09:48:13 -04:00
|
|
|
/** @var string Filename that the zip file should have */
|
2022-05-13 05:10:42 -04:00
|
|
|
private string $name;
|
|
|
|
|
private IRequest $request;
|
2018-09-02 09:48:13 -04:00
|
|
|
|
2018-10-02 16:35:31 -04:00
|
|
|
/**
|
2023-06-14 02:56:42 -04:00
|
|
|
* @param S $status
|
|
|
|
|
* @param H $headers
|
2018-10-02 16:35:31 -04:00
|
|
|
* @since 15.0.0
|
|
|
|
|
*/
|
2023-06-14 02:56:42 -04:00
|
|
|
public function __construct(IRequest $request, string $name = 'output', int $status = Http::STATUS_OK, array $headers = []) {
|
|
|
|
|
parent::__construct($status, $headers);
|
2019-04-03 12:42:34 -04:00
|
|
|
|
2018-09-02 09:48:13 -04:00
|
|
|
$this->name = $name;
|
|
|
|
|
$this->request = $request;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:35:31 -04:00
|
|
|
/**
|
|
|
|
|
* @since 15.0.0
|
|
|
|
|
*/
|
2018-09-02 09:48:13 -04:00
|
|
|
public function addResource($r, string $internalName, int $size, int $time = -1) {
|
|
|
|
|
if (!\is_resource($r)) {
|
2018-10-02 02:17:27 -04:00
|
|
|
throw new \InvalidArgumentException('No resource provided');
|
2018-09-02 09:48:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->resources[] = [
|
|
|
|
|
'resource' => $r,
|
|
|
|
|
'internalName' => $internalName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'time' => $time,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 16:35:31 -04:00
|
|
|
/**
|
|
|
|
|
* @since 15.0.0
|
|
|
|
|
*/
|
2018-09-02 09:48:13 -04:00
|
|
|
public function callback(IOutput $output) {
|
|
|
|
|
$size = 0;
|
|
|
|
|
$files = count($this->resources);
|
|
|
|
|
|
|
|
|
|
foreach ($this->resources as $resource) {
|
2018-10-02 02:16:28 -04:00
|
|
|
$size += $resource['size'];
|
2018-09-02 09:48:13 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 04:01:33 -04:00
|
|
|
$zip = new Streamer($this->request, $size, $files, \OCP\Server::get(IDateTimeZone::class));
|
2018-09-02 09:48:13 -04:00
|
|
|
$zip->sendHeaders($this->name);
|
|
|
|
|
|
|
|
|
|
foreach ($this->resources as $resource) {
|
|
|
|
|
$zip->addFileFromStream($resource['resource'], $resource['internalName'], $resource['size'], $resource['time']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$zip->finalize();
|
|
|
|
|
}
|
|
|
|
|
}
|