[Pkg-owncloud-commits] [owncloud] 34/55: Implement getPath for shared files

David Prévot taffit at moszumanska.debian.org
Wed Apr 23 19:52:00 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to annotated tag v5.0.16RC1
in repository owncloud.

commit 2af0ebf27aae3be9214a59381379c818d6f8369a
Author: Robin Appelman <icewind at owncloud.com>
Date:   Thu Apr 3 15:27:50 2014 +0200

    Implement getPath for shared files
---
 apps/files_sharing/lib/cache.php | 37 +++++++++++++++++++++++++++++++++++++
 apps/files_sharing/tests/api.php | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php
index baa632e..38b6a5f 100644
--- a/apps/files_sharing/lib/cache.php
+++ b/apps/files_sharing/lib/cache.php
@@ -336,4 +336,41 @@ class Shared_Cache extends Cache {
 		return $ids;
 	}
 
+	public function getPathById($id, $pathEnd = '') {
+		// direct shares are easy
+		if ($path = $this->getShareById($id)) {
+			return $path . $pathEnd;
+		} else {
+			// if the item is a direct share we try and get the path of the parent and append the name of the item to it
+			list($parent, $name) = $this->getParentInfo($id);
+			if ($parent > 0) {
+				return $this->getPathById($parent, '/' . $name . $pathEnd);
+			} else {
+				return null;
+			}
+		}
+	}
+
+	private function getShareById($id) {
+		$item = \OCP\Share::getItemSharedWithBySource('file', $id);
+		if ($item) {
+			return trim($item['file_target'], '/');
+		}
+		$item = \OCP\Share::getItemSharedWithBySource('folder', $id);
+		if ($item) {
+			return trim($item['file_target'], '/');
+		}
+		return null;
+	}
+
+	private function getParentInfo($id) {
+		$sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
+		$query = \OC_DB::prepare($sql);
+		$result = $query->execute(array($id));
+		if ($row = $result->fetchRow()) {
+			return array($row['parent'], $row['name']);
+		} else {
+			return array(-1, '');
+		}
+	}
 }
diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php
index 7fdbab3..c32c425 100644
--- a/apps/files_sharing/tests/api.php
+++ b/apps/files_sharing/tests/api.php
@@ -981,6 +981,45 @@ class Test_Files_Sharing_Api extends \PHPUnit_Framework_TestCase {
 		$this->assertSame($expectedResult, $shareApiDummy->correctPathTest($path, $folder));
 	}
 
+	public function testGetPathByIdDirectShare() {
+		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
+		\OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
+		$info = \OC\Files\Filesystem::getFileInfo('test.txt');
+		\OCP\Share::shareItem('file', $info['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
+		\OC_Util::tearDownFS();
+
+		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
+		$this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/test.txt'));
+		list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/Shared/test.txt');
+		/**
+		 * @var \OC\Files\Storage\Shared $sharedStorage
+		 */
+
+		$sharedCache = $sharedStorage->getCache();
+		$this->assertEquals('test.txt', $sharedCache->getPathById($info['fileid']));
+	}
+
+	public function testGetPathByIdShareSubFolder() {
+		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
+		\OC\Files\Filesystem::mkdir('foo');
+		\OC\Files\Filesystem::mkdir('foo/bar');
+		\OC\Files\Filesystem::touch('foo/bar/test.txt', 'bar');
+		$folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
+		$fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
+		\OCP\Share::shareItem('folder', $folderInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
+		\OC_Util::tearDownFS();
+
+		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
+		$this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/foo'));
+		list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/Shared/foo');
+		/**
+		 * @var \OC\Files\Storage\Shared $sharedStorage
+		 */
+
+		$sharedCache = $sharedStorage->getCache();
+		$this->assertEquals('foo', $sharedCache->getPathById($folderInfo['fileid']));
+		$this->assertEquals('foo/bar/test.txt', $sharedCache->getPathById($fileInfo['fileid']));
+	}
 }
 
 /**

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list