LazyFolder.php 12 KB

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