You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedMount.php 7.5KB

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