1
0

SharedMount.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Frédéric Fortier <frederic.fortier@oronospolytechnique.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  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_Sharing;
  30. use OC\Files\Filesystem;
  31. use OC\Files\Mount\MountPoint;
  32. use OC\Files\Mount\MoveableMount;
  33. use OC\Files\View;
  34. use OCP\Cache\CappedMemoryCache;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\Files\Events\InvalidateMountCacheEvent;
  37. use OCP\Files\Storage\IStorageFactory;
  38. use OCP\ICache;
  39. use OCP\IUser;
  40. use OCP\Share\Events\VerifyMountPointEvent;
  41. use Psr\Log\LoggerInterface;
  42. /**
  43. * Shared mount points can be moved by the user
  44. */
  45. class SharedMount extends MountPoint implements MoveableMount, ISharedMountPoint {
  46. /**
  47. * @var \OCA\Files_Sharing\SharedStorage $storage
  48. */
  49. protected $storage = null;
  50. /**
  51. * @var \OC\Files\View
  52. */
  53. private $recipientView;
  54. private IUser $user;
  55. /** @var \OCP\Share\IShare */
  56. private $superShare;
  57. /** @var \OCP\Share\IShare[] */
  58. private $groupedShares;
  59. private IEventDispatcher $eventDispatcher;
  60. private ICache $cache;
  61. public function __construct(
  62. $storage,
  63. array $mountpoints,
  64. $arguments,
  65. IStorageFactory $loader,
  66. View $recipientView,
  67. CappedMemoryCache $folderExistCache,
  68. IEventDispatcher $eventDispatcher,
  69. IUser $user,
  70. ICache $cache
  71. ) {
  72. $this->user = $user;
  73. $this->recipientView = $recipientView;
  74. $this->eventDispatcher = $eventDispatcher;
  75. $this->cache = $cache;
  76. $this->superShare = $arguments['superShare'];
  77. $this->groupedShares = $arguments['groupedShares'];
  78. $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache);
  79. $absMountPoint = '/' . $user->getUID() . '/files' . $newMountPoint;
  80. parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class);
  81. }
  82. /**
  83. * check if the parent folder exists otherwise move the mount point up
  84. *
  85. * @param \OCP\Share\IShare $share
  86. * @param SharedMount[] $mountpoints
  87. * @param CappedMemoryCache<bool> $folderExistCache
  88. * @return string
  89. */
  90. private function verifyMountPoint(
  91. \OCP\Share\IShare $share,
  92. array $mountpoints,
  93. CappedMemoryCache $folderExistCache
  94. ) {
  95. $cacheKey = $this->user->getUID() . '/' . $share->getId() . '/' . $share->getTarget();
  96. $cached = $this->cache->get($cacheKey);
  97. if ($cached !== null) {
  98. return $cached;
  99. }
  100. $mountPoint = basename($share->getTarget());
  101. $parent = dirname($share->getTarget());
  102. $event = new VerifyMountPointEvent($share, $this->recipientView, $parent);
  103. $this->eventDispatcher->dispatchTyped($event);
  104. $parent = $event->getParent();
  105. $cached = $folderExistCache->get($parent);
  106. if ($cached) {
  107. $parentExists = $cached;
  108. } else {
  109. $parentExists = $this->recipientView->is_dir($parent);
  110. $folderExistCache->set($parent, $parentExists);
  111. }
  112. if (!$parentExists) {
  113. $parent = Helper::getShareFolder($this->recipientView, $this->user->getUID());
  114. }
  115. $newMountPoint = $this->generateUniqueTarget(
  116. \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  117. $this->recipientView,
  118. $mountpoints
  119. );
  120. if ($newMountPoint !== $share->getTarget()) {
  121. $this->updateFileTarget($newMountPoint, $share);
  122. }
  123. $this->cache->set($cacheKey, $newMountPoint, 60 * 60);
  124. return $newMountPoint;
  125. }
  126. /**
  127. * update fileTarget in the database if the mount point changed
  128. *
  129. * @param string $newPath
  130. * @param \OCP\Share\IShare $share
  131. * @return bool
  132. */
  133. private function updateFileTarget($newPath, &$share) {
  134. $share->setTarget($newPath);
  135. foreach ($this->groupedShares as $tmpShare) {
  136. $tmpShare->setTarget($newPath);
  137. \OC::$server->getShareManager()->moveShare($tmpShare, $this->user->getUID());
  138. }
  139. $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user));
  140. }
  141. /**
  142. * @param string $path
  143. * @param View $view
  144. * @param SharedMount[] $mountpoints
  145. * @return mixed
  146. */
  147. private function generateUniqueTarget($path, $view, array $mountpoints) {
  148. $pathinfo = pathinfo($path);
  149. $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
  150. $name = $pathinfo['filename'];
  151. $dir = $pathinfo['dirname'];
  152. $i = 2;
  153. $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
  154. while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) {
  155. $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
  156. $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
  157. $i++;
  158. }
  159. return $path;
  160. }
  161. /**
  162. * Format a path to be relative to the /user/files/ directory
  163. *
  164. * @param string $path the absolute path
  165. * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  166. * @throws \OCA\Files_Sharing\Exceptions\BrokenPath
  167. */
  168. protected function stripUserFilesPath($path) {
  169. $trimmed = ltrim($path, '/');
  170. $split = explode('/', $trimmed);
  171. // it is not a file relative to data/user/files
  172. if (count($split) < 3 || $split[1] !== 'files') {
  173. \OCP\Server::get(LoggerInterface::class)->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
  174. throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  175. }
  176. // skip 'user' and 'files'
  177. $sliced = array_slice($split, 2);
  178. $relPath = implode('/', $sliced);
  179. return '/' . $relPath;
  180. }
  181. /**
  182. * Move the mount point to $target
  183. *
  184. * @param string $target the target mount point
  185. * @return bool
  186. */
  187. public function moveMount($target) {
  188. $relTargetPath = $this->stripUserFilesPath($target);
  189. $share = $this->storage->getShare();
  190. $result = true;
  191. try {
  192. $this->updateFileTarget($relTargetPath, $share);
  193. $this->setMountPoint($target);
  194. $this->storage->setMountPoint($relTargetPath);
  195. } catch (\Exception $e) {
  196. \OCP\Server::get(LoggerInterface::class)->error(
  197. 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  198. [
  199. 'app' => 'files_sharing',
  200. 'exception' => $e,
  201. ]
  202. );
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Remove the mount points
  208. *
  209. * @return bool
  210. */
  211. public function removeMount() {
  212. $mountManager = \OC\Files\Filesystem::getMountManager();
  213. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  214. $storage = $this->getStorage();
  215. $result = $storage->unshareStorage();
  216. $mountManager->removeMount($this->mountPoint);
  217. return $result;
  218. }
  219. /**
  220. * @return \OCP\Share\IShare
  221. */
  222. public function getShare() {
  223. return $this->superShare;
  224. }
  225. /**
  226. * @return \OCP\Share\IShare[]
  227. */
  228. public function getGroupedShares(): array {
  229. return $this->groupedShares;
  230. }
  231. /**
  232. * Get the file id of the root of the storage
  233. *
  234. * @return int
  235. */
  236. public function getStorageRootId() {
  237. return $this->getShare()->getNodeId();
  238. }
  239. /**
  240. * @return int
  241. */
  242. public function getNumericStorageId() {
  243. if (!is_null($this->getShare()->getNodeCacheEntry())) {
  244. return $this->getShare()->getNodeCacheEntry()->getStorageId();
  245. } else {
  246. $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  247. $query = $builder->select('storage')
  248. ->from('filecache')
  249. ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
  250. $result = $query->execute();
  251. $row = $result->fetch();
  252. $result->closeCursor();
  253. if ($row) {
  254. return (int)$row['storage'];
  255. }
  256. return -1;
  257. }
  258. }
  259. public function getMountType() {
  260. return 'shared';
  261. }
  262. }