Merge pull request #10184 from owncloud/getbyid-node

Fix Folder::getById
This commit is contained in:
Robin Appelman 2014-08-07 13:40:17 +02:00
commit f1091280de
4 changed files with 159 additions and 61 deletions

View File

@ -43,6 +43,7 @@ class Shared_Cache extends Cache {
/** /**
* Get the source cache of a shared file or folder * Get the source cache of a shared file or folder
*
* @param string $target Shared target file path * @param string $target Shared target file path
* @return \OC\Files\Cache\Cache * @return \OC\Files\Cache\Cache
*/ */
@ -275,7 +276,7 @@ class Shared_Cache extends Cache {
*/ */
public function search($pattern) { public function search($pattern) {
$pattern = trim($pattern,'%'); $pattern = trim($pattern, '%');
$normalizedPattern = $this->normalize($pattern); $normalizedPattern = $this->normalize($pattern);
@ -403,8 +404,7 @@ class Shared_Cache extends Cache {
*/ */
public function getPathById($id, $pathEnd = '') { public function getPathById($id, $pathEnd = '') {
// direct shares are easy // direct shares are easy
$path = $this->getShareById($id); if ($id === $this->storage->getSourceId()) {
if (is_string($path)) {
return ltrim($pathEnd, '/'); return ltrim($pathEnd, '/');
} else { } 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 // 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
@ -419,28 +419,14 @@ class Shared_Cache extends Cache {
/** /**
* @param integer $id * @param integer $id
*/ * @return array
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;
}
/**
* @param integer $id
*/ */
private function getParentInfo($id) { private function getParentInfo($id) {
$sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; $sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
$query = \OC_DB::prepare($sql); $query = \OC_DB::prepare($sql);
$result = $query->execute(array($id)); $result = $query->execute(array($id));
if ($row = $result->fetchRow()) { if ($row = $result->fetchRow()) {
return array($row['parent'], $row['name']); return array((int)$row['parent'], $row['name']);
} else { } else {
return array(-1, ''); return array(-1, '');
} }

View File

@ -27,24 +27,21 @@ class Folder extends Node implements \OCP\Files\Folder {
/** /**
* @param string $path * @param string $path
* @throws \OCP\Files\NotFoundException
* @return string * @return string
*/ */
public function getRelativePath($path) { public function getRelativePath($path) {
if ($this->path === '' or $this->path === '/') { if ($this->path === '' or $this->path === '/') {
return $this->normalizePath($path); return $this->normalizePath($path);
} }
if (strpos($path, $this->path) !== 0) { if ($path === $this->path) {
throw new NotFoundException(); return '/';
} else if (strpos($path, $this->path . '/') !== 0) {
return null;
} else { } else {
$path = substr($path, strlen($this->path)); $path = substr($path, strlen($this->path));
if (strlen($path) === 0) {
return '/';
} else {
return $this->normalizePath($path); return $this->normalizePath($path);
} }
} }
}
/** /**
* check if a node is a (grand-)child of the folder * check if a node is a (grand-)child of the folder
@ -295,15 +292,29 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return \OC\Files\Node\Node[] * @return \OC\Files\Node\Node[]
*/ */
public function getById($id) { public function getById($id) {
$nodes = $this->root->getById($id); $mounts = $this->root->getMountsIn($this->path);
$result = array(); $mounts[] = $this->root->getMount($this->path);
foreach ($nodes as $node) { // reverse the array so we start with the storage this view is in
$pathPart = substr($node->getPath(), 0, strlen($this->getPath()) + 1); // which is the most likely to contain the file we're looking for
if ($this->path === '/' or $pathPart === $this->getPath() . '/') { $mounts = array_reverse($mounts);
$result[] = $node;
$nodes = array();
foreach ($mounts as $mount) {
/**
* @var \OC\Files\Mount\Mount $mount
*/
if ($mount->getStorage()) {
$cache = $mount->getStorage()->getCache();
$internalPath = $cache->getPathById($id);
if (is_string($internalPath)) {
$fullPath = $mount->getMountPoint() . $internalPath;
if (!is_null($path = $this->getRelativePath($fullPath))) {
$nodes[] = $this->get($path);
} }
} }
return $result; }
}
return $nodes;
} }
public function getFreeSpace() { public function getFreeSpace() {

View File

@ -162,39 +162,13 @@ class Root extends Folder implements Emitter {
if ($this->view->file_exists($fullPath)) { if ($this->view->file_exists($fullPath)) {
return $this->createNode($fullPath); return $this->createNode($fullPath);
} else { } else {
throw new NotFoundException(); throw new NotFoundException($path);
} }
} else { } else {
throw new NotPermittedException(); throw new NotPermittedException();
} }
} }
/**
* search file by id
*
* An array is returned because in the case where a single storage is mounted in different places the same file
* can exist in different places
*
* @param int $id
* @throws \OCP\Files\NotFoundException
* @return Node[]
*/
public function getById($id) {
$result = Cache::getById($id);
if (is_null($result)) {
throw new NotFoundException();
} else {
list($storageId, $internalPath) = $result;
$nodes = array();
$mounts = $this->mountManager->findByStorageId($storageId);
foreach ($mounts as $mount) {
$nodes[] = $this->get($mount->getMountPoint() . $internalPath);
}
return $nodes;
}
}
//most operations cant be done on the root //most operations cant be done on the root
/** /**

View File

@ -9,6 +9,7 @@
namespace Test\Files\Node; namespace Test\Files\Node;
use OC\Files\Cache\Cache; use OC\Files\Cache\Cache;
use OC\Files\Mount\Mount;
use OC\Files\Node\Node; use OC\Files\Node\Node;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
@ -468,4 +469,130 @@ class Folder extends \PHPUnit_Framework_TestCase {
$file = new Node(null, null, '/foobar'); $file = new Node(null, null, '/foobar');
$this->assertFalse($folder->isSubNode($file)); $this->assertFalse($folder->isSubNode($file));
} }
public function testGetById() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->getMock('\OC\Files\Storage\Storage');
$mount = new Mount($storage, '/bar');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$view->expects($this->once())
->method('file_exists')
->will($this->returnValue(true));
$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
$cache->expects($this->once())
->method('getPathById')
->with('1')
->will($this->returnValue('foo/qwerty'));
$root->expects($this->once())
->method('getMountsIn')
->with('/bar/foo')
->will($this->returnValue(array()));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->getById(1);
$this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
}
public function testGetByIdOutsideFolder() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->getMock('\OC\Files\Storage\Storage');
$mount = new Mount($storage, '/bar');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
$cache->expects($this->once())
->method('getPathById')
->with('1')
->will($this->returnValue('foobar'));
$root->expects($this->once())
->method('getMountsIn')
->with('/bar/foo')
->will($this->returnValue(array()));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->getById(1);
$this->assertCount(0, $result);
}
public function testGetByIdMultipleStorages() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->getMock('\OC\Files\Storage\Storage');
$mount1 = new Mount($storage, '/bar');
$mount2 = new Mount($storage, '/bar/foo/asd');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$view->expects($this->any())
->method('file_exists')
->will($this->returnValue(true));
$storage->expects($this->any())
->method('getCache')
->will($this->returnValue($cache));
$cache->expects($this->any())
->method('getPathById')
->with('1')
->will($this->returnValue('foo/qwerty'));
$root->expects($this->any())
->method('getMountsIn')
->with('/bar/foo')
->will($this->returnValue(array($mount2)));
$root->expects($this->once())
->method('getMount')
->with('/bar/foo')
->will($this->returnValue($mount1));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->getById(1);
$this->assertEquals(2, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
$this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
}
} }