Storage.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  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 OCA\Files_Trashbin;
  30. use OC\Files\Filesystem;
  31. use OC\Files\Storage\Wrapper\Wrapper;
  32. use OCA\Files_Trashbin\Events\MoveToTrashEvent;
  33. use OCA\Files_Trashbin\Trash\ITrashManager;
  34. use OCP\Encryption\Exceptions\GenericEncryptionException;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\Mount\IMountPoint;
  37. use OCP\Files\Node;
  38. use OCP\Files\Storage\IStorage;
  39. use OCP\ILogger;
  40. use OCP\IUserManager;
  41. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  42. class Storage extends Wrapper {
  43. /** @var IMountPoint */
  44. private $mountPoint;
  45. /** @var IUserManager */
  46. private $userManager;
  47. /** @var ILogger */
  48. private $logger;
  49. /** @var EventDispatcherInterface */
  50. private $eventDispatcher;
  51. /** @var IRootFolder */
  52. private $rootFolder;
  53. /** @var ITrashManager */
  54. private $trashManager;
  55. private $trashEnabled = true;
  56. /**
  57. * Storage constructor.
  58. *
  59. * @param array $parameters
  60. * @param ITrashManager|null $trashManager
  61. * @param IUserManager|null $userManager
  62. * @param ILogger|null $logger
  63. * @param EventDispatcherInterface|null $eventDispatcher
  64. * @param IRootFolder|null $rootFolder
  65. */
  66. public function __construct(
  67. $parameters,
  68. ITrashManager $trashManager = null,
  69. IUserManager $userManager = null,
  70. ILogger $logger = null,
  71. EventDispatcherInterface $eventDispatcher = null,
  72. IRootFolder $rootFolder = null
  73. ) {
  74. $this->mountPoint = $parameters['mountPoint'];
  75. $this->trashManager = $trashManager;
  76. $this->userManager = $userManager;
  77. $this->logger = $logger;
  78. $this->eventDispatcher = $eventDispatcher;
  79. $this->rootFolder = $rootFolder;
  80. parent::__construct($parameters);
  81. }
  82. /**
  83. * Deletes the given file by moving it into the trashbin.
  84. *
  85. * @param string $path path of file or folder to delete
  86. *
  87. * @return bool true if the operation succeeded, false otherwise
  88. */
  89. public function unlink($path) {
  90. if ($this->trashEnabled) {
  91. try {
  92. return $this->doDelete($path, 'unlink');
  93. } catch (GenericEncryptionException $e) {
  94. // in case of a encryption exception we delete the file right away
  95. $this->logger->info(
  96. "Can't move file " . $path .
  97. " to the trash bin, therefore it was deleted right away");
  98. return $this->storage->unlink($path);
  99. }
  100. } else {
  101. return $this->storage->unlink($path);
  102. }
  103. }
  104. /**
  105. * Deletes the given folder by moving it into the trashbin.
  106. *
  107. * @param string $path path of folder to delete
  108. *
  109. * @return bool true if the operation succeeded, false otherwise
  110. */
  111. public function rmdir($path) {
  112. if ($this->trashEnabled) {
  113. return $this->doDelete($path, 'rmdir');
  114. } else {
  115. return $this->storage->rmdir($path);
  116. }
  117. }
  118. /**
  119. * check if it is a file located in data/user/files only files in the
  120. * 'files' directory should be moved to the trash
  121. *
  122. * @param $path
  123. * @return bool
  124. */
  125. protected function shouldMoveToTrash($path) {
  126. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  127. $parts = explode('/', $normalized);
  128. if (count($parts) < 4 || strpos($normalized, '/appdata_') === 0) {
  129. return false;
  130. }
  131. // check if there is a app which want to disable the trash bin for this file
  132. $fileId = $this->storage->getCache()->getId($path);
  133. $owner = $this->storage->getOwner($path);
  134. if ($owner === false || $this->storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class)) {
  135. $nodes = $this->rootFolder->getById($fileId);
  136. } else {
  137. $nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
  138. }
  139. foreach ($nodes as $node) {
  140. $event = $this->createMoveToTrashEvent($node);
  141. $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
  142. if ($event->shouldMoveToTrashBin() === false) {
  143. return false;
  144. }
  145. }
  146. if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * get move to trash event
  153. *
  154. * @param Node $node
  155. * @return MoveToTrashEvent
  156. */
  157. protected function createMoveToTrashEvent(Node $node) {
  158. return new MoveToTrashEvent($node);
  159. }
  160. /**
  161. * Run the delete operation with the given method
  162. *
  163. * @param string $path path of file or folder to delete
  164. * @param string $method either "unlink" or "rmdir"
  165. *
  166. * @return bool true if the operation succeeded, false otherwise
  167. */
  168. private function doDelete($path, $method) {
  169. if (
  170. !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
  171. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  172. || $this->shouldMoveToTrash($path) === false
  173. ) {
  174. return call_user_func([$this->storage, $method], $path);
  175. }
  176. // check permissions before we continue, this is especially important for
  177. // shared files
  178. if (!$this->isDeletable($path)) {
  179. return false;
  180. }
  181. $isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
  182. if (!$isMovedToTrash) {
  183. return call_user_func([$this->storage, $method], $path);
  184. } else {
  185. return true;
  186. }
  187. }
  188. /**
  189. * Setup the storage wrapper callback
  190. */
  191. public static function setupStorage() {
  192. $trashManager = \OC::$server->get(ITrashManager::class);
  193. $userManager = \OC::$server->get(IUserManager::class);
  194. $logger = \OC::$server->get(ILogger::class);
  195. $eventDispatcher = \OC::$server->get(EventDispatcherInterface::class);
  196. $rootFolder = \OC::$server->get(IRootFolder::class);
  197. Filesystem::addStorageWrapper(
  198. 'oc_trashbin',
  199. function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
  200. return new Storage(
  201. ['storage' => $storage, 'mountPoint' => $mountPoint],
  202. $trashManager,
  203. $userManager,
  204. $logger,
  205. $eventDispatcher,
  206. $rootFolder,
  207. );
  208. },
  209. 1);
  210. }
  211. public function getMountPoint() {
  212. return $this->mountPoint;
  213. }
  214. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  215. $sourceIsTrashbin = $sourceStorage->instanceOfStorage(Storage::class);
  216. try {
  217. // the fallback for moving between storage involves a copy+delete
  218. // we don't want to trigger the trashbin when doing the delete
  219. if ($sourceIsTrashbin) {
  220. /** @var Storage $sourceStorage */
  221. $sourceStorage->disableTrash();
  222. }
  223. $result = parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  224. if ($sourceIsTrashbin) {
  225. /** @var Storage $sourceStorage */
  226. $sourceStorage->enableTrash();
  227. }
  228. return $result;
  229. } catch (\Exception $e) {
  230. if ($sourceIsTrashbin) {
  231. /** @var Storage $sourceStorage */
  232. $sourceStorage->enableTrash();
  233. }
  234. throw $e;
  235. }
  236. }
  237. protected function disableTrash() {
  238. $this->trashEnabled = false;
  239. }
  240. protected function enableTrash() {
  241. $this->trashEnabled = true;
  242. }
  243. }