|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/* |
| 5 | + * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net> |
| 6 | + * |
| 7 | + * @author Julius Härtl <jus@bitgrid.net> |
| 8 | + * |
| 9 | + * @license GNU AGPL version 3 or any later version |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\DAV\Upload; |
| 27 | + |
| 28 | +use OC\Files\View; |
| 29 | +use OCA\DAV\Connector\Sabre\Directory; |
| 30 | +use OCP\Files\Storage\IChunkedFileWrite; |
| 31 | +use OCP\Files\Storage\IStorage; |
| 32 | +use OCP\Files\StorageInvalidException; |
| 33 | +use Sabre\DAV\Exception\BadRequest; |
| 34 | +use Sabre\DAV\Exception\NotFound; |
| 35 | +use Sabre\DAV\Server; |
| 36 | +use Sabre\DAV\ServerPlugin; |
| 37 | +use Sabre\HTTP\RequestInterface; |
| 38 | +use Sabre\HTTP\ResponseInterface; |
| 39 | +use Sabre\Uri; |
| 40 | + |
| 41 | +class ChunkingV2Plugin extends ServerPlugin { |
| 42 | + |
| 43 | + /** @var Server */ |
| 44 | + private $server; |
| 45 | + /** @var UploadFolder */ |
| 46 | + private $uploadFolder; |
| 47 | + |
| 48 | + private const TEMP_TARGET = '.target'; |
| 49 | + |
| 50 | + private const OBJECT_UPLOAD_TARGET = '{http://nextcloud.org/ns}upload-target'; |
| 51 | + private const OBJECT_UPLOAD_CHUNKTOKEN = '{http://nextcloud.org/ns}upload-chunktoken'; |
| 52 | + |
| 53 | + private const DESTINATION_HEADER = 'X-Chunking-Destination'; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + public function initialize(Server $server) { |
| 59 | + $server->on('afterMethod:MKCOL', [$this, 'beforeMkcol']); |
| 60 | + // 200 priority to call after the custom properties backend is registered |
| 61 | + $server->on('beforeMethod:PUT', [$this, 'beforePut'], 200); |
| 62 | + $server->on('beforeMethod:DELETE', [$this, 'beforeDelete'], 200); |
| 63 | + $server->on('beforeMove', [$this, 'beforeMove'], 90); |
| 64 | + |
| 65 | + $this->server = $server; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param string $path |
| 70 | + * @param bool $createIfNotExists |
| 71 | + * @return FutureFile|UploadFile|\Sabre\DAV\ICollection|\Sabre\DAV\INode |
| 72 | + */ |
| 73 | + private function getTargetFile(string $path, bool $createIfNotExists = false) { |
| 74 | + try { |
| 75 | + $targetFile = $this->server->tree->getNodeForPath($path); |
| 76 | + } catch (NotFound $e) { |
| 77 | + if ($createIfNotExists) { |
| 78 | + $this->uploadFolder->createFile(self::TEMP_TARGET); |
| 79 | + } |
| 80 | + $targetFile = $this->uploadFolder->getChild(self::TEMP_TARGET); |
| 81 | + } |
| 82 | + return $targetFile; |
| 83 | + } |
| 84 | + |
| 85 | + public function beforeMkcol(RequestInterface $request, ResponseInterface $response): bool { |
| 86 | + $this->uploadFolder = $this->server->tree->getNodeForPath($request->getPath()); |
| 87 | + try { |
| 88 | + $this->checkPrerequisites(); |
| 89 | + $storage = $this->getStorage(); |
| 90 | + } catch (StorageInvalidException | BadRequest $e) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + $targetPath = $this->server->httpRequest->getHeader(self::DESTINATION_HEADER); |
| 95 | + if (!$targetPath) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + $targetFile = $this->getTargetFile($targetPath, true); |
| 100 | + |
| 101 | + $uploadId = $storage->beginChunkedFile($targetFile->getInternalPath()); |
| 102 | + |
| 103 | + // DAV properties on the UploadFolder are used in order to properly cleanup stale chunked file writes and to persist the target path |
| 104 | + $this->server->updateProperties($request->getPath(), [ |
| 105 | + self::OBJECT_UPLOAD_CHUNKTOKEN => $uploadId, |
| 106 | + self::OBJECT_UPLOAD_TARGET => $targetPath, |
| 107 | + ]); |
| 108 | + |
| 109 | + $response->setStatus(201); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public function beforePut(RequestInterface $request, ResponseInterface $response): bool { |
| 114 | + $this->uploadFolder = $this->server->tree->getNodeForPath(dirname($request->getPath())); |
| 115 | + try { |
| 116 | + $this->checkPrerequisites(); |
| 117 | + $storage = $this->getStorage(); |
| 118 | + } catch (StorageInvalidException | BadRequest $e) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + $properties = $this->server->getProperties(dirname($request->getPath()) . '/', [ self::OBJECT_UPLOAD_CHUNKTOKEN, self::OBJECT_UPLOAD_TARGET ]); |
| 123 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET]; |
| 124 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN]; |
| 125 | + $partId = (int)basename($request->getPath()); |
| 126 | + |
| 127 | + if (!($partId >= 1 && $partId <= 10000)) { |
| 128 | + throw new BadRequest('Invalid chunk id'); |
| 129 | + } |
| 130 | + |
| 131 | + $targetFile = $this->getTargetFile($targetPath); |
| 132 | + $stream = $request->getBodyAsStream(); |
| 133 | + $storage->putChunkedFilePart($targetFile->getInternalPath(), $uploadId, (string)$partId, $stream, (int)$request->getHeader('Content-Length')); |
| 134 | + |
| 135 | + $response->setStatus(201); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + public function beforeMove($sourcePath, $destination): bool { |
| 140 | + $this->uploadFolder = $this->server->tree->getNodeForPath(dirname($sourcePath)); |
| 141 | + try { |
| 142 | + $this->checkPrerequisites(); |
| 143 | + $this->getStorage(); |
| 144 | + } catch (StorageInvalidException | BadRequest $e) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + $properties = $this->server->getProperties(dirname($sourcePath) . '/', [ self::OBJECT_UPLOAD_CHUNKTOKEN, self::OBJECT_UPLOAD_TARGET ]); |
| 148 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET]; |
| 149 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN]; |
| 150 | + |
| 151 | + $targetFile = $this->getTargetFile($targetPath); |
| 152 | + |
| 153 | + [$destinationDir, $destinationName] = Uri\split($destination); |
| 154 | + /** @var Directory $destinationParent */ |
| 155 | + $destinationParent = $this->server->tree->getNodeForPath($destinationDir); |
| 156 | + $destinationExists = $destinationParent->childExists($destinationName); |
| 157 | + |
| 158 | + $rootView = new View(); |
| 159 | + $rootView->writeChunkedFile($targetFile->getAbsoluteInternalPath(), $uploadId); |
| 160 | + if (!$destinationExists) { |
| 161 | + $destinationInView = $destinationParent->getFileInfo()->getPath() . '/' . $destinationName; |
| 162 | + $rootView->rename($targetFile->getAbsoluteInternalPath(), $destinationInView); |
| 163 | + } |
| 164 | + |
| 165 | + $sourceNode = $this->server->tree->getNodeForPath($sourcePath); |
| 166 | + if ($sourceNode instanceof FutureFile) { |
| 167 | + $sourceNode->delete(); |
| 168 | + } |
| 169 | + |
| 170 | + $this->server->emit('afterMove', [$sourcePath, $destination]); |
| 171 | + $this->server->emit('afterUnbind', [$sourcePath]); |
| 172 | + $this->server->emit('afterBind', [$destination]); |
| 173 | + |
| 174 | + $response = $this->server->httpResponse; |
| 175 | + $response->setHeader('Content-Length', '0'); |
| 176 | + $response->setStatus($destinationExists ? 204 : 201); |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + public function beforeDelete(RequestInterface $request, ResponseInterface $response) { |
| 181 | + $this->uploadFolder = $this->server->tree->getNodeForPath($request->getPath()); |
| 182 | + try { |
| 183 | + if (!$this->uploadFolder instanceof UploadFolder) { |
| 184 | + return true; |
| 185 | + } |
| 186 | + $storage = $this->getStorage(); |
| 187 | + } catch (StorageInvalidException | BadRequest $e) { |
| 188 | + return true; |
| 189 | + } |
| 190 | + |
| 191 | + $properties = $this->server->getProperties($request->getPath() . '/', [ self::OBJECT_UPLOAD_CHUNKTOKEN, self::OBJECT_UPLOAD_TARGET ]); |
| 192 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET]; |
| 193 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN]; |
| 194 | + if (!$targetPath || !$uploadId) { |
| 195 | + return true; |
| 196 | + } |
| 197 | + $targetFile = $this->getTargetFile($targetPath); |
| 198 | + $storage->cancelChunkedFile($targetFile->getInternalPath(), $uploadId); |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | + /** @throws BadRequest */ |
| 203 | + private function checkPrerequisites(): void { |
| 204 | + if (!$this->uploadFolder instanceof UploadFolder || !$this->server->httpRequest->getHeader(self::DESTINATION_HEADER)) { |
| 205 | + throw new BadRequest('Chunking destination header not set'); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @return IChunkedFileWrite |
| 211 | + * @throws BadRequest |
| 212 | + * @throws StorageInvalidException |
| 213 | + */ |
| 214 | + private function getStorage(): IStorage { |
| 215 | + $this->checkPrerequisites(); |
| 216 | + $storage = $this->uploadFolder->getStorage(); |
| 217 | + if (!$storage->instanceOfStorage(IChunkedFileWrite::class)) { |
| 218 | + throw new StorageInvalidException('Storage does not support chunked file write'); |
| 219 | + } |
| 220 | + /** @var IChunkedFileWrite $storage */ |
| 221 | + return $storage; |
| 222 | + } |
| 223 | +} |
0 commit comments