]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use insertIfNotExists() instead of manual logic
authorJoas Schilling <nickvergessen@owncloud.com>
Tue, 17 Mar 2015 09:32:11 +0000 (10:32 +0100)
committerJoas Schilling <nickvergessen@owncloud.com>
Tue, 17 Mar 2015 15:03:24 +0000 (16:03 +0100)
apps/files_sharing/lib/external/manager.php

index eb030656894e710a5ef6ebb3fa378cbdcd411988..592cfc4f9c1480a7caca6dca98ef303cc20fc9f4 100644 (file)
@@ -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() {