Storage.php 7.5 KB

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