diff options
author | Björn Schießle <schiessle@owncloud.com> | 2014-05-21 14:07:49 -0400 |
---|---|---|
committer | Björn Schießle <schiessle@owncloud.com> | 2014-05-21 14:07:49 -0400 |
commit | 14a953fbe01a3d26e1330ea224ab71928a2f93c1 (patch) | |
tree | 1e8feffaf4e6deded6c1b54b5565646a70374a6d /apps/files_sharing/lib | |
parent | c586d1f4d550563310b998a724309a6e645e8729 (diff) | |
parent | 5f403f3e920ea8f6ed5d0441762b3e4f4a9bd38e (diff) | |
download | nextcloud-server-14a953fbe01a3d26e1330ea224ab71928a2f93c1.tar.gz nextcloud-server-14a953fbe01a3d26e1330ea224ab71928a2f93c1.zip |
Merge pull request #8416 from owncloud/sharing_keep_mount_points_visible
[sharing] move the mount point up if the parent folder no longer exists
Diffstat (limited to 'apps/files_sharing/lib')
-rw-r--r-- | apps/files_sharing/lib/helper.php | 22 | ||||
-rw-r--r-- | apps/files_sharing/lib/proxy.php | 69 | ||||
-rw-r--r-- | apps/files_sharing/lib/share/file.php | 11 | ||||
-rw-r--r-- | apps/files_sharing/lib/sharedstorage.php | 113 | ||||
-rw-r--r-- | apps/files_sharing/lib/updater.php | 9 |
5 files changed, 166 insertions, 58 deletions
diff --git a/apps/files_sharing/lib/helper.php b/apps/files_sharing/lib/helper.php index 71b496ab944..49546f012a6 100644 --- a/apps/files_sharing/lib/helper.php +++ b/apps/files_sharing/lib/helper.php @@ -180,4 +180,26 @@ class Helper { return $relPath; } + + /** + * check if file name already exists and generate unique target + * + * @param string $path + * @param array $excludeList + * @param \OC\Files\View $view + * @return string $path + */ + public static function generateUniqueTarget($path, $excludeList, $view) { + $pathinfo = pathinfo($path); + $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : ''; + $name = $pathinfo['filename']; + $dir = $pathinfo['dirname']; + $i = 2; + while ($view->file_exists($path) || in_array($path, $excludeList)) { + $path = \OC\Files\Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext); + $i++; + } + + return $path; + } } diff --git a/apps/files_sharing/lib/proxy.php b/apps/files_sharing/lib/proxy.php new file mode 100644 index 00000000000..c899a4b4dd3 --- /dev/null +++ b/apps/files_sharing/lib/proxy.php @@ -0,0 +1,69 @@ +<?php +/** + * ownCloud + * + * @author Bjoern Schiessle + * @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Files\Share; + +class Proxy extends \OC_FileProxy { + + /** + * check if the deleted folder contains share mount points and move them + * up to the parent + * + * @param string $path + */ + public function preUnlink($path) { + $this->moveMountPointsUp($path); + } + + /** + * check if the deleted folder contains share mount points and move them + * up to the parent + * + * @param string $path + */ + public function preRmdir($path) { + $this->moveMountPointsUp($path); + } + + /** + * move share mount points up to the parent + * + * @param string $path + */ + private function moveMountPointsUp($path) { + $view = new \OC\Files\View('/'); + + // find share mount points within $path and move them up to the parent folder + // before we delete $path + $mountManager = \OC\Files\Filesystem::getMountManager(); + $mountedShares = $mountManager->findIn($path); + foreach ($mountedShares as $mount) { + if ($mount->getStorage() instanceof \OC\Files\Storage\Shared) { + $mountPoint = $mount->getMountPoint(); + $mountPointName = $mount->getMountPointName(); + $target = \OCA\Files_Sharing\Helper::generateUniqueTarget(dirname($path) . '/' . $mountPointName, array(), $view); + $view->rename($mountPoint, $target); + } + } + } + +} diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index af71786b104..91595461a61 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -75,16 +75,7 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { $excludeList = array_merge($excludeList, $exclude); } - $pathinfo = pathinfo($target); - $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : ''; - $name = $pathinfo['filename']; - $i = 2; - while ($view->file_exists($target) || in_array($target, $excludeList)) { - $target = '/' . $name . ' ('.$i.')' . $ext; - $i++; - } - - return $target; + return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view); } public function formatItems($items, $format, $parameters = null) { diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 4b69276d05a..c18e30966f0 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -305,27 +305,12 @@ class Shared extends \OC\Files\Storage\Common { $relTargetPath = $this->stripUserFilesPath($targetPath); - // if the user renames a mount point from a group share we need to create a new db entry - // for the unique name - if ($this->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && $this->uniqueNameSet() === false) { - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,' - .' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' - .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'); - $arguments = array($this->share['item_type'], $this->share['item_source'], $this->share['item_target'], - 2, \OCP\User::getUser(), $this->share['uid_owner'], $this->share['permissions'], $this->share['stime'], $this->share['file_source'], - $relTargetPath, $this->share['token'], $this->share['id']); - - } else { - // rename mount point - $query = \OC_DB::prepare( - 'Update `*PREFIX*share` - SET `file_target` = ? - WHERE `id` = ?' - ); - $arguments = array($relTargetPath, $this->getShareId()); + if ($relTargetPath === false) { + \OCP\Util::writeLog('file sharing', 'Wrong target path given: ' . $targetPath, \OCP\Util::ERROR); + return false; } - $result = $query->execute($arguments); + $result = self::updateFileTarget($relTargetPath, $this->share); if ($result) { // update the mount manager with the new paths @@ -343,9 +328,39 @@ class Shared extends \OC\Files\Storage\Common { \OCP\Util::ERROR); } - return $result; + return (bool)$result; } + /** + * @update fileTarget in the database if the mount point changed + * @param string $newPath + * @param array $share reference to the share which should be modified + * @return type + */ + private static function updateFileTarget($newPath, &$share) { + // if the user renames a mount point from a group share we need to create a new db entry + // for the unique name + if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && + (isset($share['unique_name']) && $share['unique_name'])) { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,' + .' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' + .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'); + $arguments = array($share['item_type'], $share['item_source'], $share['item_target'], + 2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'], + $newPath, $share['token'], $share['id']); + + } else { + // rename mount point + $query = \OC_DB::prepare( + 'Update `*PREFIX*share` + SET `file_target` = ? + WHERE `id` = ?' + ); + $arguments = array($newPath, $share['id']); + } + + return $query->execute($arguments); + } public function rename($path1, $path2) { @@ -471,6 +486,7 @@ class Shared extends \OC\Files\Storage\Common { || $shares ) { foreach ($shares as $share) { + self::verifyMountPoint($share); \OC\Files\Filesystem::mount('\OC\Files\Storage\Shared', array( 'share' => $share, @@ -481,19 +497,39 @@ class Shared extends \OC\Files\Storage\Common { } /** - * return mount point of share, relative to data/user/files - * @return string + * check if the parent folder exists otherwise move the mount point up + * + * @param array $share reference to the share we want to check */ - public function getMountPoint() { - return $this->share['file_target']; + private static function verifyMountPoint(&$share) { + $mountPoint = basename($share['file_target']); + $parent = dirname($share['file_target']); + + while (!\OC\Files\Filesystem::is_dir($parent)) { + $parent = dirname($parent); + } + + $newMountPoint = \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint); + + if($newMountPoint !== $share['file_target']) { + $newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget( + $newMountPoint, + array(), + new \OC\Files\View('/' . \OCP\User::getUser() . '/files') + ); + self::updateFileTarget($newMountPoint, $share); + $share['file_target'] = $newMountPoint; + + } } /** - * get share type - * @return integer can be single user share (0) group share (1), unique group share name (2) + * return mount point of share, relative to data/user/files + * + * @return string */ - private function getShareType() { - return $this->share['share_type']; + public function getMountPoint() { + return $this->share['file_target']; } private function setMountPoint($path) { @@ -501,30 +537,17 @@ class Shared extends \OC\Files\Storage\Common { } /** - * does the group share already has a user specific unique name - * @return bool - */ - private function uniqueNameSet() { - return (isset($this->share['unique_name']) && $this->share['unique_name']); - } - - /** * the share now uses a unique name of this user + * + * @brief the share now uses a unique name of this user */ private function setUniqueName() { $this->share['unique_name'] = true; } /** - * get share ID - * @return integer unique share ID - */ - private function getShareId() { - return $this->share['id']; - } - - /** - * get the user who shared the file + * @brief get the user who shared the file + * * @return string */ public function getSharedFrom() { diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index 21d67caad9d..5cb2b638e5a 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -115,11 +115,14 @@ class Shared_Updater { * @param array $params */ static public function deleteHook($params) { - self::correctFolders($params['path']); - $fileInfo = \OC\Files\Filesystem::getFileInfo($params['path']); + $path = $params['path']; + self::correctFolders($path); + + $fileInfo = \OC\Files\Filesystem::getFileInfo($path); + // mark file as deleted so that we can clean up the share table if // the file was deleted successfully - self::$toRemove[$params['path']] = $fileInfo['fileid']; + self::$toRemove[$path] = $fileInfo['fileid']; } /** |