Implement getPath for shared files

This commit is contained in:
Robin Appelman 2014-03-28 15:26:15 +01:00
parent f331d5f9d4
commit fe78d963d9
2 changed files with 82 additions and 8 deletions

View File

@ -20,6 +20,7 @@
*/ */
namespace OC\Files\Cache; namespace OC\Files\Cache;
use OCP\Share_Backend_Collection; use OCP\Share_Backend_Collection;
/** /**
@ -288,8 +289,7 @@ class Shared_Cache extends Cache {
foreach ($files as $file) { foreach ($files as $file) {
if ($file['mimetype'] === 'httpd/unix-directory') { if ($file['mimetype'] === 'httpd/unix-directory') {
$exploreDirs[] = ltrim($dir . '/' . $file['name'], '/'); $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
} } else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
// usersPath not reliable // usersPath not reliable
//$file['path'] = $file['usersPath']; //$file['path'] = $file['usersPath'];
$file['path'] = ltrim($dir . '/' . $file['name'], '/'); $file['path'] = ltrim($dir . '/' . $file['name'], '/');
@ -344,8 +344,6 @@ class Shared_Cache extends Cache {
if ($row['encrypted'] or ($row['unencrypted_size'] > 0 and $row['mimetype'] === 'httpd/unix-directory')) { if ($row['encrypted'] or ($row['unencrypted_size'] > 0 and $row['mimetype'] === 'httpd/unix-directory')) {
$row['encrypted_size'] = $row['size']; $row['encrypted_size'] = $row['size'];
$row['size'] = $row['unencrypted_size']; $row['size'] = $row['unencrypted_size'];
} else {
$row['size'] = $row['size'];
} }
$files[] = $row; $files[] = $row;
} }
@ -402,4 +400,41 @@ class Shared_Cache extends Cache {
return false; return false;
} }
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, '');
}
}
} }

View File

@ -246,4 +246,43 @@ class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
} }
} }
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->getId(), \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->getId()));
}
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->getId(), \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->getId()));
$this->assertEquals('foo/bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
}
} }