LazyFolder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Files\Node;
  25. use OC\Files\Utils\PathHelper;
  26. use OCP\Files\Folder;
  27. use OCP\Constants;
  28. /**
  29. * Class LazyFolder
  30. *
  31. * This is a lazy wrapper around a folder. So only
  32. * once it is needed this will get initialized.
  33. *
  34. * @package OC\Files\Node
  35. */
  36. class LazyFolder implements Folder {
  37. /** @var \Closure(): Folder */
  38. private $folderClosure;
  39. /** @var LazyFolder | null */
  40. protected $folder = null;
  41. protected array $data;
  42. /**
  43. * LazyFolder constructor.
  44. *
  45. * @param \Closure(): Folder $folderClosure
  46. */
  47. public function __construct(\Closure $folderClosure, array $data = []) {
  48. $this->folderClosure = $folderClosure;
  49. $this->data = $data;
  50. }
  51. /**
  52. * Magic method to first get the real rootFolder and then
  53. * call $method with $args on it
  54. *
  55. * @param $method
  56. * @param $args
  57. * @return mixed
  58. */
  59. public function __call($method, $args) {
  60. if ($this->folder === null) {
  61. $this->folder = call_user_func($this->folderClosure);
  62. }
  63. return call_user_func_array([$this->folder, $method], $args);
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. public function getUser() {
  69. return $this->__call(__FUNCTION__, func_get_args());
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function listen($scope, $method, callable $callback) {
  75. $this->__call(__FUNCTION__, func_get_args());
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function removeListener($scope = null, $method = null, callable $callback = null) {
  81. $this->__call(__FUNCTION__, func_get_args());
  82. }
  83. /**
  84. * @inheritDoc
  85. */
  86. public function emit($scope, $method, $arguments = []) {
  87. $this->__call(__FUNCTION__, func_get_args());
  88. }
  89. /**
  90. * @inheritDoc
  91. */
  92. public function mount($storage, $mountPoint, $arguments = []) {
  93. $this->__call(__FUNCTION__, func_get_args());
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function getMount($mountPoint) {
  99. return $this->__call(__FUNCTION__, func_get_args());
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. public function getMountsIn($mountPoint) {
  105. return $this->__call(__FUNCTION__, func_get_args());
  106. }
  107. /**
  108. * @inheritDoc
  109. */
  110. public function getMountByStorageId($storageId) {
  111. return $this->__call(__FUNCTION__, func_get_args());
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. public function getMountByNumericStorageId($numericId) {
  117. return $this->__call(__FUNCTION__, func_get_args());
  118. }
  119. /**
  120. * @inheritDoc
  121. */
  122. public function unMount($mount) {
  123. $this->__call(__FUNCTION__, func_get_args());
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. public function get($path) {
  129. return $this->__call(__FUNCTION__, func_get_args());
  130. }
  131. /**
  132. * @inheritDoc
  133. */
  134. public function rename($targetPath) {
  135. return $this->__call(__FUNCTION__, func_get_args());
  136. }
  137. /**
  138. * @inheritDoc
  139. */
  140. public function delete() {
  141. return $this->__call(__FUNCTION__, func_get_args());
  142. }
  143. /**
  144. * @inheritDoc
  145. */
  146. public function copy($targetPath) {
  147. return $this->__call(__FUNCTION__, func_get_args());
  148. }
  149. /**
  150. * @inheritDoc
  151. */
  152. public function touch($mtime = null) {
  153. $this->__call(__FUNCTION__, func_get_args());
  154. }
  155. /**
  156. * @inheritDoc
  157. */
  158. public function getStorage() {
  159. return $this->__call(__FUNCTION__, func_get_args());
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. public function getPath() {
  165. if (isset($this->data['path'])) {
  166. return $this->data['path'];
  167. }
  168. return $this->__call(__FUNCTION__, func_get_args());
  169. }
  170. /**
  171. * @inheritDoc
  172. */
  173. public function getInternalPath() {
  174. return $this->__call(__FUNCTION__, func_get_args());
  175. }
  176. /**
  177. * @inheritDoc
  178. */
  179. public function getId() {
  180. return $this->__call(__FUNCTION__, func_get_args());
  181. }
  182. /**
  183. * @inheritDoc
  184. */
  185. public function stat() {
  186. return $this->__call(__FUNCTION__, func_get_args());
  187. }
  188. /**
  189. * @inheritDoc
  190. */
  191. public function getMTime() {
  192. return $this->__call(__FUNCTION__, func_get_args());
  193. }
  194. /**
  195. * @inheritDoc
  196. */
  197. public function getSize($includeMounts = true): int|float {
  198. return $this->__call(__FUNCTION__, func_get_args());
  199. }
  200. /**
  201. * @inheritDoc
  202. */
  203. public function getEtag() {
  204. return $this->__call(__FUNCTION__, func_get_args());
  205. }
  206. /**
  207. * @inheritDoc
  208. */
  209. public function getPermissions() {
  210. if (isset($this->data['permissions'])) {
  211. return $this->data['permissions'];
  212. }
  213. return $this->__call(__FUNCTION__, func_get_args());
  214. }
  215. /**
  216. * @inheritDoc
  217. */
  218. public function isReadable() {
  219. if (isset($this->data['permissions'])) {
  220. return ($this->data['permissions'] & Constants::PERMISSION_READ) == Constants::PERMISSION_READ;
  221. }
  222. return $this->__call(__FUNCTION__, func_get_args());
  223. }
  224. /**
  225. * @inheritDoc
  226. */
  227. public function isUpdateable() {
  228. if (isset($this->data['permissions'])) {
  229. return ($this->data['permissions'] & Constants::PERMISSION_UPDATE) == Constants::PERMISSION_UPDATE;
  230. }
  231. return $this->__call(__FUNCTION__, func_get_args());
  232. }
  233. /**
  234. * @inheritDoc
  235. */
  236. public function isDeletable() {
  237. if (isset($this->data['permissions'])) {
  238. return ($this->data['permissions'] & Constants::PERMISSION_DELETE) == Constants::PERMISSION_DELETE;
  239. }
  240. return $this->__call(__FUNCTION__, func_get_args());
  241. }
  242. /**
  243. * @inheritDoc
  244. */
  245. public function isShareable() {
  246. if (isset($this->data['permissions'])) {
  247. return ($this->data['permissions'] & Constants::PERMISSION_SHARE) == Constants::PERMISSION_SHARE;
  248. }
  249. return $this->__call(__FUNCTION__, func_get_args());
  250. }
  251. /**
  252. * @inheritDoc
  253. */
  254. public function getParent() {
  255. return $this->__call(__FUNCTION__, func_get_args());
  256. }
  257. /**
  258. * @inheritDoc
  259. */
  260. public function getName() {
  261. return $this->__call(__FUNCTION__, func_get_args());
  262. }
  263. /**
  264. * @inheritDoc
  265. */
  266. public function getUserFolder($userId) {
  267. return $this->__call(__FUNCTION__, func_get_args());
  268. }
  269. /**
  270. * @inheritDoc
  271. */
  272. public function getMimetype() {
  273. if (isset($this->data['mimetype'])) {
  274. return $this->data['mimetype'];
  275. }
  276. return $this->__call(__FUNCTION__, func_get_args());
  277. }
  278. /**
  279. * @inheritDoc
  280. */
  281. public function getMimePart() {
  282. if (isset($this->data['mimetype'])) {
  283. [$part,] = explode('/', $this->data['mimetype']);
  284. return $part;
  285. }
  286. return $this->__call(__FUNCTION__, func_get_args());
  287. }
  288. /**
  289. * @inheritDoc
  290. */
  291. public function isEncrypted() {
  292. return $this->__call(__FUNCTION__, func_get_args());
  293. }
  294. /**
  295. * @inheritDoc
  296. */
  297. public function getType() {
  298. if (isset($this->data['type'])) {
  299. return $this->data['type'];
  300. }
  301. return $this->__call(__FUNCTION__, func_get_args());
  302. }
  303. /**
  304. * @inheritDoc
  305. */
  306. public function isShared() {
  307. return $this->__call(__FUNCTION__, func_get_args());
  308. }
  309. /**
  310. * @inheritDoc
  311. */
  312. public function isMounted() {
  313. return $this->__call(__FUNCTION__, func_get_args());
  314. }
  315. /**
  316. * @inheritDoc
  317. */
  318. public function getMountPoint() {
  319. return $this->__call(__FUNCTION__, func_get_args());
  320. }
  321. /**
  322. * @inheritDoc
  323. */
  324. public function getOwner() {
  325. return $this->__call(__FUNCTION__, func_get_args());
  326. }
  327. /**
  328. * @inheritDoc
  329. */
  330. public function getChecksum() {
  331. return $this->__call(__FUNCTION__, func_get_args());
  332. }
  333. public function getExtension(): string {
  334. return $this->__call(__FUNCTION__, func_get_args());
  335. }
  336. /**
  337. * @inheritDoc
  338. */
  339. public function getFullPath($path) {
  340. return $this->__call(__FUNCTION__, func_get_args());
  341. }
  342. /**
  343. * @inheritDoc
  344. */
  345. public function isSubNode($node) {
  346. return $this->__call(__FUNCTION__, func_get_args());
  347. }
  348. /**
  349. * @inheritDoc
  350. */
  351. public function getDirectoryListing() {
  352. return $this->__call(__FUNCTION__, func_get_args());
  353. }
  354. /**
  355. * @inheritDoc
  356. */
  357. public function nodeExists($path) {
  358. return $this->__call(__FUNCTION__, func_get_args());
  359. }
  360. /**
  361. * @inheritDoc
  362. */
  363. public function newFolder($path) {
  364. return $this->__call(__FUNCTION__, func_get_args());
  365. }
  366. /**
  367. * @inheritDoc
  368. */
  369. public function newFile($path, $content = null) {
  370. return $this->__call(__FUNCTION__, func_get_args());
  371. }
  372. /**
  373. * @inheritDoc
  374. */
  375. public function search($query) {
  376. return $this->__call(__FUNCTION__, func_get_args());
  377. }
  378. /**
  379. * @inheritDoc
  380. */
  381. public function searchByMime($mimetype) {
  382. return $this->__call(__FUNCTION__, func_get_args());
  383. }
  384. /**
  385. * @inheritDoc
  386. */
  387. public function searchByTag($tag, $userId) {
  388. return $this->__call(__FUNCTION__, func_get_args());
  389. }
  390. /**
  391. * @inheritDoc
  392. */
  393. public function getById($id) {
  394. return $this->__call(__FUNCTION__, func_get_args());
  395. }
  396. /**
  397. * @inheritDoc
  398. */
  399. public function getFreeSpace() {
  400. return $this->__call(__FUNCTION__, func_get_args());
  401. }
  402. /**
  403. * @inheritDoc
  404. */
  405. public function isCreatable() {
  406. return $this->__call(__FUNCTION__, func_get_args());
  407. }
  408. /**
  409. * @inheritDoc
  410. */
  411. public function getNonExistingName($name) {
  412. return $this->__call(__FUNCTION__, func_get_args());
  413. }
  414. /**
  415. * @inheritDoc
  416. */
  417. public function move($targetPath) {
  418. return $this->__call(__FUNCTION__, func_get_args());
  419. }
  420. /**
  421. * @inheritDoc
  422. */
  423. public function lock($type) {
  424. return $this->__call(__FUNCTION__, func_get_args());
  425. }
  426. /**
  427. * @inheritDoc
  428. */
  429. public function changeLock($targetType) {
  430. return $this->__call(__FUNCTION__, func_get_args());
  431. }
  432. /**
  433. * @inheritDoc
  434. */
  435. public function unlock($type) {
  436. return $this->__call(__FUNCTION__, func_get_args());
  437. }
  438. /**
  439. * @inheritDoc
  440. */
  441. public function getRecent($limit, $offset = 0) {
  442. return $this->__call(__FUNCTION__, func_get_args());
  443. }
  444. /**
  445. * @inheritDoc
  446. */
  447. public function getCreationTime(): int {
  448. return $this->__call(__FUNCTION__, func_get_args());
  449. }
  450. /**
  451. * @inheritDoc
  452. */
  453. public function getUploadTime(): int {
  454. return $this->__call(__FUNCTION__, func_get_args());
  455. }
  456. public function getRelativePath($path) {
  457. return PathHelper::getRelativePath($this->getPath(), $path);
  458. }
  459. }