SharedMount.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Frédéric Fortier <frederic.fortier@oronospolytechnique.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing;
  29. use OC\Cache\CappedMemoryCache;
  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\EventDispatcher\IEventDispatcher;
  35. use OCP\Files\Storage\IStorageFactory;
  36. use OCP\Share\Events\VerifyMountPointEvent;
  37. /**
  38. * Shared mount points can be moved by the user
  39. */
  40. class SharedMount extends MountPoint implements MoveableMount {
  41. /**
  42. * @var \OCA\Files_Sharing\SharedStorage $storage
  43. */
  44. protected $storage = null;
  45. /**
  46. * @var \OC\Files\View
  47. */
  48. private $recipientView;
  49. /**
  50. * @var string
  51. */
  52. private $user;
  53. /** @var \OCP\Share\IShare */
  54. private $superShare;
  55. /** @var \OCP\Share\IShare[] */
  56. private $groupedShares;
  57. /**
  58. * @param string $storage
  59. * @param SharedMount[] $mountpoints
  60. * @param array $arguments
  61. * @param IStorageFactory $loader
  62. * @param View $recipientView
  63. */
  64. public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) {
  65. $this->user = $arguments['user'];
  66. $this->recipientView = $recipientView;
  67. $this->superShare = $arguments['superShare'];
  68. $this->groupedShares = $arguments['groupedShares'];
  69. $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache);
  70. $absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
  71. parent::__construct($storage, $absMountPoint, $arguments, $loader);
  72. }
  73. /**
  74. * check if the parent folder exists otherwise move the mount point up
  75. *
  76. * @param \OCP\Share\IShare $share
  77. * @param SharedMount[] $mountpoints
  78. * @return string
  79. */
  80. private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) {
  81. $mountPoint = basename($share->getTarget());
  82. $parent = dirname($share->getTarget());
  83. $event = new VerifyMountPointEvent($share, $this->recipientView, $parent);
  84. /** @var IEventDispatcher $dispatcher */
  85. $dispatcher = \OC::$server->query(IEventDispatcher::class);
  86. $dispatcher->dispatchTyped($event);
  87. $parent = $event->getParent();
  88. if ($folderExistCache->hasKey($parent)) {
  89. $parentExists = $folderExistCache->get($parent);
  90. } else {
  91. $parentExists = $this->recipientView->is_dir($parent);
  92. $folderExistCache->set($parent, $parentExists);
  93. }
  94. if (!$parentExists) {
  95. $parent = Helper::getShareFolder($this->recipientView);
  96. }
  97. $newMountPoint = $this->generateUniqueTarget(
  98. \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  99. $this->recipientView,
  100. $mountpoints
  101. );
  102. if ($newMountPoint !== $share->getTarget()) {
  103. $this->updateFileTarget($newMountPoint, $share);
  104. }
  105. return $newMountPoint;
  106. }
  107. /**
  108. * update fileTarget in the database if the mount point changed
  109. *
  110. * @param string $newPath
  111. * @param \OCP\Share\IShare $share
  112. * @return bool
  113. */
  114. private function updateFileTarget($newPath, &$share) {
  115. $share->setTarget($newPath);
  116. foreach ($this->groupedShares as $tmpShare) {
  117. $tmpShare->setTarget($newPath);
  118. \OC::$server->getShareManager()->moveShare($tmpShare, $this->user);
  119. }
  120. }
  121. /**
  122. * @param string $path
  123. * @param View $view
  124. * @param SharedMount[] $mountpoints
  125. * @return mixed
  126. */
  127. private function generateUniqueTarget($path, $view, array $mountpoints) {
  128. $pathinfo = pathinfo($path);
  129. $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
  130. $name = $pathinfo['filename'];
  131. $dir = $pathinfo['dirname'];
  132. $i = 2;
  133. $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
  134. while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) {
  135. $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
  136. $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
  137. $i++;
  138. }
  139. return $path;
  140. }
  141. /**
  142. * Format a path to be relative to the /user/files/ directory
  143. *
  144. * @param string $path the absolute path
  145. * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  146. * @throws \OCA\Files_Sharing\Exceptions\BrokenPath
  147. */
  148. protected function stripUserFilesPath($path) {
  149. $trimmed = ltrim($path, '/');
  150. $split = explode('/', $trimmed);
  151. // it is not a file relative to data/user/files
  152. if (count($split) < 3 || $split[1] !== 'files') {
  153. \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
  154. throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  155. }
  156. // skip 'user' and 'files'
  157. $sliced = array_slice($split, 2);
  158. $relPath = implode('/', $sliced);
  159. return '/' . $relPath;
  160. }
  161. /**
  162. * Move the mount point to $target
  163. *
  164. * @param string $target the target mount point
  165. * @return bool
  166. */
  167. public function moveMount($target) {
  168. $relTargetPath = $this->stripUserFilesPath($target);
  169. $share = $this->storage->getShare();
  170. $result = true;
  171. try {
  172. $this->updateFileTarget($relTargetPath, $share);
  173. $this->setMountPoint($target);
  174. $this->storage->setMountPoint($relTargetPath);
  175. } catch (\Exception $e) {
  176. \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']);
  177. }
  178. return $result;
  179. }
  180. /**
  181. * Remove the mount points
  182. *
  183. * @return bool
  184. */
  185. public function removeMount() {
  186. $mountManager = \OC\Files\Filesystem::getMountManager();
  187. /** @var $storage \OCA\Files_Sharing\SharedStorage */
  188. $storage = $this->getStorage();
  189. $result = $storage->unshareStorage();
  190. $mountManager->removeMount($this->mountPoint);
  191. return $result;
  192. }
  193. /**
  194. * @return \OCP\Share\IShare
  195. */
  196. public function getShare() {
  197. return $this->superShare;
  198. }
  199. /**
  200. * Get the file id of the root of the storage
  201. *
  202. * @return int
  203. */
  204. public function getStorageRootId() {
  205. return $this->getShare()->getNodeId();
  206. }
  207. /**
  208. * @return int
  209. */
  210. public function getNumericStorageId() {
  211. if (!is_null($this->getShare()->getNodeCacheEntry())) {
  212. return $this->getShare()->getNodeCacheEntry()->getStorageId();
  213. } else {
  214. $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  215. $query = $builder->select('storage')
  216. ->from('filecache')
  217. ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
  218. $result = $query->execute();
  219. $row = $result->fetch();
  220. $result->closeCursor();
  221. if ($row) {
  222. return (int)$row['storage'];
  223. }
  224. return -1;
  225. }
  226. }
  227. public function getMountType() {
  228. return 'shared';
  229. }
  230. }