]> source.dussan.org Git - nextcloud-server.git/commitdiff
Scan the entire remote share at once by requesting the full file tree from the remote...
authorRobin Appelman <icewind@owncloud.com>
Mon, 19 May 2014 14:39:57 +0000 (16:39 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Sat, 14 Jun 2014 08:14:07 +0000 (10:14 +0200)
apps/files_sharing/ajax/external.php
apps/files_sharing/ajax/shareinfo.php [new file with mode: 0644]
apps/files_sharing/lib/external/scanner.php [new file with mode: 0644]
apps/files_sharing/lib/external/storage.php

index e7bf903f705fe7b7ca5867ce387fcd769b620d82..c25b34ab55248b2ffa05185bf90fb360b7b65fea 100644 (file)
@@ -23,6 +23,13 @@ $externalManager = new \OCA\Files_Sharing\External\Manager(
 );
 
 $mount = $externalManager->addShare($remote, $token, $password, $name, $owner);
-$result = $mount->getStorage()->file_exists('');
+/**
+ * @var \OCA\Files_Sharing\External\Storage $storage
+ */
+$storage = $mount->getStorage();
+$result = $storage->file_exists('');
+if($result){
+       $storage->getScanner()->scanAll();
+}
 
 echo json_encode($result);
diff --git a/apps/files_sharing/ajax/shareinfo.php b/apps/files_sharing/ajax/shareinfo.php
new file mode 100644 (file)
index 0000000..4aefdbe
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+if (!\OC_App::isEnabled('files_sharing')) {
+       exit;
+}
+
+if (!isset($_GET['t'])) {
+       \OC_Response::setStatus(400); //400 Bad Request
+       exit;
+}
+
+$token = $_GET['t'];
+
+$password = null;
+if (isset($_POST['password'])) {
+       $password = $_POST['password'];
+}
+
+$relativePath = null;
+if (isset($_GET['dir'])) {
+       $relativePath = $_GET['dir'];
+}
+
+$data = \OCA\Files_Sharing\Helper::setupFromToken($token, $relativePath, $password);
+
+$linkItem = $data['linkItem'];
+// Load the files
+$path = $data['realPath'];
+
+$rootInfo = \OC\Files\Filesystem::getFileInfo($path);
+$rootView = new \OC\Files\View('');
+
+/**
+ * @param \OCP\Files\FileInfo $dir
+ * @param \OC\Files\View $view
+ * @return array
+ */
+function getChildInfo($dir, $view) {
+       $children = $view->getDirectoryContent($dir->getPath());
+       $result = array();
+       foreach ($children as $child) {
+               $formated = \OCA\Files\Helper::formatFileInfo($child);
+               if ($child->getType() === 'dir') {
+                       $formated['children'] = getChildInfo($child, $view);
+               }
+               $result[] = $formated;
+       }
+       return $result;
+}
+
+$result = \OCA\Files\Helper::formatFileInfo($rootInfo);
+if ($rootInfo->getType() === 'dir') {
+       $result['children'] = getChildInfo($rootInfo, $rootView);
+}
+
+OCP\JSON::success(array('data' => $result));
diff --git a/apps/files_sharing/lib/external/scanner.php b/apps/files_sharing/lib/external/scanner.php
new file mode 100644 (file)
index 0000000..1f32d79
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\Files_Sharing\External;
+
+class Scanner extends \OC\Files\Cache\Scanner {
+       /**
+        * @var \OCA\Files_Sharing\External\Storage
+        */
+       protected $storage;
+
+       public function scanAll() {
+               $remote = $this->storage->getRemote();
+               $token = $this->storage->getToken();
+               $password = $this->storage->getPassword();
+               $url = $remote . '/index.php/apps/files_sharing/shareinfo?t=' . $token;
+
+               $ch = curl_init();
+
+               curl_setopt($ch, CURLOPT_URL, $url);
+               curl_setopt($ch, CURLOPT_POST, 1);
+               if ($password) {
+                       curl_setopt($ch, CURLOPT_POSTFIELDS,
+                               http_build_query(array('password' => $password)));
+               }
+               curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+               $result = curl_exec($ch);
+               curl_close($ch);
+
+               $data = json_decode($result, true);
+               if ($data['status'] === 'success') {
+                       $this->addResult($data['data'], '');
+               } else {
+                       throw new \Exception('Error while scanning remote share');
+               }
+       }
+
+       private function addResult($data, $path) {
+               $this->cache->put($path, $data);
+               if ($data['children']) {
+                       foreach ($data['children'] as $child) {
+                               $this->addResult($child, ltrim($path . '/' . $child['name'], '/'));
+                       }
+               }
+       }
+}
index 0e799a0e9e305f8b1e4b933d730cdc4fb895ce3f..741e219eff7fb19119a69025a492284a758309af 100644 (file)
@@ -27,6 +27,11 @@ class Storage extends \OC\Files\Storage\DAV implements ISharedStorage {
         */
        private $mountPoint;
 
+       /**
+        * @var string
+        */
+       private $token;
+
        /**
         * @var \OCA\Files_Sharing\External\Manager
         */
@@ -41,6 +46,7 @@ class Storage extends \OC\Files\Storage\DAV implements ISharedStorage {
                $secure = $protocol === 'https';
                $root .= '/public.php/webdav';
                $this->mountPoint = $options['mountpoint'];
+               $this->token = $options['token'];
                parent::__construct(array(
                        'secure' => $secure,
                        'host' => $host,
@@ -62,6 +68,14 @@ class Storage extends \OC\Files\Storage\DAV implements ISharedStorage {
                return $this->mountPoint;
        }
 
+       public function getToken() {
+               return $this->token;
+       }
+
+       public function getPassword() {
+               return $this->password;
+       }
+
        /**
         * @brief get id of the mount point
         * @return string
@@ -77,6 +91,17 @@ class Storage extends \OC\Files\Storage\DAV implements ISharedStorage {
                return $this->cache;
        }
 
+       /**
+        * @param string $path
+        * @return \OCA\Files_Sharing\External\Scanner
+        */
+       public function getScanner($path = '') {
+               if (!isset($this->scanner)) {
+                       $this->scanner = new Scanner($this);
+               }
+               return $this->scanner;
+       }
+
        public function rename($path1, $path2) {
                // if we renamed the mount point we need to adjust the mountpoint in the database
                if (Filesystem::normalizePath($this->mountPoint) === Filesystem::normalizePath($path1)) {