LazyFolder.php 11 KB

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