Root.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Preview\Storage;
  8. use OC\Files\AppData\AppData;
  9. use OC\SystemConfig;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\SimpleFS\ISimpleFolder;
  13. class Root extends AppData {
  14. private $isMultibucketPreviewDistributionEnabled = false;
  15. public function __construct(IRootFolder $rootFolder, SystemConfig $systemConfig) {
  16. parent::__construct($rootFolder, $systemConfig, 'preview');
  17. $this->isMultibucketPreviewDistributionEnabled = $systemConfig->getValue('objectstore.multibucket.preview-distribution', false) === true;
  18. }
  19. public function getFolder(string $name): ISimpleFolder {
  20. $internalFolder = self::getInternalFolder($name);
  21. try {
  22. return parent::getFolder($internalFolder);
  23. } catch (NotFoundException $e) {
  24. /*
  25. * The new folder structure is not found.
  26. * Lets try the old one
  27. */
  28. }
  29. try {
  30. return parent::getFolder($name);
  31. } catch (NotFoundException $e) {
  32. /*
  33. * The old folder structure is not found.
  34. * Lets try the multibucket fallback if available
  35. */
  36. if ($this->isMultibucketPreviewDistributionEnabled) {
  37. return parent::getFolder('old-multibucket/' . $internalFolder);
  38. }
  39. // when there is no further fallback just throw the exception
  40. throw $e;
  41. }
  42. }
  43. public function newFolder(string $name): ISimpleFolder {
  44. $internalFolder = self::getInternalFolder($name);
  45. return parent::newFolder($internalFolder);
  46. }
  47. /*
  48. * Do not allow directory listing on this special root
  49. * since it gets to big and time consuming
  50. */
  51. public function getDirectoryListing(): array {
  52. return [];
  53. }
  54. public static function getInternalFolder(string $name): string {
  55. return implode('/', str_split(substr(md5($name), 0, 7))) . '/' . $name;
  56. }
  57. public function getStorageId(): int {
  58. return $this->getAppDataRootFolder()->getStorage()->getCache()->getNumericStorageId();
  59. }
  60. }