SharedMount.php 7.0 KB

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