You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.6 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  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\Files\AppData;
  26. use OC\Cache\CappedMemoryCache;
  27. use OC\Files\SimpleFS\SimpleFolder;
  28. use OC\SystemConfig;
  29. use OCP\Files\Folder;
  30. use OCP\Files\IAppData;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\Node;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\NotPermittedException;
  35. use OCP\Files\SimpleFS\ISimpleFolder;
  36. class AppData implements IAppData {
  37. /** @var IRootFolder */
  38. private $rootFolder;
  39. /** @var SystemConfig */
  40. private $config;
  41. /** @var string */
  42. private $appId;
  43. /** @var Folder */
  44. private $folder;
  45. /** @var (ISimpleFolder|NotFoundException)[]|CappedMemoryCache */
  46. private $folders;
  47. /**
  48. * AppData constructor.
  49. *
  50. * @param IRootFolder $rootFolder
  51. * @param SystemConfig $systemConfig
  52. * @param string $appId
  53. */
  54. public function __construct(IRootFolder $rootFolder,
  55. SystemConfig $systemConfig,
  56. string $appId) {
  57. $this->rootFolder = $rootFolder;
  58. $this->config = $systemConfig;
  59. $this->appId = $appId;
  60. $this->folders = new CappedMemoryCache();
  61. }
  62. private function getAppDataFolderName() {
  63. $instanceId = $this->config->getValue('instanceid', null);
  64. if ($instanceId === null) {
  65. throw new \RuntimeException('no instance id!');
  66. }
  67. return 'appdata_' . $instanceId;
  68. }
  69. private function getAppDataRootFolder(): Folder {
  70. $name = $this->getAppDataFolderName();
  71. try {
  72. /** @var Folder $node */
  73. $node = $this->rootFolder->get($name);
  74. return $node;
  75. } catch (NotFoundException $e) {
  76. try {
  77. return $this->rootFolder->newFolder($name);
  78. } catch (NotPermittedException $e) {
  79. throw new \RuntimeException('Could not get appdata folder');
  80. }
  81. }
  82. }
  83. /**
  84. * @return Folder
  85. * @throws \RuntimeException
  86. */
  87. private function getAppDataFolder(): Folder {
  88. if ($this->folder === null) {
  89. $name = $this->getAppDataFolderName();
  90. try {
  91. $this->folder = $this->rootFolder->get($name . '/' . $this->appId);
  92. } catch (NotFoundException $e) {
  93. $appDataRootFolder = $this->getAppDataRootFolder();
  94. try {
  95. $this->folder = $appDataRootFolder->get($this->appId);
  96. } catch (NotFoundException $e) {
  97. try {
  98. $this->folder = $appDataRootFolder->newFolder($this->appId);
  99. } catch (NotPermittedException $e) {
  100. throw new \RuntimeException('Could not get appdata folder for ' . $this->appId);
  101. }
  102. }
  103. }
  104. }
  105. return $this->folder;
  106. }
  107. public function getFolder(string $name): ISimpleFolder {
  108. $key = $this->appId . '/' . $name;
  109. if ($cachedFolder = $this->folders->get($key)) {
  110. if ($cachedFolder instanceof \Exception) {
  111. throw $cachedFolder;
  112. } else {
  113. return $cachedFolder;
  114. }
  115. }
  116. try {
  117. // Hardening if somebody wants to retrieve '/'
  118. if ($name === '/') {
  119. $node = $this->getAppDataFolder();
  120. } else {
  121. $path = $this->getAppDataFolderName() . '/' . $this->appId . '/' . $name;
  122. $node = $this->rootFolder->get($path);
  123. }
  124. } catch (NotFoundException $e) {
  125. $this->folders->set($key, $e);
  126. throw $e;
  127. }
  128. /** @var Folder $node */
  129. $folder = new SimpleFolder($node);
  130. $this->folders->set($key, $folder);
  131. return $folder;
  132. }
  133. public function newFolder(string $name): ISimpleFolder {
  134. $key = $this->appId . '/' . $name;
  135. $folder = $this->getAppDataFolder()->newFolder($name);
  136. $simpleFolder = new SimpleFolder($folder);
  137. $this->folders->set($key, $simpleFolder);
  138. return $simpleFolder;
  139. }
  140. public function getDirectoryListing(): array {
  141. $listing = $this->getAppDataFolder()->getDirectoryListing();
  142. $fileListing = array_map(function (Node $folder) {
  143. if ($folder instanceof Folder) {
  144. return new SimpleFolder($folder);
  145. }
  146. return null;
  147. }, $listing);
  148. $fileListing = array_filter($fileListing);
  149. return array_values($fileListing);
  150. }
  151. public function getId(): int {
  152. return $this->getAppDataFolder()->getId();
  153. }
  154. }