summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoricewind1991 <robin@icewind.nl>2014-02-14 15:16:39 +0100
committericewind1991 <robin@icewind.nl>2014-02-14 15:16:39 +0100
commit2a7509ee50b3d16392c54c44cad003e8c867e349 (patch)
tree200546c66418125ab387ed8fcab70dcc53bce23c
parentd7f454771769d37e06136332f15695a98275f0f5 (diff)
parent4e2b52a376d2aab5e0f9d0034a8e2bfa196c08bd (diff)
downloadnextcloud-server-2a7509ee50b3d16392c54c44cad003e8c867e349.tar.gz
nextcloud-server-2a7509ee50b3d16392c54c44cad003e8c867e349.zip
Merge pull request #6748 from owncloud/fileinfo
Add a FileInfo class which holds all info of a file ...
-rw-r--r--apps/files_encryption/lib/proxy.php6
-rw-r--r--apps/files_encryption/lib/stream.php2
-rwxr-xr-xapps/files_encryption/tests/share.php14
-rwxr-xr-xapps/files_encryption/tests/util.php8
-rw-r--r--lib/private/files/fileinfo.php189
-rw-r--r--lib/private/files/view.php42
-rw-r--r--lib/public/files/fileinfo.php138
-rw-r--r--lib/public/util.php2
8 files changed, 367 insertions, 34 deletions
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index 11048005969..2ea477a6e4d 100644
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -283,7 +283,7 @@ class Proxy extends \OC_FileProxy {
public function postGetFileInfo($path, $data) {
// if path is a folder do nothing
- if (\OCP\App::isEnabled('files_encryption') && is_array($data) && array_key_exists('size', $data)) {
+ if (\OCP\App::isEnabled('files_encryption') && $data !== false && array_key_exists('size', $data)) {
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
@@ -341,7 +341,7 @@ class Proxy extends \OC_FileProxy {
}
// if file is encrypted return real file size
- if (is_array($fileInfo) && $fileInfo['encrypted'] === true) {
+ if ($fileInfo && $fileInfo['encrypted'] === true) {
// try to fix unencrypted file size if it doesn't look plausible
if ((int)$fileInfo['size'] > 0 && (int)$fileInfo['unencrypted_size'] === 0 ) {
$fixSize = $util->getFileSize($path);
@@ -354,7 +354,7 @@ class Proxy extends \OC_FileProxy {
$size = $fileInfo['unencrypted_size'];
} else {
// self healing if file was removed from file cache
- if (!is_array($fileInfo)) {
+ if (!$fileInfo) {
$fileInfo = array();
}
diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php
index b3bf34ddb82..88eacc6f136 100644
--- a/apps/files_encryption/lib/stream.php
+++ b/apps/files_encryption/lib/stream.php
@@ -567,7 +567,7 @@ class Stream {
// get file info
$fileInfo = $this->rootView->getFileInfo($path);
- if (is_array($fileInfo)) {
+ if ($fileInfo) {
// set encryption data
$fileInfo['encrypted'] = true;
$fileInfo['size'] = $this->size;
diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php
index acf408a07f0..5c6569f20e7 100755
--- a/apps/files_encryption/tests/share.php
+++ b/apps/files_encryption/tests/share.php
@@ -150,7 +150,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
'/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// check if the unencrypted file size is stored
$this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
@@ -310,7 +310,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
'/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// re-enable the file proxy
\OC_FileProxy::$enabled = $proxyStatus;
@@ -388,7 +388,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
. $this->subfolder);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfoSubFolder));
+ $this->assertTrue($fileInfoSubFolder instanceof \OC\Files\FileInfo);
// re-enable the file proxy
\OC_FileProxy::$enabled = $proxyStatus;
@@ -422,7 +422,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
. $this->subsubfolder . '/' . $this->filename);
// check if we have fileInfos
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// share the file with user3
\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4, OCP\PERMISSION_ALL);
@@ -517,7 +517,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
'/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// check if the unencrypted file size is stored
$this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
@@ -595,7 +595,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
'/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// check if the unencrypted file size is stored
$this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
@@ -888,7 +888,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
'/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
// check if we have a valid file info
- $this->assertTrue(is_array($fileInfo));
+ $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
// check if the unencrypted file size is stored
$this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php
index 97e3c518dac..228f7df1b90 100755
--- a/apps/files_encryption/tests/util.php
+++ b/apps/files_encryption/tests/util.php
@@ -328,7 +328,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
- $this->assertTrue(is_array($fileInfoUnencrypted));
+ $this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
// enable file encryption again
\OC_App::enable('files_encryption');
@@ -338,7 +338,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
- $this->assertTrue(is_array($fileInfoEncrypted));
+ $this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
// check if mtime and etags unchanged
$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
@@ -357,14 +357,14 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
- $this->assertTrue(is_array($fileInfoEncrypted));
+ $this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
// encrypt all unencrypted files
$util->decryptAll('/' . $this->userId . '/' . 'files');
$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
- $this->assertTrue(is_array($fileInfoUnencrypted));
+ $this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
// check if mtime and etags unchanged
$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
diff --git a/lib/private/files/fileinfo.php b/lib/private/files/fileinfo.php
new file mode 100644
index 00000000000..7edea13df96
--- /dev/null
+++ b/lib/private/files/fileinfo.php
@@ -0,0 +1,189 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Files;
+
+class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess, \JsonSerializable {
+ /**
+ * @var array $data
+ */
+ private $data;
+
+ /**
+ * @var string $path
+ */
+ private $path;
+
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ */
+ private $storage;
+
+ /**
+ * @var string $internalPath
+ */
+ private $internalPath;
+
+ public function __construct($path, $storage, $internalPath, $data) {
+ $this->path = $path;
+ $this->storage = $storage;
+ $this->internalPath = $internalPath;
+ $this->data = $data;
+ }
+
+ public function offsetSet($offset, $value) {
+ $this->data[$offset] = $value;
+ }
+
+ public function offsetExists($offset) {
+ return isset($this->data[$offset]);
+ }
+
+ public function offsetUnset($offset) {
+ unset($this->data[$offset]);
+ }
+
+ public function offsetGet($offset) {
+ return $this->data[$offset];
+ }
+
+ public function jsonSerialize() {
+ return $this->data;
+ }
+
+ /**
+ * @return string
+ */
+ public function getPath() {
+ return $this->path;
+ }
+
+ /**
+ * @return \OCP\Files\Storage
+ */
+ public function getStorage() {
+ return $this->storage;
+ }
+
+ /**
+ * @return string
+ */
+ public function getInternalPath() {
+ return $this->internalPath;
+ }
+
+ /**
+ * @return int
+ */
+ public function getId() {
+ return $this->data['fileid'];
+ }
+
+ /**
+ * @return string
+ */
+ public function getMimetype() {
+ return $this->data['mimetype'];
+ }
+
+ /**
+ * @return string
+ */
+ public function getMimePart() {
+ return $this->data['mimepart'];
+ }
+
+ /**
+ * @return string
+ */
+ public function getName() {
+ return $this->data['name'];
+ }
+
+ /**
+ * @return string
+ */
+ public function getEtag() {
+ return $this->data['etag'];
+ }
+
+ /**
+ * @return int
+ */
+ public function getSize() {
+ return $this->data['size'];
+ }
+
+ /**
+ * @return int
+ */
+ public function getMTime() {
+ return $this->data['mtime'];
+ }
+
+ /**
+ * @return bool
+ */
+ public function isEncrypted() {
+ return $this->data['encrypted'];
+ }
+
+ /**
+ * @return int
+ */
+ public function getPermissions() {
+ return $this->data['permissions'];
+ }
+
+ /**
+ * @return \OCP\Files\FileInfo::TYPE_FILE | \OCP\Files\FileInfo::TYPE_FOLDER
+ */
+ public function getType() {
+ return $this->data['type'];
+ }
+
+ public function getData(){
+ return $this->data;
+ }
+
+ /**
+ * @param int $permissions
+ * @return bool
+ */
+ protected function checkPermissions($permissions) {
+ return ($this->getPermissions() & $permissions) === $permissions;
+ }
+
+ /**
+ * @return bool
+ */
+ public function isReadable() {
+ return $this->checkPermissions(\OCP\PERMISSION_READ);
+ }
+
+ /**
+ * @return bool
+ */
+ public function isUpdateable() {
+ return $this->checkPermissions(\OCP\PERMISSION_UPDATE);
+ }
+
+ /**
+ * @return bool
+ */
+ public function isDeletable() {
+ return $this->checkPermissions(\OCP\PERMISSION_DELETE);
+ }
+
+ /**
+ * @return bool
+ */
+ public function isShareable() {
+ return $this->checkPermissions(\OCP\PERMISSION_SHARE);
+ }
+}
diff --git a/lib/private/files/view.php b/lib/private/files/view.php
index d97544b865e..6fc534757b2 100644
--- a/lib/private/files/view.php
+++ b/lib/private/files/view.php
@@ -781,14 +781,7 @@ class View {
* @param string $path
* @param boolean $includeMountPoints whether to add mountpoint sizes,
* defaults to true
- * @return array
- *
- * returns an associative array with the following keys:
- * - size
- * - mtime
- * - mimetype
- * - encrypted
- * - versioned
+ * @return \OC\Files\FileInfo | false
*/
public function getFileInfo($path, $includeMountPoints = true) {
$data = array();
@@ -841,10 +834,13 @@ class View {
$data['permissions'] = $permissions;
}
}
+ if (!$data) {
+ return false;
+ }
$data = \OC_FileProxy::runPostProxies('getFileInfo', $path, $data);
- return $data;
+ return new FileInfo($path, $storage, $internalPath, $data);
}
/**
@@ -852,7 +848,7 @@ class View {
*
* @param string $directory path under datadirectory
* @param string $mimetype_filter limit returned content to this mimetype or mimepart
- * @return array
+ * @return FileInfo[]
*/
public function getDirectoryContent($directory, $mimetype_filter = '') {
$result = array();
@@ -878,7 +874,11 @@ class View {
$watcher->checkUpdate($internalPath);
}
- $files = $cache->getFolderContents($internalPath); //TODO: mimetype_filter
+ $files = array();
+ $contents = $cache->getFolderContents($internalPath); //TODO: mimetype_filter
+ foreach ($contents as $content) {
+ $files[] = new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content);
+ }
$permissions = $permissionsCache->getDirectoryPermissions($cache->getId($internalPath), $user);
$ids = array();
@@ -936,7 +936,7 @@ class View {
break;
}
}
- $files[] = $rootEntry;
+ $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry);
}
}
}
@@ -958,6 +958,7 @@ class View {
$result = $files;
}
}
+
return $result;
}
@@ -965,12 +966,15 @@ class View {
* change file metadata
*
* @param string $path
- * @param array $data
+ * @param array | \OCP\Files\FileInfo $data
* @return int
*
* returns the fileid of the updated file
*/
public function putFileInfo($path, $data) {
+ if ($data instanceof FileInfo) {
+ $data = $data->getData();
+ }
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
/**
* @var \OC\Files\Storage\Storage $storage
@@ -995,7 +999,7 @@ class View {
* search for files with the name matching $query
*
* @param string $query
- * @return array
+ * @return FileInfo[]
*/
public function search($query) {
return $this->searchCommon('%' . $query . '%', 'search');
@@ -1005,7 +1009,7 @@ class View {
* search for files by mimetype
*
* @param string $mimetype
- * @return array
+ * @return FileInfo[]
*/
public function searchByMime($mimetype) {
return $this->searchCommon($mimetype, 'searchByMime');
@@ -1014,7 +1018,7 @@ class View {
/**
* @param string $query
* @param string $method
- * @return array
+ * @return FileInfo[]
*/
private function searchCommon($query, $method) {
$files = array();
@@ -1028,8 +1032,9 @@ class View {
$results = $cache->$method($query);
foreach ($results as $result) {
if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
+ $internalPath = $result['path'];
$result['path'] = substr($mountPoint . $result['path'], $rootLength);
- $files[] = $result;
+ $files[] = new FileInfo($mountPoint . $result['path'], $storage, $internalPath, $result);
}
}
@@ -1043,8 +1048,9 @@ class View {
$results = $cache->$method($query);
if ($results) {
foreach ($results as $result) {
+ $internalPath = $result['path'];
$result['path'] = $relativeMountPoint . $result['path'];
- $files[] = $result;
+ $files[] = new FileInfo($mountPoint . $result['path'], $storage, $internalPath, $result);
}
}
}
diff --git a/lib/public/files/fileinfo.php b/lib/public/files/fileinfo.php
new file mode 100644
index 00000000000..68ce45d3fa1
--- /dev/null
+++ b/lib/public/files/fileinfo.php
@@ -0,0 +1,138 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+namespace OCP\Files;
+
+interface FileInfo {
+ const TYPE_FILE = 'file';
+ const TYPE_FOLDER = 'folder';
+
+ /**
+ * Get the Etag of the file or folder
+ *
+ * @return string
+ */
+ public function getEtag();
+
+ /**
+ * Get the size in bytes for the file or folder
+ *
+ * @return int
+ */
+ public function getSize();
+
+ /**
+ * Get the last modified date as timestamp for the file or folder
+ *
+ * @return int
+ */
+ public function getMtime();
+
+ /**
+ * Get the name of the file or folder
+ *
+ * @return string
+ */
+ public function getName();
+
+ /**
+ * Get the path relative to the storage
+ *
+ * @return string
+ */
+ public function getInternalPath();
+
+ /**
+ * Get the absolute path
+ *
+ * @return string
+ */
+ public function getPath();
+
+ /**
+ * Get the full mimetype of the file or folder i.e. 'image/png'
+ *
+ * @return string
+ */
+ public function getMimetype();
+
+ /**
+ * Get the first part of the mimetype of the file or folder i.e. 'image'
+ *
+ * @return string
+ */
+ public function getMimePart();
+
+ /**
+ * Get the storage the file or folder is storage on
+ *
+ * @return \OCP\Files\Storage
+ */
+ public function getStorage();
+
+ /**
+ * Get the file id of the file or folder
+ *
+ * @return int
+ */
+ public function getId();
+
+ /**
+ * Check whether the file is encrypted
+ *
+ * @return bool
+ */
+ public function isEncrypted();
+
+ /**
+ * Get the permissions of the file or folder as bitmasked combination of the following constants
+ * \OCP\PERMISSION_CREATE
+ * \OCP\PERMISSION_READ
+ * \OCP\PERMISSION_UPDATE
+ * \OCP\PERMISSION_DELETE
+ * \OCP\PERMISSION_SHARE
+ * \OCP\PERMISSION_ALL
+ *
+ * @return int
+ */
+ public function getPermissions();
+
+ /**
+ * Check whether this is a file or a folder
+ *
+ * @return \OCP\Files\FileInfo::TYPE_FILE | \OCP\Files\FileInfo::TYPE_FOLDER
+ */
+ public function getType();
+
+ /**
+ * Check if the file or folder is readable
+ *
+ * @return bool
+ */
+ public function isReadable();
+
+ /**
+ * Check if a file is writable
+ *
+ * @return bool
+ */
+ public function isUpdateable();
+
+ /**
+ * Check if a file or folder can be deleted
+ *
+ * @return bool
+ */
+ public function isDeletable();
+
+ /**
+ * Check if a file or folder can be shared
+ *
+ * @return bool
+ */
+ public function isShareable();
+}
diff --git a/lib/public/util.php b/lib/public/util.php
index 4611e5e2650..570283e2a8a 100644
--- a/lib/public/util.php
+++ b/lib/public/util.php
@@ -121,7 +121,7 @@ class Util {
/**
* get l10n object
* @param string $application
- * @return OC_L10N
+ * @return \OC_L10N
*/
public static function getL10N( $application ) {
return \OC_L10N::get( $application );