|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Paginate; |
| 11 | + |
| 12 | +use Sabre\DAV\Server; |
| 13 | +use Sabre\DAV\ServerPlugin; |
| 14 | +use Sabre\HTTP\RequestInterface; |
| 15 | +use Sabre\HTTP\ResponseInterface; |
| 16 | + |
| 17 | +class PaginatePlugin extends ServerPlugin { |
| 18 | + public const PAGINATE_HEADER = 'x-nc-paginate'; |
| 19 | + public const PAGINATE_TOTAL_HEADER = 'x-nc-paginate-total'; |
| 20 | + public const PAGINATE_TOKEN_HEADER = 'x-nc-paginate-token'; |
| 21 | + public const PAGINATE_OFFSET_HEADER = 'x-nc-paginate-offset'; |
| 22 | + public const PAGINATE_COUNT_HEADER = 'x-nc-paginate-count'; |
| 23 | + |
| 24 | + /** @var Server */ |
| 25 | + private $server; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private PaginateCache $cache, |
| 29 | + private int $pageSize = 100, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function initialize(Server $server): void { |
| 34 | + $this->server = $server; |
| 35 | + $server->on('beforeMultiStatus', [$this, 'onMultiStatus']); |
| 36 | + $server->on('method:SEARCH', [$this, 'onMethod'], 1); |
| 37 | + $server->on('method:PROPFIND', [$this, 'onMethod'], 1); |
| 38 | + $server->on('method:REPORT', [$this, 'onMethod'], 1); |
| 39 | + } |
| 40 | + |
| 41 | + public function getFeatures(): array { |
| 42 | + return ['nc-paginate']; |
| 43 | + } |
| 44 | + |
| 45 | + public function onMultiStatus(&$fileProperties): void { |
| 46 | + $request = $this->server->httpRequest; |
| 47 | + if (is_array($fileProperties)) { |
| 48 | + $fileProperties = new \ArrayIterator($fileProperties); |
| 49 | + } |
| 50 | + if ( |
| 51 | + $request->hasHeader(self::PAGINATE_HEADER) && |
| 52 | + !$request->hasHeader(self::PAGINATE_TOKEN_HEADER) |
| 53 | + ) { |
| 54 | + $url = $request->getUrl(); |
| 55 | + |
| 56 | + $pageSize = (int)$request->getHeader(self::PAGINATE_COUNT_HEADER) ?: $this->pageSize; |
| 57 | + $copyIterator = new LimitedCopyIterator($fileProperties, $pageSize); |
| 58 | + [$token, $count] = $this->cache->store($url, $copyIterator); |
| 59 | + |
| 60 | + $fileProperties = $copyIterator->getFirstItems(); |
| 61 | + $this->server->httpResponse->addHeader(self::PAGINATE_HEADER, 'true'); |
| 62 | + $this->server->httpResponse->addHeader(self::PAGINATE_TOKEN_HEADER, $token); |
| 63 | + $this->server->httpResponse->addHeader(self::PAGINATE_TOTAL_HEADER, $count); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public function onMethod(RequestInterface $request, ResponseInterface $response) { |
| 68 | + if ( |
| 69 | + $request->hasHeader(self::PAGINATE_TOKEN_HEADER) && |
| 70 | + $request->hasHeader(self::PAGINATE_OFFSET_HEADER) |
| 71 | + ) { |
| 72 | + $url = $this->server->httpRequest->getUrl(); |
| 73 | + $token = $request->getHeader(self::PAGINATE_TOKEN_HEADER); |
| 74 | + $offset = (int)$request->getHeader(self::PAGINATE_OFFSET_HEADER); |
| 75 | + $count = (int)$request->getHeader(self::PAGINATE_COUNT_HEADER) ?: $this->pageSize; |
| 76 | + |
| 77 | + $items = $this->cache->get($url, $token, $offset, $count); |
| 78 | + |
| 79 | + $response->setStatus(207); |
| 80 | + $response->addHeader(self::PAGINATE_HEADER, 'true'); |
| 81 | + $response->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
| 82 | + $response->setHeader('Vary', 'Brief,Prefer'); |
| 83 | + |
| 84 | + $prefer = $this->server->getHTTPPrefer(); |
| 85 | + $minimal = $prefer['return'] === 'minimal'; |
| 86 | + |
| 87 | + $data = $this->server->generateMultiStatus($items, $minimal); |
| 88 | + $response->setBody($data); |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments