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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. \OCP\Util::writeLog('file sharing',
  146. 'Can not strip userid and "files/" from path: ' . $path,
  147. \OCP\Util::ERROR);
  148. throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  149. }
  150. // skip 'user' and 'files'
  151. $sliced = array_slice($split, 2);
  152. $relPath = implode('/', $sliced);
  153. return '/' . $relPath;
  154. }
  155. /**
  156. * Move the mount point to $target
  157. *
  158. * @param string $target the target mount point
  159. * @return bool
  160. */
  161. public function moveMount($target) {
  162. $relTargetPath = $this->stripUserFilesPath($target);
  163. $share = $this->storage->getShare();
  164. $result = true;
  165. try {
  166. $this->updateFileTarget($relTargetPath, $share);
  167. $this->setMountPoint($target);
  168. $this->storage->setMountPoint($relTargetPath);
  169. } catch (\Exception $e) {
  170. \OCP\Util::writeLog('file sharing',
  171. 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  172. \OCP\Util::ERROR);
  173. }
  174. return $result;
  175. }
  176. /**
  177. * Remove the mount points
  178. *
  179. * @return bool
  180. */
  181. public function removeMount() {
  182. $mountManager = \OC\Files\Filesystem::getMountManager();
  183. /** @var $storage \OCA\Files_Sharing\SharedStorage */
  184. $storage = $this->getStorage();
  185. $result = $storage->unshareStorage();
  186. $mountManager->removeMount($this->mountPoint);
  187. return $result;
  188. }
  189. /**
  190. * @return \OCP\Share\IShare
  191. */
  192. public function getShare() {
  193. return $this->superShare;
  194. }
  195. /**
  196. * Get the file id of the root of the storage
  197. *
  198. * @return int
  199. */
  200. public function getStorageRootId() {
  201. return $this->getShare()->getNodeId();
  202. }
  203. /**
  204. * @return int
  205. */
  206. public function getNumericStorageId() {
  207. if (!is_null($this->getShare()->getNodeCacheEntry())) {
  208. return $this->getShare()->getNodeCacheEntry()->getStorageId();
  209. } else {
  210. $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  211. $query = $builder->select('storage')
  212. ->from('filecache')
  213. ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
  214. $result = $query->execute();
  215. $row = $result->fetch();
  216. $result->closeCursor();
  217. if ($row) {
  218. return (int)$row['storage'];
  219. }
  220. return -1;
  221. }
  222. }
  223. public function getMountType() {
  224. return 'shared';
  225. }
  226. }