move the cache api from OC_Files to filesystem(view)

This commit is contained in:
Robin Appelman 2012-10-26 12:30:25 +02:00
parent 0e2bf8d373
commit 39adadd3e3
5 changed files with 138 additions and 101 deletions

View File

@ -28,98 +28,6 @@
class OC_Files {
static $tmpFiles = array();
/**
* get the filesystem info
*
* @param string $path
* @return array
*
* returns an associative array with the following keys:
* - size
* - mtime
* - mimetype
* - encrypted
* - versioned
*/
public static function getFileInfo($path) {
$path = \OC\Files\Filesystem::normalizePath($path);
/**
* @var \OC\Files\Storage\Storage $storage
* @var string $path
*/
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($path);
$cache = $storage->getCache();
if (!$cache->inCache($internalPath)) {
$scanner = $storage->getScanner();
$scanner->scan($internalPath, \OC\Files\Cache\Scanner::SCAN_SHALLOW);
}
$data = $cache->get($internalPath);
if ($data['mimetype'] === 'httpd/unix-directory') {
//add the sizes of other mountpoints to the folder
$mountPoints = \OC\Files\Filesystem::getMountPoints($path);
foreach ($mountPoints as $mountPoint) {
$subStorage = \OC\Files\Filesystem::getStorage($mountPoint);
$subCache = $subStorage->getCache();
$rootEntry = $subCache->get('');
$data['size'] += $rootEntry['size'];
}
}
return $data;
}
/**
* get the content of a directory
*
* @param string $directory path under datadirectory
* @return array
*/
public static function getDirectoryContent($directory, $mimetype_filter = '') {
$path = \OC\Files\Filesystem::normalizePath($directory);
/**
* @var \OC\Files\Storage\Storage $storage
* @var string $path
*/
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($path);
$cache = $storage->getCache();
if (!$cache->inCache($internalPath)) {
$scanner = $storage->getScanner();
$scanner->scan($internalPath, \OC\Files\Cache\Scanner::SCAN_SHALLOW);
}
$files = $cache->getFolderContents($internalPath); //TODO: mimetype_filter
//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
$mountPoints = \OC\Files\Filesystem::getMountPoints($directory);
$dirLength = strlen($path);
foreach ($mountPoints as $mountPoint) {
$subStorage = \OC\Files\Filesystem::getStorage($mountPoint);
$subCache = $subStorage->getCache();
$rootEntry = $subCache->get('');
$relativePath = trim(substr($mountPoint, $dirLength), '/');
if ($pos = strpos($relativePath, '/')) { //mountpoint inside subfolder add size to the correct folder
$entryName = substr($relativePath, 0, $pos);
foreach ($files as &$entry) {
if ($entry['name'] === $entryName) {
$entry['size'] += $rootEntry['size'];
}
}
} else { //mountpoint in this folder, add an entry for it
$rootEntry['name'] = $relativePath;
$files[] = $rootEntry;
}
}
usort($files, "fileCmp"); //TODO: remove this once ajax is merged
return $files;
}
public static function searchByMime($mimetype_filter) {
$files = array();
$dirs_to_check = array('');

View File

@ -8,6 +8,11 @@
namespace OC\Files\Cache;
/**
* Metadata cache for the filesystem
*
* don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
*/
class Cache {
const NOT_FOUND = 0;
const PARTIAL = 1; //only partial data available, file not cached in the database

View File

@ -609,6 +609,33 @@ class Filesystem {
}
return $path;
}
/**
* get the filesystem info
*
* @param string $path
* @return array
*
* returns an associative array with the following keys:
* - size
* - mtime
* - mimetype
* - encrypted
* - versioned
*/
public static function getFileInfo($path) {
return self::$defaultInstance->getFileInfo($path);
}
/**
* get the content of a directory
*
* @param string $directory path under datadirectory
* @return array
*/
public static function getDirectoryContent($directory, $mimetype_filter = '') {
return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter);
}
}
\OC_Hook::connect('OC_Filesystem', 'post_write', 'OC_Filesystem', 'removeETagHook');

View File

@ -645,4 +645,96 @@ class View {
public function hasUpdated($path, $time) {
return $this->basicOperation('hasUpdated', $path, array(), $time);
}
/**
* get the filesystem info
*
* @param string $path
* @return array
*
* returns an associative array with the following keys:
* - size
* - mtime
* - mimetype
* - encrypted
* - versioned
*/
public function getFileInfo($path) {
$path = \OC\Files\Filesystem::normalizePath($this->fakeRoot . '/' . $path);
/**
* @var \OC\Files\Storage\Storage $storage
* @var string $path
*/
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($path);
$cache = $storage->getCache();
if (!$cache->inCache($internalPath)) {
$scanner = $storage->getScanner();
$scanner->scan($internalPath, \OC\Files\Cache\Scanner::SCAN_SHALLOW);
}
$data = $cache->get($internalPath);
if ($data['mimetype'] === 'httpd/unix-directory') {
//add the sizes of other mountpoints to the folder
$mountPoints = \OC\Files\Filesystem::getMountPoints($path);
foreach ($mountPoints as $mountPoint) {
$subStorage = \OC\Files\Filesystem::getStorage($mountPoint);
$subCache = $subStorage->getCache();
$rootEntry = $subCache->get('');
$data['size'] += $rootEntry['size'];
}
}
return $data;
}
/**
* get the content of a directory
*
* @param string $directory path under datadirectory
* @return array
*/
public function getDirectoryContent($directory, $mimetype_filter = '') {
$path = \OC\Files\Filesystem::normalizePath($this->fakeRoot . '/' . $directory);
/**
* @var \OC\Files\Storage\Storage $storage
* @var string $path
*/
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($path);
$cache = $storage->getCache();
if (!$cache->inCache($internalPath)) {
$scanner = $storage->getScanner();
$scanner->scan($internalPath, \OC\Files\Cache\Scanner::SCAN_SHALLOW);
}
$files = $cache->getFolderContents($internalPath); //TODO: mimetype_filter
//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
$mountPoints = \OC\Files\Filesystem::getMountPoints($directory);
$dirLength = strlen($path);
foreach ($mountPoints as $mountPoint) {
$subStorage = \OC\Files\Filesystem::getStorage($mountPoint);
$subCache = $subStorage->getCache();
$rootEntry = $subCache->get('');
$relativePath = trim(substr($mountPoint, $dirLength), '/');
if ($pos = strpos($relativePath, '/')) { //mountpoint inside subfolder add size to the correct folder
$entryName = substr($relativePath, 0, $pos);
foreach ($files as &$entry) {
if ($entry['name'] === $entryName) {
$entry['size'] += $rootEntry['size'];
}
}
} else { //mountpoint in this folder, add an entry for it
$rootEntry['name'] = $relativePath;
$files[] = $rootEntry;
}
}
usort($files, "fileCmp"); //TODO: remove this once ajax is merged
return $files;
}
}

View File

@ -5,9 +5,11 @@
* later.
* See the COPYING-README file. */
namespace Test\Files;
use \OC\Files\Filesystem as Filesystem;
class Test_Files extends PHPUnit_Framework_TestCase {
class View extends \PHPUnit_Framework_TestCase {
/**
* @var \OC\Files\Storage\Storage[] $storages;
*/
@ -35,19 +37,21 @@ class Test_Files extends PHPUnit_Framework_TestCase {
$imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
$storageSize = $textSize * 2 + $imageSize;
$cachedData = OC_Files::getFileInfo('/foo.txt');
$rootView = new \OC\Files\View('');
$cachedData = $rootView->getFileInfo('/foo.txt');
$this->assertEquals($textSize, $cachedData['size']);
$this->assertEquals('text/plain', $cachedData['mimetype']);
$cachedData = OC_Files::getFileInfo('/');
$cachedData = $rootView->getFileInfo('/');
$this->assertEquals($storageSize * 3, $cachedData['size']);
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
$cachedData = OC_Files::getFileInfo('/folder');
$cachedData = $rootView->getFileInfo('/folder');
$this->assertEquals($storageSize + $textSize, $cachedData['size']);
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
$folderData = OC_Files::getDirectoryContent('/');
$folderData = $rootView->getDirectoryContent('/');
/**
* expected entries:
* folder
@ -74,20 +78,21 @@ class Test_Files extends PHPUnit_Framework_TestCase {
Filesystem::mount($storage2, array(), '/substorage');
$textSize = strlen("dummy file data\n");
$imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
$storageSize = $textSize * 2 + $imageSize;
$cachedData = \OC_Files::getFileInfo('/');
$rootView = new \OC\Files\View('');
$cachedData = $rootView->getFileInfo('/');
$this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
$this->assertEquals(-1, $cachedData['size']);
$folderData = \OC_Files::getDirectoryContent('/substorage/folder');
$folderData = $rootView->getDirectoryContent('/substorage/folder');
$this->assertEquals('text/plain', $folderData[0]['mimetype']);
$this->assertEquals($textSize, $folderData[0]['size']);
}
/**
* @param bool $scan
* @return OC\Files\Storage\Storage
* @return \OC\Files\Storage\Storage
*/
private function getTestStorage($scan = true) {
$storage = new \OC\Files\Storage\Temporary(array());