From: Joas Schilling Date: Tue, 17 Mar 2015 09:32:11 +0000 (+0100) Subject: Use insertIfNotExists() instead of manual logic X-Git-Tag: v8.1.0alpha1~199^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ba3e4ede3901a619307f07e2aa4d4d784ff07a48;p=nextcloud-server.git Use insertIfNotExists() instead of manual logic --- diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php index eb030656894..592cfc4f9c1 100644 --- a/apps/files_sharing/lib/external/manager.php +++ b/apps/files_sharing/lib/external/manager.php @@ -66,7 +66,7 @@ class Manager { * @param boolean $accepted * @param string $user * @param int $remoteId - * @return mixed + * @return Mount|null */ public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { @@ -74,32 +74,41 @@ class Manager { $accepted = $accepted ? 1 : 0; $name = Filesystem::normalizePath('/' . $name); - if ($accepted) { - $mountPoint = Files::buildNotExistingFileName('/', $name); - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); - $hash = md5($mountPoint); - } else { + if (!$accepted) { // To avoid conflicts with the mount point generation later, // we only use a temporary mount point name here. The real // mount point name will be generated when accepting the share, // using the original share item name. - $tmpMountPointName = Filesystem::normalizePath('/TemporaryMountPointName-' . $name); + $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; $mountPoint = $tmpMountPointName; $hash = md5($tmpMountPointName); - - $query = $this->connection->prepare('SELECT `id` FROM `*PREFIX*share_external` WHERE `user` = ? AND `mountpoint_hash` = ?', 1); - $query->execute([$user, $hash]); + $data = [ + 'remote' => $remote, + 'share_token' => $token, + 'password' => $password, + 'name' => $name, + 'owner' => $owner, + 'user' => $user, + 'mountpoint' => $mountPoint, + 'mountpoint_hash' => $hash, + 'accepted' => $accepted, + 'remote_id' => $remoteId, + ]; $i = 1; - while ($query->fetch()) { + while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { // The external share already exists for the user - $mountPoint = $tmpMountPointName . '-' . $i; - $hash = md5($mountPoint); - $query->execute([$user, $hash]); + $data['mountpoint'] = $tmpMountPointName . '-' . $i; + $data['mountpoint_hash'] = md5($data['mountpoint']); $i++; } + return null; } + $mountPoint = Files::buildNotExistingFileName('/', $name); + $mountPoint = Filesystem::normalizePath('/' . $mountPoint); + $hash = md5($mountPoint); + $query = $this->connection->prepare(' INSERT INTO `*PREFIX*share_external` (`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`) @@ -107,16 +116,14 @@ class Manager { '); $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); - if ($accepted) { - $options = array( - 'remote' => $remote, - 'token' => $token, - 'password' => $password, - 'mountpoint' => $mountPoint, - 'owner' => $owner - ); - return $this->mountShare($options); - } + $options = array( + 'remote' => $remote, + 'token' => $token, + 'password' => $password, + 'mountpoint' => $mountPoint, + 'owner' => $owner + ); + return $this->mountShare($options); } private function setupMounts() {