|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\DAV\Connector\Sabre; |
| 10 | + |
| 11 | +use OC\Streamer; |
| 12 | +use OCP\Files\File as NcFile; |
| 13 | +use OCP\Files\Folder as NcFolder; |
| 14 | +use OCP\Files\Node as NcNode; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | +use Sabre\DAV\Server; |
| 17 | +use Sabre\DAV\ServerPlugin; |
| 18 | +use Sabre\DAV\Tree; |
| 19 | +use Sabre\HTTP\Request; |
| 20 | +use Sabre\HTTP\Response; |
| 21 | + |
| 22 | +/** |
| 23 | + * This plugin allows to download folders accessed by GET HTTP requests on DAV. |
| 24 | + * The WebDAV standard explicitly say that GET is not covered and should return what ever the application thinks would be a good representation. |
| 25 | + * |
| 26 | + * When a collection is accessed using GET, this will provide the content as a archive. |
| 27 | + * The type can be set by the `Accept` header (MIME type of zip or tar), or as browser fallback using a `accept` GET parameter. |
| 28 | + * It is also possible to only include some child nodes (from the collection it self) by providing a `filter` GET parameter or `X-NC-Files` custom header. |
| 29 | + */ |
| 30 | +class ZipFolderPlugin extends ServerPlugin { |
| 31 | + |
| 32 | + /** |
| 33 | + * Reference to main server object |
| 34 | + */ |
| 35 | + private ?Server $server = null; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + private Tree $tree, |
| 39 | + private LoggerInterface $logger, |
| 40 | + ) { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This initializes the plugin. |
| 45 | + * |
| 46 | + * This function is called by \Sabre\DAV\Server, after |
| 47 | + * addPlugin is called. |
| 48 | + * |
| 49 | + * This method should set up the required event subscriptions. |
| 50 | + */ |
| 51 | + public function initialize(Server $server): void { |
| 52 | + $this->server = $server; |
| 53 | + $this->server->on('method:GET', $this->handleDownload(...), 100); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Adding a node to the archive streamer. |
| 58 | + * This will recursively add new nodes to the stream if the node is a directory. |
| 59 | + */ |
| 60 | + protected function streamNode(Streamer $streamer, NcNode $node, string $rootPath): void { |
| 61 | + // Remove the root path from the filename to make it relative to the requested folder |
| 62 | + $filename = str_replace($rootPath, '', $node->getPath()); |
| 63 | + |
| 64 | + if ($node instanceof NcFile) { |
| 65 | + $resource = $node->fopen('rb'); |
| 66 | + if ($resource === false) { |
| 67 | + $this->logger->info('Cannot read file for zip stream', ['filePath' => $node->getPath()]); |
| 68 | + throw new \Sabre\DAV\Exception\ServiceUnavailable('Requested file can currently not be accessed.'); |
| 69 | + } |
| 70 | + $streamer->addFileFromStream($resource, $filename, $node->getSize(), $node->getMTime()); |
| 71 | + } elseif ($node instanceof NcFolder) { |
| 72 | + $streamer->addEmptyDir($filename); |
| 73 | + $content = $node->getDirectoryListing(); |
| 74 | + foreach ($content as $subNode) { |
| 75 | + $this->streamNode($streamer, $subNode, $rootPath); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Download a folder as an archive. |
| 82 | + * It is possible to filter / limit the files that should be downloaded, |
| 83 | + * either by passing (multiple) `X-NC-Files: the-file` headers |
| 84 | + * or by setting a `files=JSON_ARRAY_OF_FILES` URL query. |
| 85 | + * |
| 86 | + * @return false|null |
| 87 | + */ |
| 88 | + public function handleDownload(Request $request, Response $response): ?bool { |
| 89 | + $node = $this->tree->getNodeForPath($request->getPath()); |
| 90 | + if (!($node instanceof \OCA\DAV\Connector\Sabre\Directory)) { |
| 91 | + // only handle directories |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $query = $request->getQueryParameters(); |
| 96 | + |
| 97 | + // Get accept header - or if set overwrite with accept GET-param |
| 98 | + $accept = $request->getHeaderAsArray('Accept'); |
| 99 | + $acceptParam = $query['accept'] ?? ''; |
| 100 | + if ($acceptParam !== '') { |
| 101 | + $accept = array_map(fn (string $name) => strtolower(trim($name)), explode(',', $acceptParam)); |
| 102 | + } |
| 103 | + $zipRequest = !empty(array_intersect(['application/zip', 'zip'], $accept)); |
| 104 | + $tarRequest = !empty(array_intersect(['application/x-tar', 'tar'], $accept)); |
| 105 | + if (!$zipRequest && !$tarRequest) { |
| 106 | + // does not accept zip or tar stream |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + $files = $request->getHeaderAsArray('X-NC-Files'); |
| 111 | + $filesParam = $query['files'] ?? ''; |
| 112 | + // The preferred way would be headers, but this is not possible for simple browser requests ("links") |
| 113 | + // so we also need to support GET parameters |
| 114 | + if ($filesParam !== '') { |
| 115 | + $files = json_decode($filesParam); |
| 116 | + if (!is_array($files)) { |
| 117 | + if (!is_string($files)) { |
| 118 | + // no valid parameter so continue with Sabre behavior |
| 119 | + $this->logger->debug('Invalid files filter parameter for ZipFolderPlugin', ['filter' => $filesParam]); |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + $files = [$files]; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + $folder = $node->getNode(); |
| 128 | + $content = empty($files) ? $folder->getDirectoryListing() : []; |
| 129 | + foreach ($files as $path) { |
| 130 | + $child = $node->getChild($path); |
| 131 | + assert($child instanceof Node); |
| 132 | + $content[] = $child->getNode(); |
| 133 | + } |
| 134 | + |
| 135 | + $archiveName = 'download'; |
| 136 | + $rootPath = $folder->getPath(); |
| 137 | + if (empty($files)) { |
| 138 | + // We download the full folder so keep it in the tree |
| 139 | + $rootPath = dirname($folder->getPath()); |
| 140 | + // Full folder is loaded to rename the archive to the folder name |
| 141 | + $archiveName = $folder->getName(); |
| 142 | + } |
| 143 | + $streamer = new Streamer($tarRequest, -1, count($content)); |
| 144 | + $streamer->sendHeaders($archiveName); |
| 145 | + // For full folder downloads we also add the folder itself to the archive |
| 146 | + if (empty($files)) { |
| 147 | + $streamer->addEmptyDir($archiveName); |
| 148 | + } |
| 149 | + foreach ($content as $node) { |
| 150 | + $this->streamNode($streamer, $node, $rootPath); |
| 151 | + } |
| 152 | + $streamer->finalize(); |
| 153 | + return false; |
| 154 | + } |
| 155 | +} |
0 commit comments