From 7c68025010f3aeb91b84663fb3239ae0d038b4e2 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Thu, 30 Jan 2014 11:18:46 +0100 Subject: adding share owner information to the file list --- apps/files_sharing/lib/share/file.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index ec0f368386f..5e00050fe1e 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -94,6 +94,9 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { $file['mtime'] = $item['mtime']; $file['encrypted'] = $item['encrypted']; $file['etag'] = $item['etag']; + $file['uid_owner'] = $item['uid_owner']; + $file['displayname_owner'] = $item['displayname_owner']; + $storage = \OC\Files\Filesystem::getStorage('/'); $cache = $storage->getCache(); if ($item['encrypted'] or ($item['unencrypted_size'] > 0 and $cache->getMimetype($item['mimetype']) === 'httpd/unix-directory')) { -- cgit v1.2.3 From 179fbada324bb51cfce6fde1818560b3ea739061 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Thu, 30 Jan 2014 15:10:42 +0100 Subject: show share owner within shared folders as well --- apps/files_sharing/lib/cache.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index aadc54e4a7f..27602f69abf 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -135,6 +135,7 @@ class Shared_Cache extends Cache { return $files; } else { if ($cache = $this->getSourceCache($folder)) { + $parent = $this->storage->getFile($folder); $sourceFolderContent = $cache->getFolderContents($this->files[$folder]); foreach ($sourceFolderContent as $key => $c) { $ownerPathParts = explode('/', \OC_Filesystem::normalizePath($c['path'])); @@ -144,6 +145,8 @@ class Shared_Cache extends Cache { $usersPath .= '/'.$part; } $sourceFolderContent[$key]['usersPath'] = $usersPath; + $sourceFolderContent[$key]['uid_owner'] = $parent['uid_owner']; + $sourceFolderContent[$key]['displayname_owner'] = $parent['uid_owner']; } return $sourceFolderContent; -- cgit v1.2.3 From da386aad5950235d6d1089f18b5ef4260057d78e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 28 Feb 2014 14:23:07 +0100 Subject: Allow re-using already known fileinfo when calculating folder sizes --- apps/files_sharing/lib/cache.php | 3 ++- lib/private/files/cache/cache.php | 12 ++++++++---- lib/private/files/cache/homecache.php | 13 ++++++++++--- lib/private/files/cache/scanner.php | 8 +++++--- lib/private/files/cache/updater.php | 4 ++-- 5 files changed, 27 insertions(+), 13 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index 4b0da0b002d..579780dcd2b 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -354,9 +354,10 @@ class Shared_Cache extends Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { if ($cache = $this->getSourceCache($path)) { return $cache->calculateFolderSize($this->files[$path]); } diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 9b18257088c..d886fd0fe76 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -498,9 +498,10 @@ class Cache { * update the folder size and the size of all parent folders * * @param string|boolean $path + * @param array $data (optional) meta data of the folder */ - public function correctFolderSize($path) { - $this->calculateFolderSize($path); + public function correctFolderSize($path, $data = null) { + $this->calculateFolderSize($path, $data); if ($path !== '') { $parent = dirname($path); if ($parent === '.' or $parent === '/') { @@ -514,11 +515,14 @@ class Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { $totalSize = 0; - $entry = $this->get($path); + if (is_null($entry)) { + $entry = $this->get($path); + } if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { $id = $entry['fileid']; $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2, ' . diff --git a/lib/private/files/cache/homecache.php b/lib/private/files/cache/homecache.php index a7c310a3782..fad39a7fdad 100644 --- a/lib/private/files/cache/homecache.php +++ b/lib/private/files/cache/homecache.php @@ -13,15 +13,21 @@ class HomeCache extends Cache { * get the size of a folder and set it in the cache * * @param string $path + * @param array $entry (optional) meta data of the folder * @return int */ - public function calculateFolderSize($path) { + public function calculateFolderSize($path, $entry = null) { if ($path !== '/' and $path !== '' and $path !== 'files') { - return parent::calculateFolderSize($path); + return parent::calculateFolderSize($path, $entry); + } elseif ($path === '' or $path === '/') { + // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it + return 0; } $totalSize = 0; - $entry = $this->get($path); + if (is_null($entry)) { + $entry = $this->get($path); + } if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { $id = $entry['fileid']; $sql = 'SELECT SUM(`size`) FROM `*PREFIX*filecache` ' . @@ -40,6 +46,7 @@ class HomeCache extends Cache { /** * @param string $path + * @return array */ public function get($path) { $data = parent::get($path); diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index 92a4c01841b..ee6a828f7cc 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -173,14 +173,16 @@ class Scanner extends BasicEmitter { * @param string $path * @param bool $recursive * @param int $reuse - * @return int the size of the scanned folder or -1 if the size is unknown at this stage + * @return array with the meta data of the scanned file or folder */ public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1) { if ($reuse === -1) { $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0; } - $this->scanFile($path, $reuse); - return $this->scanChildren($path, $recursive, $reuse); + $data = $this->scanFile($path, $reuse); + $size = $this->scanChildren($path, $recursive, $reuse); + $data['size'] = $size; + return $data; } /** diff --git a/lib/private/files/cache/updater.php b/lib/private/files/cache/updater.php index 7a45b9e9e96..666d5dd7fe5 100644 --- a/lib/private/files/cache/updater.php +++ b/lib/private/files/cache/updater.php @@ -38,8 +38,8 @@ class Updater { if ($storage) { $cache = $storage->getCache($internalPath); $scanner = $storage->getScanner($internalPath); - $scanner->scan($internalPath, Scanner::SCAN_SHALLOW); - $cache->correctFolderSize($internalPath); + $data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW); + $cache->correctFolderSize($internalPath, $data); self::correctFolder($path, $storage->filemtime($internalPath)); self::correctParentStorageMtime($storage, $internalPath); } -- cgit v1.2.3 From 9548670da76b15ca4e44df63304a11364dde44a0 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 10 Mar 2014 11:16:09 +0100 Subject: we need the file_source to delete a share successfully --- apps/files_sharing/lib/api.php | 13 ++++++------- apps/files_sharing/tests/api.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 0ba58aa896a..dc95a791d29 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -68,7 +68,7 @@ class Api { public static function getShare($params) { $s = self::getShareFromId($params['id']); - $params['itemSource'] = $s['item_source']; + $params['itemSource'] = $s['file_source']; $params['itemType'] = $s['item_type']; $params['specificShare'] = true; @@ -281,9 +281,8 @@ class Api { public static function updateShare($params) { $share = self::getShareFromId($params['id']); - $itemSource = isset($share['item_source']) ? $share['item_source'] : null; - if($itemSource === null) { + if(!isset($share['file_source'])) { return new \OC_OCS_Result(null, 404, "wrong share Id, share doesn't exist."); } @@ -431,10 +430,10 @@ class Api { public static function deleteShare($params) { $share = self::getShareFromId($params['id']); - $itemSource = isset($share['item_source']) ? $share['item_source'] : null; + $fileSource = isset($share['file_source']) ? $share['file_source'] : null; $itemType = isset($share['item_type']) ? $share['item_type'] : null;; - if($itemSource === null) { + if($fileSource === null) { return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist."); } @@ -448,7 +447,7 @@ class Api { try { $return = \OCP\Share::unshare( $itemType, - $itemSource, + $fileSource, $shareType, $shareWith); } catch (\Exception $e) { @@ -504,7 +503,7 @@ class Api { * @return array with: item_source, share_type, share_with, item_type, permissions */ private static function getShareFromId($shareID) { - $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?'; + $sql = 'SELECT `file_source`, `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?'; $args = array($shareID); $query = \OCP\DB::prepare($sql); $result = $query->execute($args); diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 0d3d9b98b2e..073e2dd8c50 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -559,4 +559,38 @@ class Test_Files_Sharing_Api extends Test_Files_Sharing_Base { $this->assertTrue(empty($itemsAfterDelete)); } + + /** + * @brief test unshare of a reshared file + */ + function testDeleteReshare() { + + // user 1 shares a folder with user2 + \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1); + + $fileInfo1 = $this->view->getFileInfo($this->folder); + $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename); + + $result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + $this->assertTrue($result1); + + // user2 shares a file from the folder as link + \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + + $result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1); + + $this->assertTrue(is_string($result2)); + + // test if we can unshare the link again + $items = \OCP\Share::getItemShared('file', null); + $this->assertEquals(1, count($items)); + + $item = reset($items); + $result3 = Share\Api::deleteShare(array('id' => $item['id'])); + + $this->assertTrue($result3->succeeded()); + + } } -- cgit v1.2.3 From a09df0083e6fa5c465c85d58ea04981538b41c32 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 11 Mar 2014 12:59:37 +0100 Subject: add 'received_from' info to the share, so that every share can have a different value --- apps/files_sharing/lib/api.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index dc95a791d29..7adbcab2f9b 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -175,8 +175,10 @@ class Api { if($share) { $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $file['fileid']); if ($receivedFrom) { - $share['received_from'] = $receivedFrom['uid_owner']; - $share['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); + reset($share); + $key = key($share); + $share[$key]['received_from'] = $receivedFrom['uid_owner']; + $share[$key]['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); } $result = array_merge($result, $share); } -- cgit v1.2.3 From 9d32475260b71b97c3fc7f8bf63e1c51e55f210e Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 12 Mar 2014 11:00:30 +0100 Subject: finally fix the paths for the OCS Share API --- apps/files_sharing/lib/api.php | 28 +++++++++-- apps/files_sharing/tests/api.php | 105 ++++++++++++++++++++++++++++++++++++++- lib/public/share.php | 6 ++- 3 files changed, 132 insertions(+), 7 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index 7adbcab2f9b..de3c1cd2630 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -98,8 +98,14 @@ class Api { break; } } + } else { + $path = $params['path']; + foreach ($shares as $key => $share) { + $shares[$key]['path'] = $path; + } } + // include also reshares in the lists. This means that the result // will contain every user with access to the file. if (isset($params['reshares']) && $params['reshares'] === true) { @@ -107,8 +113,10 @@ class Api { } if ($receivedFrom) { - $shares['received_from'] = $receivedFrom['uid_owner']; - $shares['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); + foreach ($shares as $key => $share) { + $shares[$key]['received_from'] = $receivedFrom['uid_owner']; + $shares[$key]['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); + } } } else { $shares = null; @@ -174,9 +182,10 @@ class Api { $share = \OCP\Share::getItemShared($itemType, $file['fileid']); if($share) { $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $file['fileid']); + reset($share); + $key = key($share); + $share[$key]['path'] = self::correctPath($share[$key]['path'], $path); if ($receivedFrom) { - reset($share); - $key = key($share); $share[$key]['received_from'] = $receivedFrom['uid_owner']; $share[$key]['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); } @@ -522,4 +531,15 @@ class Api { } + /** + * @brief make sure that the path has the correct root + * + * @param string $path path returned from the share API + * @param string $folder current root folder + * @return string the correct path + */ + protected static function correctPath($path, $folder) { + return \OC_Filesystem::normalizePath('/' . $folder . '/' . basename($path)); + } + } diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 30deb07c5b9..e2bbb548182 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -477,7 +477,90 @@ class Test_Files_Sharing_Api extends Test_Files_Sharing_Base { } - /** + /** + * @brief test multiple shared folder if the path gets constructed correctly + * @medium + */ + function testGetShareMultipleSharedFolder() { + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + $fileInfo1 = $this->view->getFileInfo($this->folder); + $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder); + + + // share sub-folder to user2 + $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + // share was successful? + $this->assertTrue($result); + + // share folder to user2 + $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31); + + // share was successful? + $this->assertTrue($result); + + + // login as user2 + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + + $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1); + // share was successful? + $this->assertTrue(is_string($result)); + + + // ask for shared/subfolder + $expectedPath1 = '/Shared' . $this->subfolder; + $_GET['path'] = $expectedPath1; + + $result1 = Share\Api::getAllShares(array()); + + $this->assertTrue($result1->succeeded()); + + // test should return one share within $this->folder + $data1 = $result1->getData(); + $share1 = reset($data1); + + // ask for shared/folder/subfolder + $expectedPath2 = '/Shared' . $this->folder . $this->subfolder; + $_GET['path'] = $expectedPath2; + + $result2 = Share\Api::getAllShares(array()); + + $this->assertTrue($result2->succeeded()); + + // test should return one share within $this->folder + $data2 = $result2->getData(); + $share2 = reset($data2); + + + // validate results + // we should get exactly one result each time + $this->assertEquals(1, count($data1)); + $this->assertEquals(1, count($data2)); + + $this->assertEquals($expectedPath1, $share1['path']); + $this->assertEquals($expectedPath2, $share2['path']); + + + // cleanup + $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); + $this->assertTrue($result); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + $this->assertTrue($result); + $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2); + $this->assertTrue($result); + + } + + /** * @brief test re-re-share of folder if the path gets constructed correctly * @medium */ @@ -784,4 +867,24 @@ class Test_Files_Sharing_Api extends Test_Files_Sharing_Base { $this->assertTrue($result3->succeeded()); } + + function testCorrectPath() { + $path = "/foo/bar/test.txt"; + $folder = "/correct/path"; + $expectedResult = "/correct/path/test.txt"; + + $shareApiDummy = new TestShareApi(); + + $this->assertSame($expectedResult, $shareApiDummy->correctPathTest($path, $folder)); + } + +} + +/** + * @brief dumnmy class to test protected methods + */ +class TestShareApi extends \OCA\Files\Share\Api { + public function correctPathTest($path, $folder) { + return self::correctPath($path, $folder); +} } diff --git a/lib/public/share.php b/lib/public/share.php index dd9e1bbf9a9..5066d40354d 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -1250,10 +1250,12 @@ class Share { // Remove root from file source paths if retrieving own shared items if (isset($uidOwner) && isset($row['path'])) { if (isset($row['parent'])) { + // FIXME: Doesn't always construct the correct path, example: + // Folder '/a/b', share '/a' and '/a/b' to user2 + // user2 reshares /Shared/b and ask for share status of /Shared/a/b + // expected result: path=/Shared/a/b; actual result /Shared/b because of the parent $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); $parentResult = $query->execute(array($row['parent'])); - //$query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); - //$parentResult = $query->execute(array($row['id'])); if (\OC_DB::isError($result)) { \OC_Log::write('OCP\Share', 'Can\'t select parent: ' . \OC_DB::getErrorMessage($result) . ', select=' . $select . ' where=' . $where, -- cgit v1.2.3