LazyFolder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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\Files\Node;
  8. use OC\Files\Filesystem;
  9. use OC\Files\Utils\PathHelper;
  10. use OCP\Constants;
  11. use OCP\Files\Folder;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Mount\IMountPoint;
  14. use OCP\Files\NotPermittedException;
  15. /**
  16. * Class LazyFolder
  17. *
  18. * This is a lazy wrapper around a folder. So only
  19. * once it is needed this will get initialized.
  20. *
  21. * @package OC\Files\Node
  22. */
  23. class LazyFolder implements Folder {
  24. /** @var \Closure(): Folder */
  25. private \Closure $folderClosure;
  26. protected ?Folder $folder = null;
  27. protected IRootFolder $rootFolder;
  28. protected array $data;
  29. /**
  30. * @param IRootFolder $rootFolder
  31. * @param \Closure(): Folder $folderClosure
  32. * @param array $data
  33. */
  34. public function __construct(IRootFolder $rootFolder, \Closure $folderClosure, array $data = []) {
  35. $this->rootFolder = $rootFolder;
  36. $this->folderClosure = $folderClosure;
  37. $this->data = $data;
  38. }
  39. protected function getRootFolder(): IRootFolder {
  40. return $this->rootFolder;
  41. }
  42. protected function getRealFolder(): Folder {
  43. if ($this->folder === null) {
  44. $this->folder = call_user_func($this->folderClosure);
  45. }
  46. return $this->folder;
  47. }
  48. /**
  49. * Magic method to first get the real rootFolder and then
  50. * call $method with $args on it
  51. *
  52. * @param $method
  53. * @param $args
  54. * @return mixed
  55. */
  56. public function __call($method, $args) {
  57. return call_user_func_array([$this->getRealFolder(), $method], $args);
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function getUser() {
  63. return $this->__call(__FUNCTION__, func_get_args());
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. public function listen($scope, $method, callable $callback) {
  69. $this->__call(__FUNCTION__, func_get_args());
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function removeListener($scope = null, $method = null, ?callable $callback = null) {
  75. $this->__call(__FUNCTION__, func_get_args());
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function emit($scope, $method, $arguments = []) {
  81. $this->__call(__FUNCTION__, func_get_args());
  82. }
  83. /**
  84. * @inheritDoc
  85. */
  86. public function mount($storage, $mountPoint, $arguments = []) {
  87. $this->__call(__FUNCTION__, func_get_args());
  88. }
  89. /**
  90. * @inheritDoc
  91. */
  92. public function getMount(string $mountPoint): IMountPoint {
  93. return $this->__call(__FUNCTION__, func_get_args());
  94. }
  95. /**
  96. * @return IMountPoint[]
  97. */
  98. public function getMountsIn(string $mountPoint): array {
  99. return $this->__call(__FUNCTION__, func_get_args());
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. public function getMountByStorageId($storageId) {
  105. return $this->__call(__FUNCTION__, func_get_args());
  106. }
  107. /**
  108. * @inheritDoc
  109. */
  110. public function getMountByNumericStorageId($numericId) {
  111. return $this->__call(__FUNCTION__, func_get_args());
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. public function unMount($mount) {
  117. $this->__call(__FUNCTION__, func_get_args());
  118. }
  119. /**
  120. * @inheritDoc
  121. */
  122. public function get($path) {
  123. return $this->getRootFolder()->get($this->getFullPath($path));
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. public function rename($targetPath) {
  129. return $this->__call(__FUNCTION__, func_get_args());
  130. }
  131. /**
  132. * @inheritDoc
  133. */
  134. public function delete() {
  135. return $this->__call(__FUNCTION__, func_get_args());
  136. }
  137. /**
  138. * @inheritDoc
  139. */
  140. public function copy($targetPath) {
  141. return $this->__call(__FUNCTION__, func_get_args());
  142. }
  143. /**
  144. * @inheritDoc
  145. */
  146. public function touch($mtime = null) {
  147. $this->__call(__FUNCTION__, func_get_args());
  148. }
  149. /**
  150. * @inheritDoc
  151. */
  152. public function getStorage() {
  153. return $this->__call(__FUNCTION__, func_get_args());
  154. }
  155. /**
  156. * @inheritDoc
  157. */
  158. public function getPath() {
  159. if (isset($this->data['path'])) {
  160. return $this->data['path'];
  161. }
  162. return $this->__call(__FUNCTION__, func_get_args());
  163. }
  164. /**
  165. * @inheritDoc
  166. */
  167. public function getInternalPath() {
  168. return $this->__call(__FUNCTION__, func_get_args());
  169. }
  170. /**
  171. * @inheritDoc
  172. */
  173. public function getId() {
  174. if (isset($this->data['fileid'])) {
  175. return $this->data['fileid'];
  176. }
  177. return $this->__call(__FUNCTION__, func_get_args());
  178. }
  179. /**
  180. * @inheritDoc
  181. */
  182. public function stat() {
  183. return $this->__call(__FUNCTION__, func_get_args());
  184. }
  185. /**
  186. * @inheritDoc
  187. */
  188. public function getMTime() {
  189. if (isset($this->data['mtime'])) {
  190. return $this->data['mtime'];
  191. }
  192. return $this->__call(__FUNCTION__, func_get_args());
  193. }
  194. /**
  195. * @inheritDoc
  196. */
  197. public function getSize($includeMounts = true): int|float {
  198. if (isset($this->data['size'])) {
  199. return $this->data['size'];
  200. }
  201. return $this->__call(__FUNCTION__, func_get_args());
  202. }
  203. /**
  204. * @inheritDoc
  205. */
  206. public function getEtag() {
  207. if (isset($this->data['etag'])) {
  208. return $this->data['etag'];
  209. }
  210. return $this->__call(__FUNCTION__, func_get_args());
  211. }
  212. /**
  213. * @inheritDoc
  214. */
  215. public function getPermissions() {
  216. if (isset($this->data['permissions'])) {
  217. return $this->data['permissions'];
  218. }
  219. return $this->__call(__FUNCTION__, func_get_args());
  220. }
  221. /**
  222. * @inheritDoc
  223. */
  224. public function isReadable() {
  225. if (isset($this->data['permissions'])) {
  226. return ($this->data['permissions'] & Constants::PERMISSION_READ) == Constants::PERMISSION_READ;
  227. }
  228. return $this->__call(__FUNCTION__, func_get_args());
  229. }
  230. /**
  231. * @inheritDoc
  232. */
  233. public function isUpdateable() {
  234. if (isset($this->data['permissions'])) {
  235. return ($this->data['permissions'] & Constants::PERMISSION_UPDATE) == Constants::PERMISSION_UPDATE;
  236. }
  237. return $this->__call(__FUNCTION__, func_get_args());
  238. }
  239. /**
  240. * @inheritDoc
  241. */
  242. public function isDeletable() {
  243. if (isset($this->data['permissions'])) {
  244. return ($this->data['permissions'] & Constants::PERMISSION_DELETE) == Constants::PERMISSION_DELETE;
  245. }
  246. return $this->__call(__FUNCTION__, func_get_args());
  247. }
  248. /**
  249. * @inheritDoc
  250. */
  251. public function isShareable() {
  252. if (isset($this->data['permissions'])) {
  253. return ($this->data['permissions'] & Constants::PERMISSION_SHARE) == Constants::PERMISSION_SHARE;
  254. }
  255. return $this->__call(__FUNCTION__, func_get_args());
  256. }
  257. /**
  258. * @inheritDoc
  259. */
  260. public function getParent() {
  261. return $this->__call(__FUNCTION__, func_get_args());
  262. }
  263. /**
  264. * @inheritDoc
  265. */
  266. public function getName() {
  267. if (isset($this->data['path'])) {
  268. return basename($this->data['path']);
  269. }
  270. if (isset($this->data['name'])) {
  271. return $this->data['name'];
  272. }
  273. return $this->__call(__FUNCTION__, func_get_args());
  274. }
  275. /**
  276. * @inheritDoc
  277. */
  278. public function getUserFolder($userId) {
  279. return $this->__call(__FUNCTION__, func_get_args());
  280. }
  281. /**
  282. * @inheritDoc
  283. */
  284. public function getMimetype() {
  285. if (isset($this->data['mimetype'])) {
  286. return $this->data['mimetype'];
  287. }
  288. return $this->__call(__FUNCTION__, func_get_args());
  289. }
  290. /**
  291. * @inheritDoc
  292. */
  293. public function getMimePart() {
  294. if (isset($this->data['mimetype'])) {
  295. [$part,] = explode('/', $this->data['mimetype']);
  296. return $part;
  297. }
  298. return $this->__call(__FUNCTION__, func_get_args());
  299. }
  300. /**
  301. * @inheritDoc
  302. */
  303. public function isEncrypted() {
  304. return $this->__call(__FUNCTION__, func_get_args());
  305. }
  306. /**
  307. * @inheritDoc
  308. */
  309. public function getType() {
  310. if (isset($this->data['type'])) {
  311. return $this->data['type'];
  312. }
  313. return $this->__call(__FUNCTION__, func_get_args());
  314. }
  315. /**
  316. * @inheritDoc
  317. */
  318. public function isShared() {
  319. return $this->__call(__FUNCTION__, func_get_args());
  320. }
  321. /**
  322. * @inheritDoc
  323. */
  324. public function isMounted() {
  325. return $this->__call(__FUNCTION__, func_get_args());
  326. }
  327. /**
  328. * @inheritDoc
  329. */
  330. public function getMountPoint() {
  331. return $this->__call(__FUNCTION__, func_get_args());
  332. }
  333. /**
  334. * @inheritDoc
  335. */
  336. public function getOwner() {
  337. return $this->__call(__FUNCTION__, func_get_args());
  338. }
  339. /**
  340. * @inheritDoc
  341. */
  342. public function getChecksum() {
  343. return $this->__call(__FUNCTION__, func_get_args());
  344. }
  345. public function getExtension(): string {
  346. return $this->__call(__FUNCTION__, func_get_args());
  347. }
  348. /**
  349. * @inheritDoc
  350. */
  351. public function getFullPath($path) {
  352. if (isset($this->data['path'])) {
  353. $path = PathHelper::normalizePath($path);
  354. if (!Filesystem::isValidPath($path)) {
  355. throw new NotPermittedException('Invalid path "' . $path . '"');
  356. }
  357. return $this->data['path'] . $path;
  358. }
  359. return $this->__call(__FUNCTION__, func_get_args());
  360. }
  361. /**
  362. * @inheritDoc
  363. */
  364. public function isSubNode($node) {
  365. return $this->__call(__FUNCTION__, func_get_args());
  366. }
  367. /**
  368. * @inheritDoc
  369. */
  370. public function getDirectoryListing() {
  371. return $this->__call(__FUNCTION__, func_get_args());
  372. }
  373. /**
  374. * @inheritDoc
  375. */
  376. public function nodeExists($path) {
  377. return $this->__call(__FUNCTION__, func_get_args());
  378. }
  379. /**
  380. * @inheritDoc
  381. */
  382. public function newFolder($path) {
  383. return $this->__call(__FUNCTION__, func_get_args());
  384. }
  385. /**
  386. * @inheritDoc
  387. */
  388. public function newFile($path, $content = null) {
  389. return $this->__call(__FUNCTION__, func_get_args());
  390. }
  391. /**
  392. * @inheritDoc
  393. */
  394. public function search($query) {
  395. return $this->__call(__FUNCTION__, func_get_args());
  396. }
  397. /**
  398. * @inheritDoc
  399. */
  400. public function searchByMime($mimetype) {
  401. return $this->__call(__FUNCTION__, func_get_args());
  402. }
  403. /**
  404. * @inheritDoc
  405. */
  406. public function searchByTag($tag, $userId) {
  407. return $this->__call(__FUNCTION__, func_get_args());
  408. }
  409. public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0) {
  410. return $this->__call(__FUNCTION__, func_get_args());
  411. }
  412. /**
  413. * @inheritDoc
  414. */
  415. public function getById($id) {
  416. return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
  417. }
  418. public function getFirstNodeById(int $id): ?\OCP\Files\Node {
  419. return $this->getRootFolder()->getFirstNodeByIdInPath($id, $this->getPath());
  420. }
  421. /**
  422. * @inheritDoc
  423. */
  424. public function getFreeSpace() {
  425. return $this->__call(__FUNCTION__, func_get_args());
  426. }
  427. /**
  428. * @inheritDoc
  429. */
  430. public function isCreatable() {
  431. return $this->__call(__FUNCTION__, func_get_args());
  432. }
  433. /**
  434. * @inheritDoc
  435. */
  436. public function getNonExistingName($name) {
  437. return $this->__call(__FUNCTION__, func_get_args());
  438. }
  439. /**
  440. * @inheritDoc
  441. */
  442. public function move($targetPath) {
  443. return $this->__call(__FUNCTION__, func_get_args());
  444. }
  445. /**
  446. * @inheritDoc
  447. */
  448. public function lock($type) {
  449. return $this->__call(__FUNCTION__, func_get_args());
  450. }
  451. /**
  452. * @inheritDoc
  453. */
  454. public function changeLock($targetType) {
  455. return $this->__call(__FUNCTION__, func_get_args());
  456. }
  457. /**
  458. * @inheritDoc
  459. */
  460. public function unlock($type) {
  461. return $this->__call(__FUNCTION__, func_get_args());
  462. }
  463. /**
  464. * @inheritDoc
  465. */
  466. public function getRecent($limit, $offset = 0) {
  467. return $this->__call(__FUNCTION__, func_get_args());
  468. }
  469. /**
  470. * @inheritDoc
  471. */
  472. public function getCreationTime(): int {
  473. return $this->__call(__FUNCTION__, func_get_args());
  474. }
  475. /**
  476. * @inheritDoc
  477. */
  478. public function getUploadTime(): int {
  479. return $this->__call(__FUNCTION__, func_get_args());
  480. }
  481. public function getRelativePath($path) {
  482. return PathHelper::getRelativePath($this->getPath(), $path);
  483. }
  484. public function getParentId(): int {
  485. if (isset($this->data['parent'])) {
  486. return $this->data['parent'];
  487. }
  488. return $this->__call(__FUNCTION__, func_get_args());
  489. }
  490. /**
  491. * @inheritDoc
  492. * @return array<string, int|string|bool|float|string[]|int[]>
  493. */
  494. public function getMetadata(): array {
  495. return $this->data['metadata'] ?? $this->__call(__FUNCTION__, func_get_args());
  496. }
  497. }