improved file size

remotes/origin/stable6
Florin Peter 2013-04-27 23:34:25 +07:00
parent 7f1f0464a8
commit f9760f6521
1 changed files with 42 additions and 17 deletions

@ -440,53 +440,78 @@ class Util {
}
/**
* @brief fix the file size of the encrypted file
* @brief get the file size of the unencrypted file
*
* @param $path absolute path
* @return true / false if file is encrypted
*/
public function fixFileSize($path) {
$result = false;
public function getFileSize($path) {
$result = 0;
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
if ($this->view->file_exists($path) && $this->isEncryptedPath($path)) {
// Reformat path for use with OC_FSV
$pathSplit = explode( '/', $path );
$pathRelative = implode( '/', array_slice( $pathSplit, 3 ) );
// Reformat path for use with OC_FSV
$pathSplit = explode( '/', $path );
$pathRelative = implode( '/', array_slice( $pathSplit, 3 ) );
$cached = $this->view->getFileInfo($path);
$cached['encrypted'] = 1;
if ($pathSplit[2] == 'files' && $this->view->file_exists($path) && $this->isEncryptedPath($path)) {
// get the size from filesystem
$size = $this->view->filesize($path);
$fullPath = $this->view->getLocalFile($path);
$size = filesize($fullPath);
// calculate last chunk nr
$lastChunckNr = floor($size / 8192);
// open stream
$result = fopen('crypt://' . $pathRelative, "r");
$stream = fopen('crypt://' . $pathRelative, "r");
if(is_resource($result)) {
if(is_resource($stream)) {
// calculate last chunk position
$lastChunckPos = ($lastChunckNr * 8192);
// seek to end
fseek($result, $lastChunckPos);
fseek($stream, $lastChunckPos);
// get the content of the last chunk
$lastChunkContent = fread($result, 8192);
$lastChunkContent = fread($stream, 8192);
// calc the real file size with the size of the last chunk
$realSize = (($lastChunckNr * 6126) + strlen($lastChunkContent));
// set the size
$cached['unencrypted_size'] = $realSize;
// store file size
$result = $realSize;
}
}
\OC_FileProxy::$enabled = $proxyStatus;
return $result;
}
/**
* @brief fix the file size of the encrypted file
*
* @param $path absolute path
* @return true / false if file is encrypted
*/
public function fixFileSize($path) {
$result = false;
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$realSize = $this->getFileSize($path);
if($realSize > 0) {
$cached = $this->view->getFileInfo($path);
$cached['encrypted'] = 1;
// set the size
$cached['unencrypted_size'] = $realSize;
// put file info
$this->view->putFileInfo( $path, $cached );