Commit 672495a1 authored by Robin Appelman's avatar Robin Appelman
Browse files

make \OC\Files\Node\Node implement the FileInfo interface

parent c5ee95a4
...@@ -34,19 +34,13 @@ class File extends Node implements \OCP\Files\File { ...@@ -34,19 +34,13 @@ class File extends Node implements \OCP\Files\File {
if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
$this->sendHooks(array('preWrite')); $this->sendHooks(array('preWrite'));
$this->view->file_put_contents($this->path, $data); $this->view->file_put_contents($this->path, $data);
$this->fileInfo = null;
$this->sendHooks(array('postWrite')); $this->sendHooks(array('postWrite'));
} else { } else {
throw new NotPermittedException(); throw new NotPermittedException();
} }
} }
/**
* @return string
*/
public function getMimeType() {
return $this->view->getMimeType($this->path);
}
/** /**
* @param string $mode * @param string $mode
* @return resource * @return resource
...@@ -94,6 +88,7 @@ class File extends Node implements \OCP\Files\File { ...@@ -94,6 +88,7 @@ class File extends Node implements \OCP\Files\File {
$nonExisting = new NonExistingFile($this->root, $this->view, $this->path); $nonExisting = new NonExistingFile($this->root, $this->view, $this->path);
$this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
$this->exists = false; $this->exists = false;
$this->fileInfo = null;
} else { } else {
throw new NotPermittedException(); throw new NotPermittedException();
} }
...@@ -138,6 +133,7 @@ class File extends Node implements \OCP\Files\File { ...@@ -138,6 +133,7 @@ class File extends Node implements \OCP\Files\File {
$this->root->emit('\OC\Files', 'postRename', array($this, $targetNode)); $this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
$this->root->emit('\OC\Files', 'postWrite', array($targetNode)); $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
$this->path = $targetPath; $this->path = $targetPath;
$this->fileInfo = null;
return $targetNode; return $targetNode;
} else { } else {
throw new NotPermittedException(); throw new NotPermittedException();
......
...@@ -321,13 +321,6 @@ class Folder extends Node implements \OCP\Files\Folder { ...@@ -321,13 +321,6 @@ class Folder extends Node implements \OCP\Files\Folder {
return $this->view->free_space($this->path); return $this->view->free_space($this->path);
} }
/**
* @return bool
*/
public function isCreatable() {
return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
}
public function delete() { public function delete() {
if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
$this->sendHooks(array('preDelete')); $this->sendHooks(array('preDelete'));
......
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
namespace OC\Files\Node; namespace OC\Files\Node;
use OCP\Files\NotFoundException; use OCP\Files\FileInfo;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
class Node implements \OCP\Files\Node { class Node implements \OCP\Files\Node, FileInfo {
/** /**
* @var \OC\Files\View $view * @var \OC\Files\View $view
*/ */
...@@ -27,6 +27,11 @@ class Node implements \OCP\Files\Node { ...@@ -27,6 +27,11 @@ class Node implements \OCP\Files\Node {
*/ */
protected $path; protected $path;
/**
* @var \OCP\Files\FileInfo
*/
protected $fileInfo;
/** /**
* @param \OC\Files\View $view * @param \OC\Files\View $view
* @param \OC\Files\Node\Root $root * @param \OC\Files\Node\Root $root
...@@ -38,6 +43,13 @@ class Node implements \OCP\Files\Node { ...@@ -38,6 +43,13 @@ class Node implements \OCP\Files\Node {
$this->path = $path; $this->path = $path;
} }
private function getFileInfo() {
if (!$this->fileInfo) {
$this->fileInfo = $this->view->getFileInfo($this->path);
}
return $this->fileInfo;
}
/** /**
* @param string[] $hooks * @param string[] $hooks
*/ */
...@@ -85,6 +97,12 @@ class Node implements \OCP\Files\Node { ...@@ -85,6 +97,12 @@ class Node implements \OCP\Files\Node {
$this->sendHooks(array('preTouch')); $this->sendHooks(array('preTouch'));
$this->view->touch($this->path, $mtime); $this->view->touch($this->path, $mtime);
$this->sendHooks(array('postTouch')); $this->sendHooks(array('postTouch'));
if ($this->fileInfo) {
if (is_null($mtime)) {
$mtime = time();
}
$this->fileInfo['mtime'] = $mtime;
}
} else { } else {
throw new NotPermittedException(); throw new NotPermittedException();
} }
...@@ -118,8 +136,7 @@ class Node implements \OCP\Files\Node { ...@@ -118,8 +136,7 @@ class Node implements \OCP\Files\Node {
* @return int * @return int
*/ */
public function getId() { public function getId() {
$info = $this->view->getFileInfo($this->path); return $this->getFileInfo()->getId();
return $info['fileid'];
} }
/** /**
...@@ -133,58 +150,60 @@ class Node implements \OCP\Files\Node { ...@@ -133,58 +150,60 @@ class Node implements \OCP\Files\Node {
* @return int * @return int
*/ */
public function getMTime() { public function getMTime() {
return $this->view->filemtime($this->path); return $this->getFileInfo()->getMTime();
} }
/** /**
* @return int * @return int
*/ */
public function getSize() { public function getSize() {
return $this->view->filesize($this->path); return $this->getFileInfo()->getSize();
} }
/** /**
* @return string * @return string
*/ */
public function getEtag() { public function getEtag() {
$info = $this->view->getFileInfo($this->path); return $this->getFileInfo()->getEtag();
return $info['etag'];
} }
/** /**
* @return int * @return int
*/ */
public function getPermissions() { public function getPermissions() {
$info = $this->view->getFileInfo($this->path); return $this->getFileInfo()->getPermissions();
return $info['permissions'];
} }
/** /**
* @return bool * @return bool
*/ */
public function isReadable() { public function isReadable() {
return $this->checkPermissions(\OCP\Constants::PERMISSION_READ); return $this->getFileInfo()->isReadable();
} }
/** /**
* @return bool * @return bool
*/ */
public function isUpdateable() { public function isUpdateable() {
return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE); return $this->getFileInfo()->isUpdateable();
} }
/** /**
* @return bool * @return bool
*/ */
public function isDeletable() { public function isDeletable() {
return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE); return $this->getFileInfo()->isDeletable();
} }
/** /**
* @return bool * @return bool
*/ */
public function isShareable() { public function isShareable() {
return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE); return $this->getFileInfo()->isShareable();
}
public function isCreatable() {
return $this->getFileInfo()->isCreatable();
} }
/** /**
...@@ -240,4 +259,28 @@ class Node implements \OCP\Files\Node { ...@@ -240,4 +259,28 @@ class Node implements \OCP\Files\Node {
} }
return true; return true;
} }
public function isMounted() {
return $this->getFileInfo()->isMounted();
}
public function isShared() {
return $this->getFileInfo()->isShared();
}
public function getMimeType() {
return $this->getFileInfo()->getMimetype();
}
public function getMimePart() {
return $this->getFileInfo()->getMimePart();
}
public function getType() {
return $this->getFileInfo()->getType();
}
public function isEncrypted() {
return $this->getFileInfo()->isEncrypted();
}
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Test\Files\Node; namespace Test\Files\Node;
use OC\Files\FileInfo;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OC\Files\View; use OC\Files\View;
...@@ -20,6 +21,10 @@ class File extends \Test\TestCase { ...@@ -20,6 +21,10 @@ class File extends \Test\TestCase {
$this->user = new \OC\User\User('', new \OC_User_Dummy); $this->user = new \OC\User\User('', new \OC_User_Dummy);
} }
protected function getFileInfo($data) {
return new FileInfo('', null, '', $data);
}
public function testDelete() { public function testDelete() {
$manager = $this->getMock('\OC\Files\Mount\Manager'); $manager = $this->getMock('\OC\Files\Mount\Manager');
...@@ -39,7 +44,7 @@ class File extends \Test\TestCase { ...@@ -39,7 +44,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$view->expects($this->once()) $view->expects($this->once())
->method('unlink') ->method('unlink')
...@@ -89,7 +94,7 @@ class File extends \Test\TestCase { ...@@ -89,7 +94,7 @@ class File extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
$view->expects($this->once()) $view->expects($this->once())
->method('unlink') ->method('unlink')
...@@ -124,7 +129,7 @@ class File extends \Test\TestCase { ...@@ -124,7 +129,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->delete(); $node->delete();
...@@ -156,7 +161,7 @@ class File extends \Test\TestCase { ...@@ -156,7 +161,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$this->assertEquals('bar', $node->getContent()); $this->assertEquals('bar', $node->getContent());
...@@ -180,7 +185,7 @@ class File extends \Test\TestCase { ...@@ -180,7 +185,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => 0))); ->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->getContent(); $node->getContent();
...@@ -201,7 +206,7 @@ class File extends \Test\TestCase { ...@@ -201,7 +206,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$view->expects($this->once()) $view->expects($this->once())
->method('file_put_contents') ->method('file_put_contents')
...@@ -226,7 +231,7 @@ class File extends \Test\TestCase { ...@@ -226,7 +231,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->putContent('bar'); $node->putContent('bar');
...@@ -241,9 +246,9 @@ class File extends \Test\TestCase { ...@@ -241,9 +246,9 @@ class File extends \Test\TestCase {
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$view->expects($this->once()) $view->expects($this->once())
->method('getMimeType') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue('text/plain')); ->will($this->returnValue($this->getFileInfo(array('mimetype' => 'text/plain'))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$this->assertEquals('text/plain', $node->getMimeType()); $this->assertEquals('text/plain', $node->getMimeType());
...@@ -279,7 +284,7 @@ class File extends \Test\TestCase { ...@@ -279,7 +284,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$fh = $node->fopen('r'); $fh = $node->fopen('r');
...@@ -316,7 +321,7 @@ class File extends \Test\TestCase { ...@@ -316,7 +321,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$fh = $node->fopen('w'); $fh = $node->fopen('w');
...@@ -348,7 +353,7 @@ class File extends \Test\TestCase { ...@@ -348,7 +353,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => 0))); ->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->fopen('r'); $node->fopen('r');
...@@ -375,7 +380,7 @@ class File extends \Test\TestCase { ...@@ -375,7 +380,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_UPDATE))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_UPDATE))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->fopen('w'); $node->fopen('w');
...@@ -402,7 +407,7 @@ class File extends \Test\TestCase { ...@@ -402,7 +407,7 @@ class File extends \Test\TestCase {
$view->expects($this->once()) $view->expects($this->once())
->method('getFileInfo') ->method('getFileInfo')
->with('/bar/foo') ->with('/bar/foo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node->fopen('w'); $node->fopen('w');
...@@ -425,7 +430,7 @@ class File extends \Test\TestCase { ...@@ -425,7 +430,7 @@ class File extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $view, '/bar'); $parentNode = new \OC\Files\Node\Folder($root, $view, '/bar');
...@@ -469,7 +474,7 @@ class File extends \Test\TestCase { ...@@ -469,7 +474,7 @@ class File extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $view, '/bar'); $parentNode = new \OC\Files\Node\Folder($root, $view, '/bar');
...@@ -556,7 +561,7 @@ class File extends \Test\TestCase { ...@@ -556,7 +561,7 @@ class File extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo'); $node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $view, '/bar'); $parentNode = new \OC\Files\Node\Folder($root, $view, '/bar');
...@@ -587,7 +592,7 @@ class File extends \Test\TestCase { ...@@ -587,7 +592,7 @@ class File extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_READ))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$view->expects($this->never()) $view->expects($this->never())
->method('rename'); ->method('rename');
......
...@@ -9,6 +9,7 @@ ...@@ -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\FileInfo;
use OC\Files\Mount\Mount; use OC\Files\Mount\Mount;
use OC\Files\Node\Node; use OC\Files\Node\Node;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
...@@ -23,6 +24,10 @@ class Folder extends \Test\TestCase { ...@@ -23,6 +24,10 @@ class Folder extends \Test\TestCase {
$this->user = new \OC\User\User('', new \OC_User_Dummy); $this->user = new \OC\User\User('', new \OC_User_Dummy);
} }
protected function getFileInfo($data) {
return new FileInfo('', null, '', $data);
}
public function testDelete() { public function testDelete() {
$manager = $this->getMock('\OC\Files\Mount\Manager'); $manager = $this->getMock('\OC\Files\Mount\Manager');
/** /**
...@@ -39,7 +44,7 @@ class Folder extends \Test\TestCase { ...@@ -39,7 +44,7 @@ class Folder extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$view->expects($this->once()) $view->expects($this->once())
->method('rmdir') ->method('rmdir')
...@@ -87,7 +92,7 @@ class Folder extends \Test\TestCase { ...@@ -87,7 +92,7 @@ class Folder extends \Test\TestCase {
$view->expects($this->any()) $view->expects($this->any())
->method('getFileInfo') ->method('getFileInfo')
->will($this->returnValue(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))); ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));