]> source.dussan.org Git - nextcloud-server.git/commitdiff
add function to extract filename from sharekey name + tests
authorBjoern Schiessle <schiessle@owncloud.com>
Mon, 3 Feb 2014 12:39:05 +0000 (13:39 +0100)
committerBjoern Schiessle <schiessle@owncloud.com>
Mon, 3 Feb 2014 12:39:05 +0000 (13:39 +0100)
apps/files_encryption/lib/keymanager.php
apps/files_encryption/tests/keymanager.php

index 17b8180bfdde31362f334489d5ce374e58e9d3eb..7abc565f60901e59d3b5d65bc7eb608830c4fb0c 100755 (executable)
@@ -437,7 +437,7 @@ class Keymanager {
                        $filename = pathinfo($filePath, PATHINFO_BASENAME);
                        foreach($view->getDirectoryContent($parentDir) as $content) {
                                $path = $content['path'];
-                               if (strpos($content['name'], $filename) === 0) {
+                               if (self::getFilenameFromShareKey($content['name'])  === $filename) {
                                        $view->unlink('/' . $userId . '/' . $path);
                                }
                        }
@@ -538,4 +538,20 @@ class Keymanager {
                return $targetPath;
 
        }
+
+       /**
+        * @brief extract filename from share key name
+        * @param string $shareKey (filename.userid.sharekey)
+        * @return mixed filename or false
+        */
+       protected static function getFilenameFromShareKey($shareKey) {
+               $parts = explode('.', $shareKey);
+
+               $filename = false;
+               if(count($parts) > 2) {
+                       $filename = implode('.', array_slice($parts, 0, count($parts)-2));
+               }
+
+               return $filename;
+       }
 }
index 58a57ee5af4a86e0c94a0428ae4627bae53b7ea7..6f32c50743ce8b86fb135357a909ae2f54df6a5f 100644 (file)
@@ -136,6 +136,17 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase {
                $this->assertArrayHasKey('key', $sslInfo);
        }
 
+       /**
+        * @small
+        */
+       function testGetFilenameFromShareKey() {
+               $this->assertEquals("file",
+                               \TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.user.shareKey"));
+               $this->assertEquals("file.name.with.dots",
+                               \TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.name.with.dots.user.shareKey"));
+               $this->assertFalse(\TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.txt"));
+       }
+
        /**
         * @medium
         */
@@ -234,3 +245,12 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase {
                \OC_FileProxy::$enabled = $proxyStatus;
        }
 }
+
+/**
+ * dummy class to access protected methods of \OCA\Encryption\Keymanager for testing
+ */
+class TestProtectedKeymanagerMethods extends \OCA\Encryption\Keymanager {
+       public static function testGetFilenameFromShareKey($sharekey) {
+               return self::getFilenameFromShareKey($sharekey);
+       }
+}
\ No newline at end of file