1
0

LazyFolder.php 10 KB

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