Wrapper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Storage\Wrapper;
  30. use OCP\Files\InvalidPathException;
  31. use OCP\Files\Storage\ILockingStorage;
  32. use OCP\Files\Storage\IStorage;
  33. use OCP\Files\Storage\IWriteStreamStorage;
  34. use OCP\Lock\ILockingProvider;
  35. class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStreamStorage {
  36. /**
  37. * @var \OC\Files\Storage\Storage $storage
  38. */
  39. protected $storage;
  40. public $cache;
  41. public $scanner;
  42. public $watcher;
  43. public $propagator;
  44. public $updater;
  45. /**
  46. * @param array $parameters
  47. */
  48. public function __construct($parameters) {
  49. $this->storage = $parameters['storage'];
  50. }
  51. /**
  52. * @return \OC\Files\Storage\Storage
  53. */
  54. public function getWrapperStorage() {
  55. return $this->storage;
  56. }
  57. /**
  58. * Get the identifier for the storage,
  59. * the returned id should be the same for every storage object that is created with the same parameters
  60. * and two storage objects with the same id should refer to two storages that display the same files.
  61. *
  62. * @return string
  63. */
  64. public function getId() {
  65. return $this->getWrapperStorage()->getId();
  66. }
  67. /**
  68. * see http://php.net/manual/en/function.mkdir.php
  69. *
  70. * @param string $path
  71. * @return bool
  72. */
  73. public function mkdir($path) {
  74. return $this->getWrapperStorage()->mkdir($path);
  75. }
  76. /**
  77. * see http://php.net/manual/en/function.rmdir.php
  78. *
  79. * @param string $path
  80. * @return bool
  81. */
  82. public function rmdir($path) {
  83. return $this->getWrapperStorage()->rmdir($path);
  84. }
  85. /**
  86. * see http://php.net/manual/en/function.opendir.php
  87. *
  88. * @param string $path
  89. * @return resource
  90. */
  91. public function opendir($path) {
  92. return $this->getWrapperStorage()->opendir($path);
  93. }
  94. /**
  95. * see http://php.net/manual/en/function.is_dir.php
  96. *
  97. * @param string $path
  98. * @return bool
  99. */
  100. public function is_dir($path) {
  101. return $this->getWrapperStorage()->is_dir($path);
  102. }
  103. /**
  104. * see http://php.net/manual/en/function.is_file.php
  105. *
  106. * @param string $path
  107. * @return bool
  108. */
  109. public function is_file($path) {
  110. return $this->getWrapperStorage()->is_file($path);
  111. }
  112. /**
  113. * see http://php.net/manual/en/function.stat.php
  114. * only the following keys are required in the result: size and mtime
  115. *
  116. * @param string $path
  117. * @return array
  118. */
  119. public function stat($path) {
  120. return $this->getWrapperStorage()->stat($path);
  121. }
  122. /**
  123. * see http://php.net/manual/en/function.filetype.php
  124. *
  125. * @param string $path
  126. * @return bool
  127. */
  128. public function filetype($path) {
  129. return $this->getWrapperStorage()->filetype($path);
  130. }
  131. /**
  132. * see http://php.net/manual/en/function.filesize.php
  133. * The result for filesize when called on a folder is required to be 0
  134. *
  135. * @param string $path
  136. * @return int
  137. */
  138. public function filesize($path) {
  139. return $this->getWrapperStorage()->filesize($path);
  140. }
  141. /**
  142. * check if a file can be created in $path
  143. *
  144. * @param string $path
  145. * @return bool
  146. */
  147. public function isCreatable($path) {
  148. return $this->getWrapperStorage()->isCreatable($path);
  149. }
  150. /**
  151. * check if a file can be read
  152. *
  153. * @param string $path
  154. * @return bool
  155. */
  156. public function isReadable($path) {
  157. return $this->getWrapperStorage()->isReadable($path);
  158. }
  159. /**
  160. * check if a file can be written to
  161. *
  162. * @param string $path
  163. * @return bool
  164. */
  165. public function isUpdatable($path) {
  166. return $this->getWrapperStorage()->isUpdatable($path);
  167. }
  168. /**
  169. * check if a file can be deleted
  170. *
  171. * @param string $path
  172. * @return bool
  173. */
  174. public function isDeletable($path) {
  175. return $this->getWrapperStorage()->isDeletable($path);
  176. }
  177. /**
  178. * check if a file can be shared
  179. *
  180. * @param string $path
  181. * @return bool
  182. */
  183. public function isSharable($path) {
  184. return $this->getWrapperStorage()->isSharable($path);
  185. }
  186. /**
  187. * get the full permissions of a path.
  188. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  189. *
  190. * @param string $path
  191. * @return int
  192. */
  193. public function getPermissions($path) {
  194. return $this->getWrapperStorage()->getPermissions($path);
  195. }
  196. /**
  197. * see http://php.net/manual/en/function.file_exists.php
  198. *
  199. * @param string $path
  200. * @return bool
  201. */
  202. public function file_exists($path) {
  203. return $this->getWrapperStorage()->file_exists($path);
  204. }
  205. /**
  206. * see http://php.net/manual/en/function.filemtime.php
  207. *
  208. * @param string $path
  209. * @return int
  210. */
  211. public function filemtime($path) {
  212. return $this->getWrapperStorage()->filemtime($path);
  213. }
  214. /**
  215. * see http://php.net/manual/en/function.file_get_contents.php
  216. *
  217. * @param string $path
  218. * @return string
  219. */
  220. public function file_get_contents($path) {
  221. return $this->getWrapperStorage()->file_get_contents($path);
  222. }
  223. /**
  224. * see http://php.net/manual/en/function.file_put_contents.php
  225. *
  226. * @param string $path
  227. * @param string $data
  228. * @return bool
  229. */
  230. public function file_put_contents($path, $data) {
  231. return $this->getWrapperStorage()->file_put_contents($path, $data);
  232. }
  233. /**
  234. * see http://php.net/manual/en/function.unlink.php
  235. *
  236. * @param string $path
  237. * @return bool
  238. */
  239. public function unlink($path) {
  240. return $this->getWrapperStorage()->unlink($path);
  241. }
  242. /**
  243. * see http://php.net/manual/en/function.rename.php
  244. *
  245. * @param string $path1
  246. * @param string $path2
  247. * @return bool
  248. */
  249. public function rename($path1, $path2) {
  250. return $this->getWrapperStorage()->rename($path1, $path2);
  251. }
  252. /**
  253. * see http://php.net/manual/en/function.copy.php
  254. *
  255. * @param string $path1
  256. * @param string $path2
  257. * @return bool
  258. */
  259. public function copy($path1, $path2) {
  260. return $this->getWrapperStorage()->copy($path1, $path2);
  261. }
  262. /**
  263. * see http://php.net/manual/en/function.fopen.php
  264. *
  265. * @param string $path
  266. * @param string $mode
  267. * @return resource
  268. */
  269. public function fopen($path, $mode) {
  270. return $this->getWrapperStorage()->fopen($path, $mode);
  271. }
  272. /**
  273. * get the mimetype for a file or folder
  274. * The mimetype for a folder is required to be "httpd/unix-directory"
  275. *
  276. * @param string $path
  277. * @return string
  278. */
  279. public function getMimeType($path) {
  280. return $this->getWrapperStorage()->getMimeType($path);
  281. }
  282. /**
  283. * see http://php.net/manual/en/function.hash.php
  284. *
  285. * @param string $type
  286. * @param string $path
  287. * @param bool $raw
  288. * @return string
  289. */
  290. public function hash($type, $path, $raw = false) {
  291. return $this->getWrapperStorage()->hash($type, $path, $raw);
  292. }
  293. /**
  294. * see http://php.net/manual/en/function.free_space.php
  295. *
  296. * @param string $path
  297. * @return int
  298. */
  299. public function free_space($path) {
  300. return $this->getWrapperStorage()->free_space($path);
  301. }
  302. /**
  303. * search for occurrences of $query in file names
  304. *
  305. * @param string $query
  306. * @return array
  307. */
  308. public function search($query) {
  309. return $this->getWrapperStorage()->search($query);
  310. }
  311. /**
  312. * see http://php.net/manual/en/function.touch.php
  313. * If the backend does not support the operation, false should be returned
  314. *
  315. * @param string $path
  316. * @param int $mtime
  317. * @return bool
  318. */
  319. public function touch($path, $mtime = null) {
  320. return $this->getWrapperStorage()->touch($path, $mtime);
  321. }
  322. /**
  323. * get the path to a local version of the file.
  324. * The local version of the file can be temporary and doesn't have to be persistent across requests
  325. *
  326. * @param string $path
  327. * @return string
  328. */
  329. public function getLocalFile($path) {
  330. return $this->getWrapperStorage()->getLocalFile($path);
  331. }
  332. /**
  333. * check if a file or folder has been updated since $time
  334. *
  335. * @param string $path
  336. * @param int $time
  337. * @return bool
  338. *
  339. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  340. * returning true for other changes in the folder is optional
  341. */
  342. public function hasUpdated($path, $time) {
  343. return $this->getWrapperStorage()->hasUpdated($path, $time);
  344. }
  345. /**
  346. * get a cache instance for the storage
  347. *
  348. * @param string $path
  349. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  350. * @return \OC\Files\Cache\Cache
  351. */
  352. public function getCache($path = '', $storage = null) {
  353. if (!$storage) {
  354. $storage = $this;
  355. }
  356. return $this->getWrapperStorage()->getCache($path, $storage);
  357. }
  358. /**
  359. * get a scanner instance for the storage
  360. *
  361. * @param string $path
  362. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
  363. * @return \OC\Files\Cache\Scanner
  364. */
  365. public function getScanner($path = '', $storage = null) {
  366. if (!$storage) {
  367. $storage = $this;
  368. }
  369. return $this->getWrapperStorage()->getScanner($path, $storage);
  370. }
  371. /**
  372. * get the user id of the owner of a file or folder
  373. *
  374. * @param string $path
  375. * @return string
  376. */
  377. public function getOwner($path) {
  378. return $this->getWrapperStorage()->getOwner($path);
  379. }
  380. /**
  381. * get a watcher instance for the cache
  382. *
  383. * @param string $path
  384. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
  385. * @return \OC\Files\Cache\Watcher
  386. */
  387. public function getWatcher($path = '', $storage = null) {
  388. if (!$storage) {
  389. $storage = $this;
  390. }
  391. return $this->getWrapperStorage()->getWatcher($path, $storage);
  392. }
  393. public function getPropagator($storage = null) {
  394. if (!$storage) {
  395. $storage = $this;
  396. }
  397. return $this->getWrapperStorage()->getPropagator($storage);
  398. }
  399. public function getUpdater($storage = null) {
  400. if (!$storage) {
  401. $storage = $this;
  402. }
  403. return $this->getWrapperStorage()->getUpdater($storage);
  404. }
  405. /**
  406. * @return \OC\Files\Cache\Storage
  407. */
  408. public function getStorageCache() {
  409. return $this->getWrapperStorage()->getStorageCache();
  410. }
  411. /**
  412. * get the ETag for a file or folder
  413. *
  414. * @param string $path
  415. * @return string
  416. */
  417. public function getETag($path) {
  418. return $this->getWrapperStorage()->getETag($path);
  419. }
  420. /**
  421. * Returns true
  422. *
  423. * @return true
  424. */
  425. public function test() {
  426. return $this->getWrapperStorage()->test();
  427. }
  428. /**
  429. * Returns the wrapped storage's value for isLocal()
  430. *
  431. * @return bool wrapped storage's isLocal() value
  432. */
  433. public function isLocal() {
  434. return $this->getWrapperStorage()->isLocal();
  435. }
  436. /**
  437. * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
  438. *
  439. * @param string $class
  440. * @return bool
  441. */
  442. public function instanceOfStorage($class) {
  443. if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
  444. // FIXME Temporary fix to keep existing checks working
  445. $class = '\OCA\Files_Sharing\SharedStorage';
  446. }
  447. return is_a($this, $class) or $this->getWrapperStorage()->instanceOfStorage($class);
  448. }
  449. /**
  450. * Pass any methods custom to specific storage implementations to the wrapped storage
  451. *
  452. * @param string $method
  453. * @param array $args
  454. * @return mixed
  455. */
  456. public function __call($method, $args) {
  457. return call_user_func_array(array($this->getWrapperStorage(), $method), $args);
  458. }
  459. /**
  460. * A custom storage implementation can return an url for direct download of a give file.
  461. *
  462. * For now the returned array can hold the parameter url - in future more attributes might follow.
  463. *
  464. * @param string $path
  465. * @return array
  466. */
  467. public function getDirectDownload($path) {
  468. return $this->getWrapperStorage()->getDirectDownload($path);
  469. }
  470. /**
  471. * Get availability of the storage
  472. *
  473. * @return array [ available, last_checked ]
  474. */
  475. public function getAvailability() {
  476. return $this->getWrapperStorage()->getAvailability();
  477. }
  478. /**
  479. * Set availability of the storage
  480. *
  481. * @param bool $isAvailable
  482. */
  483. public function setAvailability($isAvailable) {
  484. $this->getWrapperStorage()->setAvailability($isAvailable);
  485. }
  486. /**
  487. * @param string $path the path of the target folder
  488. * @param string $fileName the name of the file itself
  489. * @return void
  490. * @throws InvalidPathException
  491. */
  492. public function verifyPath($path, $fileName) {
  493. $this->getWrapperStorage()->verifyPath($path, $fileName);
  494. }
  495. /**
  496. * @param IStorage $sourceStorage
  497. * @param string $sourceInternalPath
  498. * @param string $targetInternalPath
  499. * @return bool
  500. */
  501. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  502. if ($sourceStorage === $this) {
  503. return $this->copy($sourceInternalPath, $targetInternalPath);
  504. }
  505. return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  506. }
  507. /**
  508. * @param IStorage $sourceStorage
  509. * @param string $sourceInternalPath
  510. * @param string $targetInternalPath
  511. * @return bool
  512. */
  513. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  514. if ($sourceStorage === $this) {
  515. return $this->rename($sourceInternalPath, $targetInternalPath);
  516. }
  517. return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  518. }
  519. /**
  520. * @param string $path
  521. * @return array
  522. */
  523. public function getMetaData($path) {
  524. return $this->getWrapperStorage()->getMetaData($path);
  525. }
  526. /**
  527. * @param string $path
  528. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  529. * @param \OCP\Lock\ILockingProvider $provider
  530. * @throws \OCP\Lock\LockedException
  531. */
  532. public function acquireLock($path, $type, ILockingProvider $provider) {
  533. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  534. $this->getWrapperStorage()->acquireLock($path, $type, $provider);
  535. }
  536. }
  537. /**
  538. * @param string $path
  539. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  540. * @param \OCP\Lock\ILockingProvider $provider
  541. */
  542. public function releaseLock($path, $type, ILockingProvider $provider) {
  543. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  544. $this->getWrapperStorage()->releaseLock($path, $type, $provider);
  545. }
  546. }
  547. /**
  548. * @param string $path
  549. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  550. * @param \OCP\Lock\ILockingProvider $provider
  551. */
  552. public function changeLock($path, $type, ILockingProvider $provider) {
  553. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  554. $this->getWrapperStorage()->changeLock($path, $type, $provider);
  555. }
  556. }
  557. /**
  558. * @return bool
  559. */
  560. public function needsPartFile() {
  561. return $this->getWrapperStorage()->needsPartFile();
  562. }
  563. public function writeStream(string $path, $stream, int $size = null): int {
  564. $storage = $this->getWrapperStorage();
  565. if ($storage->instanceOfStorage(IWriteStreamStorage::class)) {
  566. /** @var IWriteStreamStorage $storage */
  567. return $storage->writeStream($path, $stream, $size);
  568. } else {
  569. $target = $this->fopen($path, 'w');
  570. list($count, $result) = \OC_Helper::streamCopy($stream, $target);
  571. fclose($stream);
  572. fclose($target);
  573. return $count;
  574. }
  575. }
  576. }