| Total Complexity | 112 |
| Total Lines | 580 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like File often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 79 | class File extends Node implements IFile { |
||
| 80 | protected $request; |
||
| 81 | |||
| 82 | protected IL10N $l10n; |
||
| 83 | |||
| 84 | /** @var array<string, FileMetadata> */ |
||
| 85 | private array $metadata = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets up the node, expects a full path name |
||
| 89 | * |
||
| 90 | * @param \OC\Files\View $view |
||
| 91 | * @param \OCP\Files\FileInfo $info |
||
| 92 | * @param \OCP\Share\IManager $shareManager |
||
| 93 | * @param \OC\AppFramework\Http\Request $request |
||
| 94 | */ |
||
| 95 | public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Updates the data |
||
| 112 | * |
||
| 113 | * The data argument is a readable stream resource. |
||
| 114 | * |
||
| 115 | * After a successful put operation, you may choose to return an ETag. The |
||
| 116 | * etag must always be surrounded by double-quotes. These quotes must |
||
| 117 | * appear in the actual string you're returning. |
||
| 118 | * |
||
| 119 | * Clients may use the ETag from a PUT request to later on make sure that |
||
| 120 | * when they update the file, the contents haven't changed in the mean |
||
| 121 | * time. |
||
| 122 | * |
||
| 123 | * If you don't plan to store the file byte-by-byte, and you return a |
||
| 124 | * different object on a subsequent GET you are strongly recommended to not |
||
| 125 | * return an ETag, and just return null. |
||
| 126 | * |
||
| 127 | * @param resource $data |
||
| 128 | * |
||
| 129 | * @throws Forbidden |
||
| 130 | * @throws UnsupportedMediaType |
||
| 131 | * @throws BadRequest |
||
| 132 | * @throws Exception |
||
| 133 | * @throws EntityTooLarge |
||
| 134 | * @throws ServiceUnavailable |
||
| 135 | * @throws FileLocked |
||
| 136 | * @return string|null |
||
| 137 | */ |
||
| 138 | public function put($data) { |
||
| 139 | try { |
||
| 140 | $exists = $this->fileView->file_exists($this->path); |
||
| 141 | if ($exists && !$this->info->isUpdateable()) { |
||
| 142 | throw new Forbidden(); |
||
| 143 | } |
||
| 144 | } catch (StorageNotAvailableException $e) { |
||
| 145 | throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()])); |
||
| 146 | } |
||
| 147 | |||
| 148 | // verify path of the target |
||
| 149 | $this->verifyPath(); |
||
| 150 | |||
| 151 | /** @var Storage $partStorage */ |
||
| 152 | [$partStorage] = $this->fileView->resolvePath($this->path); |
||
| 153 | $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
||
| 154 | |||
| 155 | $view = \OC\Files\Filesystem::getView(); |
||
| 156 | |||
| 157 | if ($needsPartFile) { |
||
| 158 | // mark file as partial while uploading (ignored by the scanner) |
||
| 159 | $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
||
| 160 | |||
| 161 | if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) { |
||
|
|
|||
| 162 | $needsPartFile = false; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | if (!$needsPartFile) { |
||
| 166 | // upload file directly as the final path |
||
| 167 | $partFilePath = $this->path; |
||
| 168 | |||
| 169 | if ($view && !$this->emitPreHooks($exists)) { |
||
| 170 | throw new Exception($this->l10n->t('Could not write to final file, canceled by hook')); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
||
| 175 | /** @var \OC\Files\Storage\Storage $partStorage */ |
||
| 176 | [$partStorage, $internalPartPath] = $this->fileView->resolvePath($partFilePath); |
||
| 177 | /** @var \OC\Files\Storage\Storage $storage */ |
||
| 178 | [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
||
| 179 | try { |
||
| 180 | if (!$needsPartFile) { |
||
| 181 | try { |
||
| 182 | $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
| 183 | } catch (LockedException $e) { |
||
| 184 | // during very large uploads, the shared lock we got at the start might have been expired |
||
| 185 | // meaning that the above lock can fail not just only because somebody else got a shared lock |
||
| 186 | // or because there is no existing shared lock to make exclusive |
||
| 187 | // |
||
| 188 | // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
||
| 189 | // lock this will still fail, if our original shared lock expired the new lock will be successful and |
||
| 190 | // the entire operation will be safe |
||
| 191 | |||
| 192 | try { |
||
| 193 | $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
| 194 | } catch (LockedException $ex) { |
||
| 195 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!is_resource($data)) { |
||
| 201 | $tmpData = fopen('php://temp', 'r+'); |
||
| 202 | if ($data !== null) { |
||
| 203 | fwrite($tmpData, $data); |
||
| 204 | rewind($tmpData); |
||
| 205 | } |
||
| 206 | $data = $tmpData; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($this->request->getHeader('X-HASH') !== '') { |
||
| 210 | $hash = $this->request->getHeader('X-HASH'); |
||
| 211 | if ($hash === 'all' || $hash === 'md5') { |
||
| 212 | $data = HashWrapper::wrap($data, 'md5', function ($hash) { |
||
| 213 | $this->header('X-Hash-MD5: ' . $hash); |
||
| 214 | }); |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($hash === 'all' || $hash === 'sha1') { |
||
| 218 | $data = HashWrapper::wrap($data, 'sha1', function ($hash) { |
||
| 219 | $this->header('X-Hash-SHA1: ' . $hash); |
||
| 220 | }); |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($hash === 'all' || $hash === 'sha256') { |
||
| 224 | $data = HashWrapper::wrap($data, 'sha256', function ($hash) { |
||
| 225 | $this->header('X-Hash-SHA256: ' . $hash); |
||
| 226 | }); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { |
||
| 231 | $isEOF = false; |
||
| 232 | $wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) { |
||
| 233 | $isEOF = feof($stream); |
||
| 234 | }); |
||
| 235 | |||
| 236 | $result = true; |
||
| 237 | $count = -1; |
||
| 238 | try { |
||
| 239 | $count = $partStorage->writeStream($internalPartPath, $wrappedData); |
||
| 240 | } catch (GenericFileException $e) { |
||
| 241 | $result = false; |
||
| 242 | } catch (BadGateway $e) { |
||
| 243 | throw $e; |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | if ($result === false) { |
||
| 248 | $result = $isEOF; |
||
| 249 | if (is_resource($wrappedData)) { |
||
| 250 | $result = feof($wrappedData); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } else { |
||
| 254 | $target = $partStorage->fopen($internalPartPath, 'wb'); |
||
| 255 | if ($target === false) { |
||
| 256 | \OC::$server->get(LoggerInterface::class)->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
||
| 257 | // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
||
| 258 | throw new Exception($this->l10n->t('Could not write file contents')); |
||
| 259 | } |
||
| 260 | [$count, $result] = \OC_Helper::streamCopy($data, $target); |
||
| 261 | fclose($target); |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($result === false) { |
||
| 265 | $expected = -1; |
||
| 266 | if (isset($_SERVER['CONTENT_LENGTH'])) { |
||
| 267 | $expected = (int)$_SERVER['CONTENT_LENGTH']; |
||
| 268 | } |
||
| 269 | if ($expected !== 0) { |
||
| 270 | throw new Exception( |
||
| 271 | $this->l10n->t( |
||
| 272 | 'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)', |
||
| 273 | [ |
||
| 274 | $this->l10n->n('%n byte', '%n bytes', $count), |
||
| 275 | $this->l10n->n('%n byte', '%n bytes', $expected), |
||
| 276 | ], |
||
| 277 | ) |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | // if content length is sent by client: |
||
| 283 | // double check if the file was fully received |
||
| 284 | // compare expected and actual size |
||
| 285 | if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
||
| 286 | $expected = (int)$_SERVER['CONTENT_LENGTH']; |
||
| 287 | if ($count !== $expected) { |
||
| 288 | throw new BadRequest( |
||
| 289 | $this->l10n->t( |
||
| 290 | 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
||
| 291 | [ |
||
| 292 | $this->l10n->n('%n byte', '%n bytes', $expected), |
||
| 293 | $this->l10n->n('%n byte', '%n bytes', $count), |
||
| 294 | ], |
||
| 295 | ) |
||
| 296 | ); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } catch (\Exception $e) { |
||
| 300 | if ($e instanceof LockedException) { |
||
| 301 | \OC::$server->get(LoggerInterface::class)->debug($e->getMessage(), ['exception' => $e]); |
||
| 302 | } else { |
||
| 303 | \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($needsPartFile) { |
||
| 307 | $partStorage->unlink($internalPartPath); |
||
| 308 | } |
||
| 309 | $this->convertToSabreException($e); |
||
| 310 | } |
||
| 311 | |||
| 312 | try { |
||
| 313 | if ($needsPartFile) { |
||
| 314 | if ($view && !$this->emitPreHooks($exists)) { |
||
| 315 | $partStorage->unlink($internalPartPath); |
||
| 316 | throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook')); |
||
| 317 | } |
||
| 318 | try { |
||
| 319 | $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
| 320 | } catch (LockedException $e) { |
||
| 321 | // during very large uploads, the shared lock we got at the start might have been expired |
||
| 322 | // meaning that the above lock can fail not just only because somebody else got a shared lock |
||
| 323 | // or because there is no existing shared lock to make exclusive |
||
| 324 | // |
||
| 325 | // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
||
| 326 | // lock this will still fail, if our original shared lock expired the new lock will be successful and |
||
| 327 | // the entire operation will be safe |
||
| 328 | |||
| 329 | try { |
||
| 330 | $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
||
| 331 | } catch (LockedException $ex) { |
||
| 332 | if ($needsPartFile) { |
||
| 333 | $partStorage->unlink($internalPartPath); |
||
| 334 | } |
||
| 335 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | // rename to correct path |
||
| 340 | try { |
||
| 341 | $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
||
| 342 | $fileExists = $storage->file_exists($internalPath); |
||
| 343 | if ($renameOkay === false || $fileExists === false) { |
||
| 344 | \OC::$server->get(LoggerInterface::class)->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']); |
||
| 345 | throw new Exception($this->l10n->t('Could not rename part file to final file')); |
||
| 346 | } |
||
| 347 | } catch (ForbiddenException $ex) { |
||
| 348 | if (!$ex->getRetry()) { |
||
| 349 | $partStorage->unlink($internalPartPath); |
||
| 350 | } |
||
| 351 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
||
| 352 | } catch (\Exception $e) { |
||
| 353 | $partStorage->unlink($internalPartPath); |
||
| 354 | $this->convertToSabreException($e); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // since we skipped the view we need to scan and emit the hooks ourselves |
||
| 359 | $storage->getUpdater()->update($internalPath); |
||
| 360 | |||
| 361 | try { |
||
| 362 | $this->changeLock(ILockingProvider::LOCK_SHARED); |
||
| 363 | } catch (LockedException $e) { |
||
| 364 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 365 | } |
||
| 366 | |||
| 367 | // allow sync clients to send the mtime along in a header |
||
| 368 | if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
||
| 369 | $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
||
| 370 | if ($this->fileView->touch($this->path, $mtime)) { |
||
| 371 | $this->header('X-OC-MTime: accepted'); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | $fileInfoUpdate = [ |
||
| 376 | 'upload_time' => time() |
||
| 377 | ]; |
||
| 378 | |||
| 379 | // allow sync clients to send the creation time along in a header |
||
| 380 | if (isset($this->request->server['HTTP_X_OC_CTIME'])) { |
||
| 381 | $ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']); |
||
| 382 | $fileInfoUpdate['creation_time'] = $ctime; |
||
| 383 | $this->header('X-OC-CTime: accepted'); |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->fileView->putFileInfo($this->path, $fileInfoUpdate); |
||
| 387 | |||
| 388 | if ($view) { |
||
| 389 | $this->emitPostHooks($exists); |
||
| 390 | } |
||
| 391 | |||
| 392 | $this->refreshInfo(); |
||
| 393 | |||
| 394 | if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
||
| 395 | $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
||
| 396 | $this->setChecksum($checksum); |
||
| 397 | } elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
||
| 398 | $this->setChecksum(''); |
||
| 399 | } |
||
| 400 | } catch (StorageNotAvailableException $e) { |
||
| 401 | throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e); |
||
| 402 | } |
||
| 403 | |||
| 404 | return '"' . $this->info->getEtag() . '"'; |
||
| 405 | } |
||
| 406 | |||
| 407 | private function getPartFileBasePath($path) { |
||
| 408 | $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
||
| 409 | if ($partFileInStorage) { |
||
| 410 | return $path; |
||
| 411 | } else { |
||
| 412 | return md5($path); // will place it in the root of the view with a unique name |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $path |
||
| 418 | */ |
||
| 419 | private function emitPreHooks($exists, $path = null) { |
||
| 420 | if (is_null($path)) { |
||
| 421 | $path = $this->path; |
||
| 422 | } |
||
| 423 | $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
||
| 424 | $run = true; |
||
| 425 | |||
| 426 | if (!$exists) { |
||
| 427 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, [ |
||
| 428 | \OC\Files\Filesystem::signal_param_path => $hookPath, |
||
| 429 | \OC\Files\Filesystem::signal_param_run => &$run, |
||
| 430 | ]); |
||
| 431 | } else { |
||
| 432 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, [ |
||
| 433 | \OC\Files\Filesystem::signal_param_path => $hookPath, |
||
| 434 | \OC\Files\Filesystem::signal_param_run => &$run, |
||
| 435 | ]); |
||
| 436 | } |
||
| 437 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, [ |
||
| 438 | \OC\Files\Filesystem::signal_param_path => $hookPath, |
||
| 439 | \OC\Files\Filesystem::signal_param_run => &$run, |
||
| 440 | ]); |
||
| 441 | return $run; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $path |
||
| 446 | */ |
||
| 447 | private function emitPostHooks($exists, $path = null) { |
||
| 448 | if (is_null($path)) { |
||
| 449 | $path = $this->path; |
||
| 450 | } |
||
| 451 | $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
||
| 452 | if (!$exists) { |
||
| 453 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, [ |
||
| 454 | \OC\Files\Filesystem::signal_param_path => $hookPath |
||
| 455 | ]); |
||
| 456 | } else { |
||
| 457 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, [ |
||
| 458 | \OC\Files\Filesystem::signal_param_path => $hookPath |
||
| 459 | ]); |
||
| 460 | } |
||
| 461 | \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, [ |
||
| 462 | \OC\Files\Filesystem::signal_param_path => $hookPath |
||
| 463 | ]); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns the data |
||
| 468 | * |
||
| 469 | * @return resource |
||
| 470 | * @throws Forbidden |
||
| 471 | * @throws ServiceUnavailable |
||
| 472 | */ |
||
| 473 | public function get() { |
||
| 474 | //throw exception if encryption is disabled but files are still encrypted |
||
| 475 | try { |
||
| 476 | if (!$this->info->isReadable()) { |
||
| 477 | // do a if the file did not exist |
||
| 478 | throw new NotFound(); |
||
| 479 | } |
||
| 480 | try { |
||
| 481 | $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
||
| 482 | } catch (\Exception $e) { |
||
| 483 | $this->convertToSabreException($e); |
||
| 484 | } |
||
| 485 | |||
| 486 | if ($res === false) { |
||
| 487 | throw new ServiceUnavailable($this->l10n->t('Could not open file')); |
||
| 488 | } |
||
| 489 | |||
| 490 | // comparing current file size with the one in DB |
||
| 491 | // if different, fix DB and refresh cache. |
||
| 492 | if ($this->getSize() !== $this->fileView->filesize($this->getPath())) { |
||
| 493 | $logger = \OC::$server->get(LoggerInterface::class); |
||
| 494 | $logger->warning('fixing cached size of file id=' . $this->getId()); |
||
| 495 | |||
| 496 | $this->getFileInfo()->getStorage()->getUpdater()->update($this->getFileInfo()->getInternalPath()); |
||
| 497 | $this->refreshInfo(); |
||
| 498 | } |
||
| 499 | |||
| 500 | return $res; |
||
| 501 | } catch (GenericEncryptionException $e) { |
||
| 502 | // returning 503 will allow retry of the operation at a later point in time |
||
| 503 | throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()])); |
||
| 504 | } catch (StorageNotAvailableException $e) { |
||
| 505 | throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()])); |
||
| 506 | } catch (ForbiddenException $ex) { |
||
| 507 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
||
| 508 | } catch (LockedException $e) { |
||
| 509 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Delete the current file |
||
| 515 | * |
||
| 516 | * @throws Forbidden |
||
| 517 | * @throws ServiceUnavailable |
||
| 518 | */ |
||
| 519 | public function delete() { |
||
| 520 | if (!$this->info->isDeletable()) { |
||
| 521 | throw new Forbidden(); |
||
| 522 | } |
||
| 523 | |||
| 524 | try { |
||
| 525 | if (!$this->fileView->unlink($this->path)) { |
||
| 526 | // assume it wasn't possible to delete due to permissions |
||
| 527 | throw new Forbidden(); |
||
| 528 | } |
||
| 529 | } catch (StorageNotAvailableException $e) { |
||
| 530 | throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()])); |
||
| 531 | } catch (ForbiddenException $ex) { |
||
| 532 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
||
| 533 | } catch (LockedException $e) { |
||
| 534 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns the mime-type for a file |
||
| 540 | * |
||
| 541 | * If null is returned, we'll assume application/octet-stream |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public function getContentType() { |
||
| 546 | $mimeType = $this->info->getMimetype(); |
||
| 547 | |||
| 548 | // PROPFIND needs to return the correct mime type, for consistency with the web UI |
||
| 549 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
||
| 550 | return $mimeType; |
||
| 551 | } |
||
| 552 | return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return array|bool |
||
| 557 | */ |
||
| 558 | public function getDirectDownload() { |
||
| 559 | if (\OCP\Server::get(\OCP\App\IAppManager::class)->isEnabledForUser('encryption')) { |
||
| 560 | return []; |
||
| 561 | } |
||
| 562 | /** @var \OCP\Files\Storage $storage */ |
||
| 563 | [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
||
| 564 | if (is_null($storage)) { |
||
| 565 | return []; |
||
| 566 | } |
||
| 567 | |||
| 568 | return $storage->getDirectDownload($internalPath); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Convert the given exception to a SabreException instance |
||
| 573 | * |
||
| 574 | * @param \Exception $e |
||
| 575 | * |
||
| 576 | * @throws \Sabre\DAV\Exception |
||
| 577 | */ |
||
| 578 | private function convertToSabreException(\Exception $e) { |
||
| 579 | if ($e instanceof \Sabre\DAV\Exception) { |
||
| 580 | throw $e; |
||
| 581 | } |
||
| 582 | if ($e instanceof NotPermittedException) { |
||
| 583 | // a more general case - due to whatever reason the content could not be written |
||
| 584 | throw new Forbidden($e->getMessage(), 0, $e); |
||
| 585 | } |
||
| 586 | if ($e instanceof ForbiddenException) { |
||
| 587 | // the path for the file was forbidden |
||
| 588 | throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
||
| 589 | } |
||
| 590 | if ($e instanceof EntityTooLargeException) { |
||
| 591 | // the file is too big to be stored |
||
| 592 | throw new EntityTooLarge($e->getMessage(), 0, $e); |
||
| 593 | } |
||
| 594 | if ($e instanceof InvalidContentException) { |
||
| 595 | // the file content is not permitted |
||
| 596 | throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
||
| 597 | } |
||
| 598 | if ($e instanceof InvalidPathException) { |
||
| 599 | // the path for the file was not valid |
||
| 600 | // TODO: find proper http status code for this case |
||
| 601 | throw new Forbidden($e->getMessage(), 0, $e); |
||
| 602 | } |
||
| 603 | if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
||
| 604 | // the file is currently being written to by another process |
||
| 605 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 606 | } |
||
| 607 | if ($e instanceof GenericEncryptionException) { |
||
| 608 | // returning 503 will allow retry of the operation at a later point in time |
||
| 609 | throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e); |
||
| 610 | } |
||
| 611 | if ($e instanceof StorageNotAvailableException) { |
||
| 612 | throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e); |
||
| 613 | } |
||
| 614 | if ($e instanceof NotFoundException) { |
||
| 615 | throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e); |
||
| 616 | } |
||
| 617 | |||
| 618 | throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the checksum for this file |
||
| 623 | * |
||
| 624 | * @return string|null |
||
| 625 | */ |
||
| 626 | public function getChecksum() { |
||
| 627 | return $this->info->getChecksum(); |
||
| 628 | } |
||
| 629 | |||
| 630 | public function setChecksum(string $checksum) { |
||
| 633 | } |
||
| 634 | |||
| 635 | protected function header($string) { |
||
| 636 | if (!\OC::$CLI) { |
||
| 637 | \header($string); |
||
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | public function hash(string $type) { |
||
| 642 | return $this->fileView->hash($type, $this->path); |
||
| 643 | } |
||
| 644 | |||
| 645 | public function getNode(): \OCP\Files\File { |
||
| 646 | return $this->node; |
||
| 647 | } |
||
| 648 | |||
| 649 | public function getMetadata(string $group): FileMetadata { |
||
| 651 | } |
||
| 652 | |||
| 653 | public function setMetadata(string $group, FileMetadata $metadata): void { |
||
| 654 | $this->metadata[$group] = $metadata; |
||
| 655 | } |
||
| 656 | |||
| 657 | public function hasMetadata(string $group) { |
||
| 658 | return array_key_exists($group, $this->metadata); |
||
| 659 | } |
||
| 660 | } |
||
| 661 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.