improved file size handling

This commit is contained in:
Florin Peter 2013-04-23 19:08:52 +02:00
parent 8ab9433fdf
commit baa6fd639f
1 changed files with 60 additions and 32 deletions

View File

@ -166,6 +166,7 @@ class Proxy extends \OC_FileProxy {
} }
} }
return true;
} }
/** /**
@ -300,7 +301,6 @@ class Proxy extends \OC_FileProxy {
$oldRelPath = implode('/', $oldSliced); $oldRelPath = implode('/', $oldSliced);
$oldKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'keyfiles' . '/' . $oldRelPath; $oldKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'keyfiles' . '/' . $oldRelPath;
$newTrimmed = ltrim($newPath, '/'); $newTrimmed = ltrim($newPath, '/');
$newSplit = explode('/', $newTrimmed); $newSplit = explode('/', $newTrimmed);
$newSliced = array_slice($newSplit, 2); $newSliced = array_slice($newSplit, 2);
@ -418,6 +418,25 @@ class Proxy extends \OC_FileProxy {
} }
public function postGetFileInfo( $path, $data ) {
// if path is a folder do nothing
if(is_array($data) && array_key_exists('size', $data)) {
// Disable encryption proxy to prevent recursive calls
\OC_FileProxy::$enabled = false;
// get file size
$data['size'] = self::postFileSize($path, $data['size']);
// Re-enable the proxy
\OC_FileProxy::$enabled = true;
trigger_error('postGetFileInfo '.$path.' size: '.$data['size']);
}
return $data;
}
public function postStat( $path, $data ) { public function postStat( $path, $data ) {
if ( Crypt::isCatfileContent( $path ) ) { if ( Crypt::isCatfileContent( $path ) ) {
@ -433,29 +452,38 @@ class Proxy extends \OC_FileProxy {
public function postFileSize( $path, $size ) { public function postFileSize( $path, $size ) {
$view = new \OC_FilesystemView( '/' );
// if path is a folder do nothing
if($view->is_dir($path)) {
return $size;
}
// Reformat path for use with OC_FSV // Reformat path for use with OC_FSV
$path_split = explode('/', $path); $path_split = explode('/', $path);
$path_f = implode('/', array_slice($path_split, 3)); $path_f = implode('/', array_slice($path_split, 3));
$view = new \OC_FilesystemView( '/' );
$userId = \OCP\User::getUser(); $userId = \OCP\User::getUser();
$util = new Util( $view, $userId ); $util = new Util( $view, $userId );
if ($util->isEncryptedPath($path)) {
// FIXME: is there a better solution to check if file belongs to files path?
// only get file size if file is in 'files' path
if (count($path_split) >= 2 && $path_split[2] == 'files' && $util->isEncryptedPath($path)) {
// Disable encryption proxy to prevent recursive calls // Disable encryption proxy to prevent recursive calls
\OC_FileProxy::$enabled = false; \OC_FileProxy::$enabled = false;
// get file info
$cached = \OC\Files\Filesystem::getFileInfo($path_f, '');
// calculate last chunk nr
$lastChunckNr = floor($size / 8192);
// open stream // open stream
$result = fopen('crypt://' . $path_f, "r"); $result = fopen('crypt://' . $path_f, "r");
if(is_resource($result)) { if(is_resource($result)) {
// don't trust the given size, allways get the size from filesystem
$size = $view->filesize($path);
// calculate last chunk nr
$lastChunckNr = floor($size / 8192);
// calculate last chunk position // calculate last chunk position
$lastChunckPos = ($lastChunckNr * 8192); $lastChunckPos = ($lastChunckNr * 8192);
@ -469,13 +497,13 @@ class Proxy extends \OC_FileProxy {
$realSize = (($lastChunckNr * 6126) + strlen($lastChunkContent)); $realSize = (($lastChunckNr * 6126) + strlen($lastChunkContent));
// set the size // set the size
$cached['size'] = $realSize; $size = $realSize;
} }
// enable proxy // enable proxy
\OC_FileProxy::$enabled = true; \OC_FileProxy::$enabled = true;
return $cached['size']; return $size;
} else { } else {