|
| 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\ObjectStore\ObjectStoreStorage; |
| 29 | +use OC\Files\View; |
| 30 | +use OCA\DAV\Connector\Sabre\Directory; |
| 31 | +use OCA\DAV\Connector\Sabre\FilesPlugin; |
| 32 | +use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload; |
| 33 | +use OCP\Files\Storage\IChunkedFileWrite; |
| 34 | +use OCP\Files\Storage\IProcessingCallbackStorage; |
| 35 | +use OCP\Files\Storage\IStorage; |
| 36 | +use OCP\Files\StorageInvalidException; |
| 37 | +use Sabre\DAV\Exception\BadRequest; |
| 38 | +use Sabre\DAV\Exception\InsufficientStorage; |
| 39 | +use Sabre\DAV\Exception\NotFound; |
| 40 | +use Sabre\DAV\Exception\PreconditionFailed; |
| 41 | +use Sabre\DAV\Server; |
| 42 | +use Sabre\DAV\ServerPlugin; |
| 43 | +use Sabre\DAV\Xml\Element\Response; |
| 44 | +use Sabre\DAV\Xml\Response\MultiStatus; |
| 45 | +use Sabre\HTTP\RequestInterface; |
| 46 | +use Sabre\HTTP\ResponseInterface; |
| 47 | +use Sabre\Uri; |
| 48 | + |
| 49 | +class ChunkingV2Plugin extends ServerPlugin { |
| 50 | + |
| 51 | + /** @var Server */ |
| 52 | + private $server; |
| 53 | + /** @var UploadFolder */ |
| 54 | + private $uploadFolder; |
| 55 | + |
| 56 | + private const TEMP_TARGET = '.target'; |
| 57 | + |
| 58 | + public const OBJECT_UPLOAD_CACHE_KEY = 'chunkedv2'; |
| 59 | + public const OBJECT_UPLOAD_TARGET_ID = '{http://nextcloud.org/ns}upload-target-id'; |
| 60 | + public const OBJECT_UPLOAD_TARGET = '{http://nextcloud.org/ns}upload-target'; |
| 61 | + public const OBJECT_UPLOAD_CHUNKTOKEN = '{http://nextcloud.org/ns}upload-chunktoken'; |
| 62 | + |
| 63 | + private const DESTINATION_HEADER = 'X-Chunking-Destination'; |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritdoc |
| 67 | + */ |
| 68 | + public function initialize(Server $server) { |
| 69 | + $server->on('afterMethod:MKCOL', [$this, 'beforeMkcol']); |
| 70 | + // 200 priority to call after the custom properties backend is registered |
| 71 | + $server->on('beforeMethod:PUT', [$this, 'beforePut'], 200); |
| 72 | + $server->on('beforeMethod:DELETE', [$this, 'beforeDelete'], 200); |
| 73 | + $server->on('beforeMove', [$this, 'beforeMove'], 90); |
| 74 | + |
| 75 | + $this->server = $server; |
| 76 | + |
| 77 | + $this->cache = \OC::$server->getMemCacheFactory()->createDistributed(self::OBJECT_UPLOAD_CACHE_KEY); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param string $path |
| 82 | + * @param bool $createIfNotExists |
| 83 | + * @return FutureFile|UploadFile|\Sabre\DAV\ICollection|\Sabre\DAV\INode |
| 84 | + */ |
| 85 | + private function getTargetFile(string $path, bool $createIfNotExists = false) { |
| 86 | + try { |
| 87 | + $targetFile = $this->server->tree->getNodeForPath($path); |
| 88 | + } catch (NotFound $e) { |
| 89 | + if ($createIfNotExists) { |
| 90 | + $this->uploadFolder->createFile(self::TEMP_TARGET); |
| 91 | + } |
| 92 | + $targetFile = $this->uploadFolder->getChild(self::TEMP_TARGET); |
| 93 | + } |
| 94 | + return $targetFile; |
| 95 | + } |
| 96 | + |
| 97 | + public function beforeMkcol(RequestInterface $request, ResponseInterface $response): bool { |
| 98 | + $this->uploadFolder = $this->server->tree->getNodeForPath($request->getPath()); |
| 99 | + try { |
| 100 | + $this->checkPrerequisites(); |
| 101 | + $storage = $this->getStorage(); |
| 102 | + } catch (StorageInvalidException | BadRequest $e) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + $targetPath = $this->server->httpRequest->getHeader(self::DESTINATION_HEADER); |
| 107 | + if (!$targetPath) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + $targetPath = $this->server->calculateUri($targetPath); |
| 112 | + |
| 113 | + $targetFile = $this->getTargetFile($targetPath, true); |
| 114 | + |
| 115 | + $uploadId = $storage->beginChunkedFile($targetFile->getInternalPath()); |
| 116 | + |
| 117 | + $this->cache->set($this->uploadFolder->getName(), [ |
| 118 | + self::OBJECT_UPLOAD_TARGET_ID => $targetFile->getId(), |
| 119 | + self::OBJECT_UPLOAD_CHUNKTOKEN => $uploadId, |
| 120 | + self::OBJECT_UPLOAD_TARGET => $targetPath, |
| 121 | + ]); |
| 122 | + |
| 123 | + $response->setStatus(201); |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + public function beforePut(RequestInterface $request, ResponseInterface $response): bool { |
| 128 | + $this->uploadFolder = $this->server->tree->getNodeForPath(dirname($request->getPath())); |
| 129 | + if (!$this->uploadFolder instanceof UploadFolder) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + $this->checkPrerequisites(); |
| 135 | + $storage = $this->getStorage(); |
| 136 | + } catch (StorageInvalidException | BadRequest $e) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + $properties = $this->getUploadSession(); |
| 141 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET] ?? null; |
| 142 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN] ?? null; |
| 143 | + if (empty($targetPath) || empty($uploadId)) { |
| 144 | + throw new PreconditionFailed('Missing metadata for chunked upload'); |
| 145 | + } |
| 146 | + $partId = (int)basename($request->getPath()); |
| 147 | + |
| 148 | + if (!($partId >= 1 && $partId <= 10000)) { |
| 149 | + throw new BadRequest('Invalid chunk id'); |
| 150 | + } |
| 151 | + |
| 152 | + $targetFile = $this->getTargetFile($targetPath); |
| 153 | + $cacheEntry = $storage->getCache()->get($targetFile->getInternalPath()); |
| 154 | + $tempTargetFile = null; |
| 155 | + |
| 156 | + $additionalSize = (int)$request->getHeader('Content-Length'); |
| 157 | + if ($this->uploadFolder->childExists(self::TEMP_TARGET)) { |
| 158 | + /** @var UploadFile $tempTargetFile */ |
| 159 | + $tempTargetFile = $this->uploadFolder->getChild(self::TEMP_TARGET); |
| 160 | + $tempTargetCache = $storage->getCache()->get($tempTargetFile->getInternalPath()); |
| 161 | + |
| 162 | + [$destinationDir, $destinationName] = Uri\split($targetPath); |
| 163 | + /** @var Directory $destinationParent */ |
| 164 | + $destinationParent = $this->server->tree->getNodeForPath($destinationDir); |
| 165 | + $free = $storage->free_space($destinationParent->getInternalPath()); |
| 166 | + $newSize = $tempTargetCache->getSize() + $additionalSize; |
| 167 | + if ($free >= 0 && ($tempTargetCache->getSize() > $free || $newSize > $free)) { |
| 168 | + throw new InsufficientStorage("Insufficient space in $targetPath"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + $stream = $request->getBodyAsStream(); |
| 173 | + $storage->putChunkedFilePart($targetFile->getInternalPath(), $uploadId, (string)$partId, $stream, $additionalSize); |
| 174 | + |
| 175 | + $storage->getCache()->update($cacheEntry->getId(), ['size' => $cacheEntry->getSize() + $additionalSize]); |
| 176 | + if ($tempTargetFile) { |
| 177 | + $storage->getPropagator()->propagateChange($tempTargetFile->getInternalPath(), time(), $additionalSize); |
| 178 | + } |
| 179 | + |
| 180 | + $response->setStatus(201); |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + public function beforeMove($sourcePath, $destination): bool { |
| 185 | + $this->uploadFolder = $this->server->tree->getNodeForPath(dirname($sourcePath)); |
| 186 | + try { |
| 187 | + $this->checkPrerequisites(); |
| 188 | + $storage = $this->getStorage(); |
| 189 | + } catch (StorageInvalidException | BadRequest $e) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + $properties = $this->getUploadSession(); |
| 193 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET] ?? null; |
| 194 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN] ?? null; |
| 195 | + |
| 196 | + // FIXME: check if $destination === TARGET |
| 197 | + if (empty($targetPath) || empty($uploadId)) { |
| 198 | + throw new PreconditionFailed('Missing metadata for chunked upload'); |
| 199 | + } |
| 200 | + |
| 201 | + $targetFile = $this->getTargetFile($targetPath); |
| 202 | + |
| 203 | + [$destinationDir, $destinationName] = Uri\split($destination); |
| 204 | + /** @var Directory $destinationParent */ |
| 205 | + $destinationParent = $this->server->tree->getNodeForPath($destinationDir); |
| 206 | + $destinationExists = $destinationParent->childExists($destinationName); |
| 207 | + |
| 208 | + // Using a multipart status here in order to be able to sent the actual status after processing the move |
| 209 | + $this->server->httpResponse->setStatus(207); |
| 210 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
| 211 | + |
| 212 | + // allow sync clients to send the modification and creation time along in a header |
| 213 | + $updateFileInfo = []; |
| 214 | + if ($this->server->httpRequest->getHeader('X-OC-MTime') !== null) { |
| 215 | + $updateFileInfo['mtime'] = $this->sanitizeMtime($this->server->httpRequest->getHeader('X-OC-MTime')); |
| 216 | + $this->server->httpResponse->setHeader('X-OC-MTime', 'accepted'); |
| 217 | + } |
| 218 | + if ($this->server->httpRequest->getHeader('X-OC-CTime') !== null) { |
| 219 | + $updateFileInfo['creation_time'] = $this->sanitizeMtime($this->server->httpRequest->getHeader('X-OC-CTime')); |
| 220 | + $this->server->httpResponse->setHeader('X-OC-CTime', 'accepted'); |
| 221 | + } |
| 222 | + |
| 223 | + $rootView = new View(); |
| 224 | + if ($storage->instanceOfStorage(ObjectStoreStorage::class)) { |
| 225 | + /** @var ObjectStoreStorage $storage */ |
| 226 | + $objectStore = $storage->getObjectStore(); |
| 227 | + if ($objectStore instanceof IObjectStoreMultiPartUpload) { |
| 228 | + $parts = $objectStore->getMultipartUploads($storage->getURN($targetFile->getId()), $uploadId); |
| 229 | + $size = 0; |
| 230 | + foreach ($parts as $part) { |
| 231 | + $size += $part['Size']; |
| 232 | + } |
| 233 | + $free = $storage->free_space($destinationParent->getInternalPath()); |
| 234 | + if ($free >= 0 && ($size > $free)) { |
| 235 | + throw new InsufficientStorage("Insufficient space in $targetPath"); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + if ($storage->instanceOfStorage(IProcessingCallbackStorage::class)) { |
| 240 | + /** @var IProcessingCallbackStorage $storage */ |
| 241 | + $lastTick = time(); |
| 242 | + $storage->processingCallback('writeChunkedFile', function () use ($lastTick) { |
| 243 | + if ($lastTick < time()) { |
| 244 | + \OC_Util::obEnd(); |
| 245 | + echo ' '; |
| 246 | + flush(); |
| 247 | + } |
| 248 | + $lastTick = time(); |
| 249 | + }); |
| 250 | + } |
| 251 | + |
| 252 | + $this->server->httpResponse->setBody(function () use ($targetFile, $rootView, $uploadId, $destinationName, $destinationParent, $destinationExists, $sourcePath, $destination, $updateFileInfo) { |
| 253 | + $rootView->writeChunkedFile($targetFile->getAbsoluteInternalPath(), $uploadId); |
| 254 | + $destinationInView = $destinationParent->getFileInfo()->getPath() . '/' . $destinationName; |
| 255 | + if (!$destinationExists) { |
| 256 | + $rootView->rename($targetFile->getAbsoluteInternalPath(), $destinationInView); |
| 257 | + } |
| 258 | + |
| 259 | + $rootView->putFileInfo($destinationInView, $updateFileInfo); |
| 260 | + |
| 261 | + $sourceNode = $this->server->tree->getNodeForPath($sourcePath); |
| 262 | + if ($sourceNode instanceof FutureFile) { |
| 263 | + $this->uploadFolder->delete(); |
| 264 | + } |
| 265 | + |
| 266 | + $this->server->emit('afterMove', [$sourcePath, $destination]); |
| 267 | + $this->server->emit('afterUnbind', [$sourcePath]); |
| 268 | + $this->server->emit('afterBind', [$destination]); |
| 269 | + |
| 270 | + $response = new Response( |
| 271 | + $destination, |
| 272 | + ['200' => [ |
| 273 | + FilesPlugin::SIZE_PROPERTYNAME => $rootView->filesize($destinationInView) |
| 274 | + ]], |
| 275 | + $destinationExists ? '204' : '201' |
| 276 | + ); |
| 277 | + echo $this->server->xml->write( |
| 278 | + '{DAV:}multistatus', |
| 279 | + new MultiStatus([$response]) |
| 280 | + ); |
| 281 | + }); |
| 282 | + return false; |
| 283 | + } |
| 284 | + |
| 285 | + public function beforeDelete(RequestInterface $request, ResponseInterface $response) { |
| 286 | + $this->uploadFolder = $this->server->tree->getNodeForPath($request->getPath()); |
| 287 | + try { |
| 288 | + if (!$this->uploadFolder instanceof UploadFolder) { |
| 289 | + return true; |
| 290 | + } |
| 291 | + $storage = $this->getStorage(); |
| 292 | + } catch (StorageInvalidException | BadRequest $e) { |
| 293 | + return true; |
| 294 | + } |
| 295 | + |
| 296 | + $properties = $this->getUploadSession(); |
| 297 | + $targetPath = $properties[self::OBJECT_UPLOAD_TARGET]; |
| 298 | + $uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN]; |
| 299 | + if (!$targetPath || !$uploadId) { |
| 300 | + return true; |
| 301 | + } |
| 302 | + $targetFile = $this->getTargetFile($targetPath); |
| 303 | + $storage->cancelChunkedFile($targetFile->getInternalPath(), $uploadId); |
| 304 | + return true; |
| 305 | + } |
| 306 | + |
| 307 | + /** @throws BadRequest */ |
| 308 | + private function checkPrerequisites(): void { |
| 309 | + if (!$this->uploadFolder instanceof UploadFolder || !$this->server->httpRequest->getHeader(self::DESTINATION_HEADER)) { |
| 310 | + throw new BadRequest('Chunking destination header not set'); |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * @return IChunkedFileWrite |
| 316 | + * @throws BadRequest |
| 317 | + * @throws StorageInvalidException |
| 318 | + */ |
| 319 | + private function getStorage(): IStorage { |
| 320 | + $this->checkPrerequisites(); |
| 321 | + $storage = $this->uploadFolder->getStorage(); |
| 322 | + if (!$storage->instanceOfStorage(IChunkedFileWrite::class)) { |
| 323 | + throw new StorageInvalidException('Storage does not support chunked file write'); |
| 324 | + } |
| 325 | + /** @var IChunkedFileWrite $storage */ |
| 326 | + return $storage; |
| 327 | + } |
| 328 | + |
| 329 | + protected function sanitizeMtime($mtimeFromRequest) { |
| 330 | + if (!is_numeric($mtimeFromRequest)) { |
| 331 | + throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).'); |
| 332 | + } |
| 333 | + |
| 334 | + return (int)$mtimeFromRequest; |
| 335 | + } |
| 336 | + |
| 337 | + public function getUploadSession(): array { |
| 338 | + return $this->cache->get($this->uploadFolder->getName()) ?? []; |
| 339 | + } |
| 340 | +} |
0 commit comments