LazyFolder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. public function get($path) {
  120. return $this->getRootFolder()->get($this->getFullPath($path));
  121. }
  122. /**
  123. * @inheritDoc
  124. */
  125. public function rename($targetPath) {
  126. return $this->__call(__FUNCTION__, func_get_args());
  127. }
  128. /**
  129. * @inheritDoc
  130. */
  131. public function delete() {
  132. return $this->__call(__FUNCTION__, func_get_args());
  133. }
  134. /**
  135. * @inheritDoc
  136. */
  137. public function copy($targetPath) {
  138. return $this->__call(__FUNCTION__, func_get_args());
  139. }
  140. /**
  141. * @inheritDoc
  142. */
  143. public function touch($mtime = null) {
  144. $this->__call(__FUNCTION__, func_get_args());
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. public function getStorage() {
  150. return $this->__call(__FUNCTION__, func_get_args());
  151. }
  152. /**
  153. * @inheritDoc
  154. */
  155. public function getPath() {
  156. if (isset($this->data['path'])) {
  157. return $this->data['path'];
  158. }
  159. return $this->__call(__FUNCTION__, func_get_args());
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. public function getInternalPath() {
  165. return $this->__call(__FUNCTION__, func_get_args());
  166. }
  167. /**
  168. * @inheritDoc
  169. */
  170. public function getId() {
  171. if (isset($this->data['fileid'])) {
  172. return $this->data['fileid'];
  173. }
  174. return $this->__call(__FUNCTION__, func_get_args());
  175. }
  176. /**
  177. * @inheritDoc
  178. */
  179. public function stat() {
  180. return $this->__call(__FUNCTION__, func_get_args());
  181. }
  182. /**
  183. * @inheritDoc
  184. */
  185. public function getMTime() {
  186. if (isset($this->data['mtime'])) {
  187. return $this->data['mtime'];
  188. }
  189. return $this->__call(__FUNCTION__, func_get_args());
  190. }
  191. /**
  192. * @inheritDoc
  193. */
  194. public function getSize($includeMounts = true): int|float {
  195. if (isset($this->data['size'])) {
  196. return $this->data['size'];
  197. }
  198. return $this->__call(__FUNCTION__, func_get_args());
  199. }
  200. /**
  201. * @inheritDoc
  202. */
  203. public function getEtag() {
  204. if (isset($this->data['etag'])) {
  205. return $this->data['etag'];
  206. }
  207. return $this->__call(__FUNCTION__, func_get_args());
  208. }
  209. /**
  210. * @inheritDoc
  211. */
  212. public function getPermissions() {
  213. if (isset($this->data['permissions'])) {
  214. return $this->data['permissions'];
  215. }
  216. return $this->__call(__FUNCTION__, func_get_args());
  217. }
  218. /**
  219. * @inheritDoc
  220. */
  221. public function isReadable() {
  222. if (isset($this->data['permissions'])) {
  223. return ($this->data['permissions'] & Constants::PERMISSION_READ) == Constants::PERMISSION_READ;
  224. }
  225. return $this->__call(__FUNCTION__, func_get_args());
  226. }
  227. /**
  228. * @inheritDoc
  229. */
  230. public function isUpdateable() {
  231. if (isset($this->data['permissions'])) {
  232. return ($this->data['permissions'] & Constants::PERMISSION_UPDATE) == Constants::PERMISSION_UPDATE;
  233. }
  234. return $this->__call(__FUNCTION__, func_get_args());
  235. }
  236. /**
  237. * @inheritDoc
  238. */
  239. public function isDeletable() {
  240. if (isset($this->data['permissions'])) {
  241. return ($this->data['permissions'] & Constants::PERMISSION_DELETE) == Constants::PERMISSION_DELETE;
  242. }
  243. return $this->__call(__FUNCTION__, func_get_args());
  244. }
  245. /**
  246. * @inheritDoc
  247. */
  248. public function isShareable() {
  249. if (isset($this->data['permissions'])) {
  250. return ($this->data['permissions'] & Constants::PERMISSION_SHARE) == Constants::PERMISSION_SHARE;
  251. }
  252. return $this->__call(__FUNCTION__, func_get_args());
  253. }
  254. /**
  255. * @inheritDoc
  256. */
  257. public function getParent() {
  258. return $this->__call(__FUNCTION__, func_get_args());
  259. }
  260. /**
  261. * @inheritDoc
  262. */
  263. public function getName() {
  264. if (isset($this->data['path'])) {
  265. return basename($this->data['path']);
  266. }
  267. if (isset($this->data['name'])) {
  268. return $this->data['name'];
  269. }
  270. return $this->__call(__FUNCTION__, func_get_args());
  271. }
  272. /**
  273. * @inheritDoc
  274. */
  275. public function getUserFolder($userId) {
  276. return $this->__call(__FUNCTION__, func_get_args());
  277. }
  278. /**
  279. * @inheritDoc
  280. */
  281. public function getMimetype() {
  282. if (isset($this->data['mimetype'])) {
  283. return $this->data['mimetype'];
  284. }
  285. return $this->__call(__FUNCTION__, func_get_args());
  286. }
  287. /**
  288. * @inheritDoc
  289. */
  290. public function getMimePart() {
  291. if (isset($this->data['mimetype'])) {
  292. [$part,] = explode('/', $this->data['mimetype']);
  293. return $part;
  294. }
  295. return $this->__call(__FUNCTION__, func_get_args());
  296. }
  297. /**
  298. * @inheritDoc
  299. */
  300. public function isEncrypted() {
  301. return $this->__call(__FUNCTION__, func_get_args());
  302. }
  303. /**
  304. * @inheritDoc
  305. */
  306. public function getType() {
  307. if (isset($this->data['type'])) {
  308. return $this->data['type'];
  309. }
  310. return $this->__call(__FUNCTION__, func_get_args());
  311. }
  312. /**
  313. * @inheritDoc
  314. */
  315. public function isShared() {
  316. return $this->__call(__FUNCTION__, func_get_args());
  317. }
  318. /**
  319. * @inheritDoc
  320. */
  321. public function isMounted() {
  322. return $this->__call(__FUNCTION__, func_get_args());
  323. }
  324. /**
  325. * @inheritDoc
  326. */
  327. public function getMountPoint() {
  328. return $this->__call(__FUNCTION__, func_get_args());
  329. }
  330. /**
  331. * @inheritDoc
  332. */
  333. public function getOwner() {
  334. return $this->__call(__FUNCTION__, func_get_args());
  335. }
  336. /**
  337. * @inheritDoc
  338. */
  339. public function getChecksum() {
  340. return $this->__call(__FUNCTION__, func_get_args());
  341. }
  342. public function getExtension(): string {
  343. return $this->__call(__FUNCTION__, func_get_args());
  344. }
  345. /**
  346. * @inheritDoc
  347. */
  348. public function getFullPath($path) {
  349. if (isset($this->data['path'])) {
  350. $path = PathHelper::normalizePath($path);
  351. if (!Filesystem::isValidPath($path)) {
  352. throw new NotPermittedException('Invalid path "' . $path . '"');
  353. }
  354. return $this->data['path'] . $path;
  355. }
  356. return $this->__call(__FUNCTION__, func_get_args());
  357. }
  358. /**
  359. * @inheritDoc
  360. */
  361. public function isSubNode($node) {
  362. return $this->__call(__FUNCTION__, func_get_args());
  363. }
  364. /**
  365. * @inheritDoc
  366. */
  367. public function getDirectoryListing() {
  368. return $this->__call(__FUNCTION__, func_get_args());
  369. }
  370. public function nodeExists($path) {
  371. return $this->__call(__FUNCTION__, func_get_args());
  372. }
  373. /**
  374. * @inheritDoc
  375. */
  376. public function newFolder($path) {
  377. return $this->__call(__FUNCTION__, func_get_args());
  378. }
  379. /**
  380. * @inheritDoc
  381. */
  382. public function newFile($path, $content = null) {
  383. return $this->__call(__FUNCTION__, func_get_args());
  384. }
  385. /**
  386. * @inheritDoc
  387. */
  388. public function search($query) {
  389. return $this->__call(__FUNCTION__, func_get_args());
  390. }
  391. /**
  392. * @inheritDoc
  393. */
  394. public function searchByMime($mimetype) {
  395. return $this->__call(__FUNCTION__, func_get_args());
  396. }
  397. /**
  398. * @inheritDoc
  399. */
  400. public function searchByTag($tag, $userId) {
  401. return $this->__call(__FUNCTION__, func_get_args());
  402. }
  403. public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0) {
  404. return $this->__call(__FUNCTION__, func_get_args());
  405. }
  406. /**
  407. * @inheritDoc
  408. */
  409. public function getById($id) {
  410. return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
  411. }
  412. public function getFirstNodeById(int $id): ?\OCP\Files\Node {
  413. return $this->getRootFolder()->getFirstNodeByIdInPath($id, $this->getPath());
  414. }
  415. /**
  416. * @inheritDoc
  417. */
  418. public function getFreeSpace() {
  419. return $this->__call(__FUNCTION__, func_get_args());
  420. }
  421. /**
  422. * @inheritDoc
  423. */
  424. public function isCreatable() {
  425. return $this->__call(__FUNCTION__, func_get_args());
  426. }
  427. /**
  428. * @inheritDoc
  429. */
  430. public function getNonExistingName($name) {
  431. return $this->__call(__FUNCTION__, func_get_args());
  432. }
  433. /**
  434. * @inheritDoc
  435. */
  436. public function move($targetPath) {
  437. return $this->__call(__FUNCTION__, func_get_args());
  438. }
  439. /**
  440. * @inheritDoc
  441. */
  442. public function lock($type) {
  443. return $this->__call(__FUNCTION__, func_get_args());
  444. }
  445. /**
  446. * @inheritDoc
  447. */
  448. public function changeLock($targetType) {
  449. return $this->__call(__FUNCTION__, func_get_args());
  450. }
  451. /**
  452. * @inheritDoc
  453. */
  454. public function unlock($type) {
  455. return $this->__call(__FUNCTION__, func_get_args());
  456. }
  457. /**
  458. * @inheritDoc
  459. */
  460. public function getRecent($limit, $offset = 0) {
  461. return $this->__call(__FUNCTION__, func_get_args());
  462. }
  463. /**
  464. * @inheritDoc
  465. */
  466. public function getCreationTime(): int {
  467. return $this->__call(__FUNCTION__, func_get_args());
  468. }
  469. /**
  470. * @inheritDoc
  471. */
  472. public function getUploadTime(): int {
  473. return $this->__call(__FUNCTION__, func_get_args());
  474. }
  475. public function getRelativePath($path) {
  476. return PathHelper::getRelativePath($this->getPath(), $path);
  477. }
  478. public function getParentId(): int {
  479. if (isset($this->data['parent'])) {
  480. return $this->data['parent'];
  481. }
  482. return $this->__call(__FUNCTION__, func_get_args());
  483. }
  484. /**
  485. * @inheritDoc
  486. * @return array<string, int|string|bool|float|string[]|int[]>
  487. */
  488. public function getMetadata(): array {
  489. return $this->data['metadata'] ?? $this->__call(__FUNCTION__, func_get_args());
  490. }
  491. }