1
0

SharedMount.php 7.1 KB

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