AppData.php 4.6 KB

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