Storage.php 7.6 KB

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