]> source.dussan.org Git - nextcloud-server.git/commitdiff
replace hook with storage wrapper
authorBjoern Schiessle <schiessle@owncloud.com>
Wed, 14 Jan 2015 20:06:26 +0000 (21:06 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Mon, 19 Jan 2015 08:16:15 +0000 (09:16 +0100)
apps/files_encryption/tests/trashbin.php
apps/files_sharing/tests/share.php
apps/files_sharing/tests/sharedmount.php
apps/files_sharing/tests/sharedstorage.php
apps/files_sharing/tests/updater.php
apps/files_trashbin/lib/hooks.php
apps/files_trashbin/lib/storage.php [new file with mode: 0644]
apps/files_trashbin/lib/trashbin.php
apps/files_versions/lib/storage.php

index d924b8ac77aac38eb9387cf152329e246e345fb6..b759c8e32fd1dd55cf08aff0f588efaac8544cda 100755 (executable)
@@ -158,31 +158,30 @@ class Trashbin extends TestCase {
                        . $filename2 . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
 
                // get files
-               $trashFiles = $this->view->getDirectoryContent(
-                       '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/');
+               $trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1);
 
-               $trashFileSuffix = null;
                // find created file with timestamp
+               $timestamp = null;
                foreach ($trashFiles as $file) {
-                       if (strpos($file['path'], $filename . '.d') !== false) {
-                               $path_parts = pathinfo($file['name']);
-                               $trashFileSuffix = $path_parts['extension'];
+                       if ($file['name'] === $filename) {
+                               $timestamp = $file['mtime'];
+                               break;
                        }
                }
 
                // check if we found the file we created
-               $this->assertNotNull($trashFileSuffix);
+               $this->assertNotNull($timestamp);
 
-               $this->assertTrue($this->view->is_dir('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.' . $trashFileSuffix));
+               $this->assertTrue($this->view->is_dir('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp));
 
                // check if key for admin not exists
                $this->assertTrue($this->view->file_exists(
-                       '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.' . $trashFileSuffix . '/fileKey'));
+                       '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp . '/fileKey'));
 
                // check if share key for admin not exists
                $this->assertTrue($this->view->file_exists(
                        '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename
-                       .  '.' . $trashFileSuffix . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
+                       . '.d' . $timestamp . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
        }
 
        /**
@@ -195,27 +194,26 @@ class Trashbin extends TestCase {
                $filename2 = $filename . '.backup'; // a second file with similar name
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
-               $cryptedFile2 = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename2, $this->dataShort);
+               file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
+               file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename2, $this->dataShort);
 
                // delete both files
                \OC\Files\Filesystem::unlink($filename);
                \OC\Files\Filesystem::unlink($filename2);
 
-               $trashFiles = $this->view->getDirectoryContent('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/');
+               $trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1);
 
-               $trashFileSuffix = null;
-               $trashFileSuffix2 = null;
                // find created file with timestamp
+               $timestamp = null;
                foreach ($trashFiles as $file) {
-                       if (strpos($file['path'], $filename . '.d') !== false) {
-                               $path_parts = pathinfo($file['name']);
-                               $trashFileSuffix = $path_parts['extension'];
+                       if ($file['name'] === $filename) {
+                               $timestamp = $file['mtime'];
+                               break;
                        }
                }
 
-               // prepare file information
-               $timestamp = str_replace('d', '', $trashFileSuffix);
+               // make sure that we have a timestamp
+               $this->assertNotNull($timestamp);
 
                // before calling the restore operation the keys shouldn't be there
                $this->assertFalse($this->view->file_exists(
@@ -225,7 +223,7 @@ class Trashbin extends TestCase {
                        . $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
 
                // restore first file
-               $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename . '.' . $trashFileSuffix, $filename, $timestamp));
+               $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename . '.d' . $timestamp, $filename, $timestamp));
 
                // check if file exists
                $this->assertTrue($this->view->file_exists(
index b8c8b70bd1f92e0d68e81cc41e7e626a37a58f73..9ae2e330649bd83cb67747ddd8e93594729fb623 100644 (file)
@@ -50,6 +50,7 @@ class Test_Files_Sharing extends OCA\Files_sharing\Tests\TestCase {
        }
 
        protected function tearDown() {
+               self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
                $this->view->unlink($this->filename);
                $this->view->deleteAll($this->folder);
 
index dd66ca05d3855d7dd2cf774d96478fcb94ee7bfe..715c22cf4ae876d3e2472567a6da03aeb84b2f3c 100644 (file)
@@ -186,6 +186,7 @@ class Test_Files_Sharing_Mount extends OCA\Files_sharing\Tests\TestCase {
                $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName"));
 
                //cleanup
+               self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
                \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'testGroup');
                \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup');
                \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup');
index 75373244508df2fd7bc7c470f27b3e313980cb9b..7ab1564bc3d7dbcfa6f4086995bfe96b97c7deb9 100644 (file)
@@ -29,7 +29,7 @@ class Test_Files_Sharing_Storage extends OCA\Files_sharing\Tests\TestCase {
 
        protected function setUp() {
                parent::setUp();
-
+               \OCA\Files_Trashbin\Trashbin::registerHooks();
                $this->folder = '/folder_share_storage_test';
 
                $this->filename = '/share-api-storage.txt';
index bc8deaf19b0f62737431f3b84e50ed7602cdabf8..1d6ec8caa615c22cdcd17e3d8265957cee4eb96d 100644 (file)
@@ -98,12 +98,12 @@ class Test_Files_Sharing_Updater extends OCA\Files_sharing\Tests\TestCase {
 
                // trashbin should contain the local file but not the mount point
                $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
-               $dirContent = $rootView->getDirectoryContent('files_trashbin/files');
-               $this->assertSame(1, count($dirContent));
-               $firstElement = reset($dirContent);
-               $ext = pathinfo($firstElement['path'], PATHINFO_EXTENSION);
-               $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/localFile.txt'));
-               $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/' . $this->folder));
+               $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2);
+               $this->assertSame(1, count($trashContent));
+               $firstElement = reset($trashContent);
+               $timestamp = $firstElement['mtime'];
+               $this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/localFile.txt'));
+               $this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.d' . $timestamp . '/' . $this->folder));
 
                //cleanup
                $rootView->deleteAll('files_trashin');
index b6f0fb7e547882bb2bccd80945b5298070a5b1e0..c6c69aaa23f564ed66e1ae2f0faad3bb67d784dc 100644 (file)
@@ -28,21 +28,6 @@ namespace OCA\Files_Trashbin;
 
 class Hooks {
 
-       /**
-        * Copy files to trash bin
-        * @param array $params
-        *
-        * This function is connected to the delete signal of OC_Filesystem
-        * to copy the file to the trash bin
-        */
-       public static function remove_hook($params) {
-
-               if ( \OCP\App::isEnabled('files_trashbin') ) {
-                       $path = $params['path'];
-                       Trashbin::move2trash($path);
-               }
-       }
-
        /**
         * clean up user specific settings if user gets deleted
         * @param array $params array with uid
diff --git a/apps/files_trashbin/lib/storage.php b/apps/files_trashbin/lib/storage.php
new file mode 100644 (file)
index 0000000..aa5d48b
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * ownCloud
+ *
+ * @copyright (C) 2015 ownCloud, Inc.
+ *
+ * @author 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_Trashbin;
+
+use OC\Files\Storage\Wrapper\Wrapper;
+
+class Storage extends Wrapper {
+
+       private $mountPoint;
+       // remember already deleted files to avoid infinite loops if the trash bin
+       // move files across storages
+       private $deletedFiles = array();
+
+       function __construct($parameters) {
+               $this->mountPoint = $parameters['mountPoint'];
+               parent::__construct($parameters);
+       }
+
+       public function unlink($path) {
+               $normalized = \OC\Files\Filesystem::normalizePath($this->mountPoint . '/' . $path);
+               $result = true;
+               if (!isset($this->deletedFiles[$normalized])) {
+                       $this->deletedFiles[$normalized] = $normalized;
+                       $parts = explode('/', $normalized);
+                       if (count($parts) > 3 && $parts[2] === 'files') {
+                               $filesPath = implode('/', array_slice($parts, 3));
+                               $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
+                       } else {
+                               $result = $this->storage->unlink($path);
+                       }
+                       unset($this->deletedFiles[$normalized]);
+               }
+
+               return $result;
+       }
+
+       /**
+        * Setup the storate wrapper callback
+        */
+       public static function setupStorage() {
+               \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
+                       return new \OCA\Files_Trashbin\Storage(array('storage' => $storage, 'mountPoint' => $mountPoint));
+               });
+       }
+
+}
index 26257bd38175ff711fc7f12b93ff6f4fcf692dc0..f5cebea6b789a0d18d3eebb4dfd5a71a517c77ce 100644 (file)
@@ -144,9 +144,10 @@ class Trashbin {
                $size = 0;
                list($owner, $ownerPath) = self::getUidAndFilename($file_path);
 
+               $view = new \OC\Files\View('/' . $user);
                // file has been deleted in between
-               if (empty($ownerPath)) {
-                       return false;
+               if (!$view->file_exists('/files/' . $file_path)) {
+                       return true;
                }
 
                self::setUpTrash($user);
@@ -165,7 +166,8 @@ class Trashbin {
                \OC_FileProxy::$enabled = false;
                $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp;
                try {
-                       $sizeOfAddedFiles = self::copy_recursive('/files/'.$file_path, $trashPath, $view);
+                       $sizeOfAddedFiles = $view->filesize('/files/' . $file_path);
+                       $view->rename('/files/' . $file_path, $trashPath);
                } catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) {
                        $sizeOfAddedFiles = false;
                        if ($view->file_exists($trashPath)) {
@@ -203,6 +205,8 @@ class Trashbin {
                        $ownerTrashSize += $size;
                        $ownerTrashSize -= self::expire($ownerTrashSize, $owner);
                }
+
+               return ($sizeOfAddedFiles === false) ? false : true;
        }
 
        /**
@@ -321,8 +325,8 @@ class Trashbin {
                        } else {
                                // if location no longer exists, restore file in the root directory
                                if ($location !== '/' &&
-                                       (!$view->is_dir('files' . $location) ||
-                                               !$view->isCreatable('files' . $location))
+                                       (!$view->is_dir('files/' . $location) ||
+                                       !$view->isCreatable('files/' . $location))
                                ) {
                                        $location = '';
                                }
@@ -918,12 +922,12 @@ class Trashbin {
         * register hooks
         */
        public static function registerHooks() {
-               //Listen to delete file signal
-               \OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Files_Trashbin\Hooks", "remove_hook");
+               // create storage wrapper on setup
+               \OCP\Util::connectHook('OC_Filesystem', 'setup', 'OCA\Files_Trashbin\Storage', 'setupStorage');
                //Listen to delete user signal
-               \OCP\Util::connectHook('OC_User', 'pre_deleteUser', "OCA\Files_Trashbin\Hooks", "deleteUser_hook");
+               \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook');
                //Listen to post write hook
-               \OCP\Util::connectHook('OC_Filesystem', 'post_write', "OCA\Files_Trashbin\Hooks", "post_write_hook");
+               \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook');
        }
 
        /**
index 0fbb80f425f6eb661a18e2abd486b83aceac5ea7..60a4c463fd5168d80eb8e0b3118798ba9e55a850 100644 (file)
@@ -327,7 +327,7 @@ class Storage {
                                                } else {
                                                        $versions[$key]['preview'] = \OCP\Util::linkToRoute('core_ajax_versions_preview', array('file' => $userFullPath, 'version' => $timestamp));
                                                }
-                                               $versions[$key]['path'] = $pathinfo['dirname'] . '/' . $filename;
+                                               $versions[$key]['path'] = \OC\Files\Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
                                                $versions[$key]['name'] = $versionedFile;
                                                $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
                                        }