You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Storage.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Trashbin;
  28. use OC\Files\Filesystem;
  29. use OC\Files\Storage\Wrapper\Wrapper;
  30. use OC\Files\View;
  31. use OCA\Files_Trashbin\Events\MoveToTrashEvent;
  32. use OCA\Files_Trashbin\Trash\ITrashManager;
  33. use OCP\Encryption\Exceptions\GenericEncryptionException;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Mount\IMountPoint;
  36. use OCP\Files\Node;
  37. use OCP\ILogger;
  38. use OCP\IUserManager;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  40. class Storage extends Wrapper {
  41. /** @var IMountPoint */
  42. private $mountPoint;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var ILogger */
  46. private $logger;
  47. /** @var EventDispatcherInterface */
  48. private $eventDispatcher;
  49. /** @var IRootFolder */
  50. private $rootFolder;
  51. /** @var ITrashManager */
  52. private $trashManager;
  53. /**
  54. * Storage constructor.
  55. *
  56. * @param array $parameters
  57. * @param ITrashManager $trashManager
  58. * @param IUserManager|null $userManager
  59. * @param ILogger|null $logger
  60. * @param EventDispatcherInterface|null $eventDispatcher
  61. * @param IRootFolder|null $rootFolder
  62. */
  63. public function __construct(
  64. $parameters,
  65. ITrashManager $trashManager = null,
  66. IUserManager $userManager = null,
  67. ILogger $logger = null,
  68. EventDispatcherInterface $eventDispatcher = null,
  69. IRootFolder $rootFolder = null
  70. ) {
  71. $this->mountPoint = $parameters['mountPoint'];
  72. $this->trashManager = $trashManager;
  73. $this->userManager = $userManager;
  74. $this->logger = $logger;
  75. $this->eventDispatcher = $eventDispatcher;
  76. $this->rootFolder = $rootFolder;
  77. parent::__construct($parameters);
  78. }
  79. /**
  80. * Deletes the given file by moving it into the trashbin.
  81. *
  82. * @param string $path path of file or folder to delete
  83. *
  84. * @return bool true if the operation succeeded, false otherwise
  85. */
  86. public function unlink($path) {
  87. try {
  88. return $this->doDelete($path, 'unlink');
  89. } catch (GenericEncryptionException $e) {
  90. // in case of a encryption exception we delete the file right away
  91. $this->logger->info(
  92. "Can't move file" . $path .
  93. "to the trash bin, therefore it was deleted right away");
  94. return $this->storage->unlink($path);
  95. }
  96. }
  97. /**
  98. * Deletes the given folder by moving it into the trashbin.
  99. *
  100. * @param string $path path of folder to delete
  101. *
  102. * @return bool true if the operation succeeded, false otherwise
  103. */
  104. public function rmdir($path) {
  105. return $this->doDelete($path, 'rmdir');
  106. }
  107. /**
  108. * check if it is a file located in data/user/files only files in the
  109. * 'files' directory should be moved to the trash
  110. *
  111. * @param $path
  112. * @return bool
  113. */
  114. protected function shouldMoveToTrash($path) {
  115. // check if there is a app which want to disable the trash bin for this file
  116. $fileId = $this->storage->getCache()->getId($path);
  117. $nodes = $this->rootFolder->getById($fileId);
  118. foreach ($nodes as $node) {
  119. $event = $this->createMoveToTrashEvent($node);
  120. $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
  121. if ($event->shouldMoveToTrashBin() === false) {
  122. return false;
  123. }
  124. }
  125. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  126. $parts = explode('/', $normalized);
  127. if (count($parts) < 4) {
  128. return false;
  129. }
  130. if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * get move to trash event
  137. *
  138. * @param Node $node
  139. * @return MoveToTrashEvent
  140. */
  141. protected function createMoveToTrashEvent(Node $node) {
  142. return new MoveToTrashEvent($node);
  143. }
  144. /**
  145. * Run the delete operation with the given method
  146. *
  147. * @param string $path path of file or folder to delete
  148. * @param string $method either "unlink" or "rmdir"
  149. *
  150. * @return bool true if the operation succeeded, false otherwise
  151. */
  152. private function doDelete($path, $method) {
  153. if (
  154. !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
  155. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  156. || $this->shouldMoveToTrash($path) === false
  157. ) {
  158. return call_user_func([$this->storage, $method], $path);
  159. }
  160. // check permissions before we continue, this is especially important for
  161. // shared files
  162. if (!$this->isDeletable($path)) {
  163. return false;
  164. }
  165. $isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
  166. if (!$isMovedToTrash) {
  167. return call_user_func([$this->storage, $method], $path);
  168. } else {
  169. return true;
  170. }
  171. }
  172. /**
  173. * Setup the storate wrapper callback
  174. */
  175. public static function setupStorage() {
  176. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  177. return new \OCA\Files_Trashbin\Storage(
  178. ['storage' => $storage, 'mountPoint' => $mountPoint],
  179. \OC::$server->query(ITrashManager::class),
  180. \OC::$server->getUserManager(),
  181. \OC::$server->getLogger(),
  182. \OC::$server->getEventDispatcher(),
  183. \OC::$server->getLazyRootFolder()
  184. );
  185. }, 1);
  186. }
  187. public function getMountPoint() {
  188. return $this->mountPoint;
  189. }
  190. }