Root.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. namespace OC\Preview\Storage;
  26. use OC\Files\AppData\AppData;
  27. use OC\SystemConfig;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFolder;
  31. class Root extends AppData {
  32. private $isMultibucketPreviewDistributionEnabled = false;
  33. public function __construct(IRootFolder $rootFolder, SystemConfig $systemConfig) {
  34. parent::__construct($rootFolder, $systemConfig, 'preview');
  35. $this->isMultibucketPreviewDistributionEnabled = $systemConfig->getValue('objectstore.multibucket.preview-distribution', false) === true;
  36. }
  37. public function getFolder(string $name): ISimpleFolder {
  38. $internalFolder = self::getInternalFolder($name);
  39. try {
  40. return parent::getFolder($internalFolder);
  41. } catch (NotFoundException $e) {
  42. /*
  43. * The new folder structure is not found.
  44. * Lets try the old one
  45. */
  46. }
  47. try {
  48. return parent::getFolder($name);
  49. } catch (NotFoundException $e) {
  50. /*
  51. * The old folder structure is not found.
  52. * Lets try the multibucket fallback if available
  53. */
  54. if ($this->isMultibucketPreviewDistributionEnabled) {
  55. return parent::getFolder('old-multibucket/' . $internalFolder);
  56. }
  57. // when there is no further fallback just throw the exception
  58. throw $e;
  59. }
  60. }
  61. public function newFolder(string $name): ISimpleFolder {
  62. $internalFolder = self::getInternalFolder($name);
  63. return parent::newFolder($internalFolder);
  64. }
  65. /*
  66. * Do not allow directory listing on this special root
  67. * since it gets to big and time consuming
  68. */
  69. public function getDirectoryListing(): array {
  70. return [];
  71. }
  72. public static function getInternalFolder(string $name): string {
  73. return implode('/', str_split(substr(md5($name), 0, 7))) . '/' . $name;
  74. }
  75. public function getStorageId(): int {
  76. return $this->getAppDataRootFolder()->getStorage()->getCache()->getNumericStorageId();
  77. }
  78. }