123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace OCA\Files_Sharing;
- use OC\Cache\CappedMemoryCache;
- use OC\Files\Filesystem;
- use OC\Files\Mount\MountPoint;
- use OC\Files\Mount\MoveableMount;
- use OC\Files\View;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\Storage\IStorageFactory;
- use OCP\Share\Events\VerifyMountPointEvent;
- class SharedMount extends MountPoint implements MoveableMount {
-
- protected $storage = null;
-
- private $recipientView;
-
- private $user;
-
- private $superShare;
-
- private $groupedShares;
-
- public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) {
- $this->user = $arguments['user'];
- $this->recipientView = $recipientView;
- $this->superShare = $arguments['superShare'];
- $this->groupedShares = $arguments['groupedShares'];
- $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache);
- $absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
- parent::__construct($storage, $absMountPoint, $arguments, $loader);
- }
-
- private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) {
- $mountPoint = basename($share->getTarget());
- $parent = dirname($share->getTarget());
- $event = new VerifyMountPointEvent($share, $this->recipientView, $parent);
-
- $dispatcher = \OC::$server->query(IEventDispatcher::class);
- $dispatcher->dispatchTyped($event);
- $parent = $event->getParent();
- if ($folderExistCache->hasKey($parent)) {
- $parentExists = $folderExistCache->get($parent);
- } else {
- $parentExists = $this->recipientView->is_dir($parent);
- $folderExistCache->set($parent, $parentExists);
- }
- if (!$parentExists) {
- $parent = Helper::getShareFolder($this->recipientView);
- }
- $newMountPoint = $this->generateUniqueTarget(
- \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
- $this->recipientView,
- $mountpoints
- );
- if ($newMountPoint !== $share->getTarget()) {
- $this->updateFileTarget($newMountPoint, $share);
- }
- return $newMountPoint;
- }
-
- private function updateFileTarget($newPath, &$share) {
- $share->setTarget($newPath);
- foreach ($this->groupedShares as $tmpShare) {
- $tmpShare->setTarget($newPath);
- \OC::$server->getShareManager()->moveShare($tmpShare, $this->user);
- }
- }
-
- private function generateUniqueTarget($path, $view, array $mountpoints) {
- $pathinfo = pathinfo($path);
- $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
- $name = $pathinfo['filename'];
- $dir = $pathinfo['dirname'];
- $i = 2;
- $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
- while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) {
- $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
- $absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
- $i++;
- }
- return $path;
- }
-
- protected function stripUserFilesPath($path) {
- $trimmed = ltrim($path, '/');
- $split = explode('/', $trimmed);
-
- if (count($split) < 3 || $split[1] !== 'files') {
- \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
- throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
- }
-
- $sliced = array_slice($split, 2);
- $relPath = implode('/', $sliced);
- return '/' . $relPath;
- }
-
- public function moveMount($target) {
- $relTargetPath = $this->stripUserFilesPath($target);
- $share = $this->storage->getShare();
- $result = true;
- try {
- $this->updateFileTarget($relTargetPath, $share);
- $this->setMountPoint($target);
- $this->storage->setMountPoint($relTargetPath);
- } catch (\Exception $e) {
- \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']);
- }
- return $result;
- }
-
- public function removeMount() {
- $mountManager = \OC\Files\Filesystem::getMountManager();
-
- $storage = $this->getStorage();
- $result = $storage->unshareStorage();
- $mountManager->removeMount($this->mountPoint);
- return $result;
- }
-
- public function getShare() {
- return $this->superShare;
- }
-
- public function getStorageRootId() {
- return $this->getShare()->getNodeId();
- }
-
- public function getNumericStorageId() {
- if (!is_null($this->getShare()->getNodeCacheEntry())) {
- return $this->getShare()->getNodeCacheEntry()->getStorageId();
- } else {
- $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
- $query = $builder->select('storage')
- ->from('filecache')
- ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
- $result = $query->execute();
- $row = $result->fetch();
- $result->closeCursor();
- if ($row) {
- return (int)$row['storage'];
- }
- return -1;
- }
- }
- public function getMountType() {
- return 'shared';
- }
- }
|