Storage.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 $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 storate wrapper callback
  190. */
  191. public static function setupStorage() {
  192. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  193. return new \OCA\Files_Trashbin\Storage(
  194. ['storage' => $storage, 'mountPoint' => $mountPoint],
  195. \OC::$server->query(ITrashManager::class),
  196. \OC::$server->getUserManager(),
  197. \OC::$server->getLogger(),
  198. \OC::$server->getEventDispatcher(),
  199. \OC::$server->getLazyRootFolder()
  200. );
  201. }, 1);
  202. }
  203. public function getMountPoint() {
  204. return $this->mountPoint;
  205. }
  206. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  207. $sourceIsTrashbin = $sourceStorage->instanceOfStorage(Storage::class);
  208. try {
  209. // the fallback for moving between storage involves a copy+delete
  210. // we don't want to trigger the trashbin when doing the delete
  211. if ($sourceIsTrashbin) {
  212. /** @var Storage $sourceStorage */
  213. $sourceStorage->disableTrash();
  214. }
  215. $result = $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  216. if ($sourceIsTrashbin) {
  217. /** @var Storage $sourceStorage */
  218. $sourceStorage->enableTrash();
  219. }
  220. return $result;
  221. } catch (\Exception $e) {
  222. if ($sourceIsTrashbin) {
  223. /** @var Storage $sourceStorage */
  224. $sourceStorage->enableTrash();
  225. }
  226. throw $e;
  227. }
  228. }
  229. protected function disableTrash() {
  230. $this->trashEnabled = false;
  231. }
  232. protected function enableTrash() {
  233. $this->trashEnabled = true;
  234. }
  235. }