|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> |
| 7 | + * |
| 8 | + * @author Louis Chmn <louis@chmn.me> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\Files\Controller; |
| 28 | + |
| 29 | +use OC\AppFramework\Bootstrap\Coordinator; |
| 30 | +use OCP\AppFramework\Http\ZipResponse; |
| 31 | +use OCP\AppFramework\Controller; |
| 32 | +use OCP\Files\File; |
| 33 | +use OCP\Files\Folder; |
| 34 | +use OCP\Files\IFileDownloadProvider; |
| 35 | +use OCP\Files\Node; |
| 36 | +use OCP\IRequest; |
| 37 | +use Psr\Log\LoggerInterface; |
| 38 | + |
| 39 | +class DownloadController extends Controller { |
| 40 | + private Coordinator $coordinator; |
| 41 | + private LoggerInterface $logger; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + string $appName, |
| 45 | + IRequest $request, |
| 46 | + Coordinator $coordinator, |
| 47 | + LoggerInterface $logger |
| 48 | + ) { |
| 49 | + parent::__construct($appName, $request); |
| 50 | + |
| 51 | + $this->request = $request; |
| 52 | + $this->coordinator = $coordinator; |
| 53 | + $this->logger = $logger; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @NoCSRFRequired |
| 58 | + * @PublicPage |
| 59 | + * @UserRateThrottle(limit=5, period=100) |
| 60 | + * @AnonRateThrottle(limit=1, period=100) |
| 61 | + * @BruteForceProtection(action='download_files') |
| 62 | + */ |
| 63 | + public function index(string $files): ZipResponse { |
| 64 | + $response = new ZipResponse($this->request, 'download'); |
| 65 | + |
| 66 | + /** @var string[] */ |
| 67 | + $files = json_decode($files); |
| 68 | + |
| 69 | + if (count($files) === 0) { |
| 70 | + return $response; |
| 71 | + } |
| 72 | + |
| 73 | + [$firstPrefix,] = \Sabre\Uri\split($files[0]); |
| 74 | + $commonPrefix = $firstPrefix; |
| 75 | + foreach ($files as $filePath) { |
| 76 | + $commonPrefix = $this->getCommonPrefix($filePath, $commonPrefix); |
| 77 | + } |
| 78 | + |
| 79 | + foreach ($files as $filePath) { |
| 80 | + $node = null; |
| 81 | + |
| 82 | + foreach ($this->getProviders() as $provider) { |
| 83 | + try { |
| 84 | + $node = $provider->getNode($filePath); |
| 85 | + if ($node !== null) { |
| 86 | + break; |
| 87 | + } |
| 88 | + } catch (\Throwable $ex) { |
| 89 | + $providerClass = $provider::class; |
| 90 | + $this->logger->warning("Error while getting file content from $providerClass", ['exception' => $ex]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if ($node === null) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $this->addNode($response, $node, substr($filePath, strlen($commonPrefix))); |
| 99 | + } |
| 100 | + |
| 101 | + return $response; |
| 102 | + } |
| 103 | + |
| 104 | + private function getCommonPrefix(string $str1, string $str2): string { |
| 105 | + $mbStr1 = mb_str_split($str1); |
| 106 | + $mbStr2 = mb_str_split($str2); |
| 107 | + |
| 108 | + for ($i = 0; $i < count($mbStr1); $i++) { |
| 109 | + if ($mbStr1[$i] !== $mbStr2[$i]) { |
| 110 | + $i--; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if ($i < 0) { |
| 116 | + return ''; |
| 117 | + } else { |
| 118 | + return join(array_slice($mbStr1, 0, $i)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private function addNode(ZipResponse $response, Node $node, string $path): void { |
| 123 | + if ($node instanceof File) { |
| 124 | + $response->addResource($node->fopen('r'), $path, $node->getSize()); |
| 125 | + } |
| 126 | + |
| 127 | + if ($node instanceof Folder) { |
| 128 | + foreach ($node->getDirectoryListing() as $subnode) { |
| 129 | + $this->addNode($response, $subnode, $path.'/'.$subnode->getName()); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return IFileDownloadProvider[] |
| 136 | + */ |
| 137 | + private function getProviders() { |
| 138 | + /** @var IFileDownloadProvider[] */ |
| 139 | + $providers = []; |
| 140 | + |
| 141 | + $context = $this->coordinator->getRegistrationContext(); |
| 142 | + if ($context === null) { |
| 143 | + throw new \Exception("Can't get download providers"); |
| 144 | + } |
| 145 | + |
| 146 | + $providerRegistrations = $context->getFileDownloadProviders(); |
| 147 | + |
| 148 | + foreach ($providerRegistrations as $registration) { |
| 149 | + $providers[] = \OCP\Server::get($registration->getService()); |
| 150 | + } |
| 151 | + |
| 152 | + return $providers; |
| 153 | + } |
| 154 | +} |
0 commit comments