diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2013-10-10 16:43:40 +0400 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2013-10-10 16:43:40 +0400 |
commit | e56947255e0760c6c560fa15f67468c966919685 (patch) | |
tree | 726a93275b8a6f556bf482bf00b24ab71610dee1 /apps/files_versions | |
parent | 666e52a46b971d49eb43024afe87cabf42b6f49b (diff) |
don't use glob() and getLocalFile(), this can lead to problems on windows servers
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/lib/versions.php | 138 |
1 files changed, 57 insertions, 81 deletions
diff --git a/apps/files_versions/lib/versions.php b/apps/files_versions/lib/versions.php index fc8d0365c71..4ad272e43cc 100644 --- a/apps/files_versions/lib/versions.php +++ b/apps/files_versions/lib/versions.php @@ -238,60 +238,35 @@ class Storage { * @param $filename file to find versions of, relative to the user files dir * @returns array */ - public static function getVersions($uid, $filename ) { - if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) { - $versions_fileview = new \OC\Files\View('/' . $uid . '/files_versions'); - $versionsName = $versions_fileview->getLocalFile($filename).'.v'; - $escapedVersionName = preg_replace('/(\*|\?|\[)/', '[$1]', $versionsName); - - $versions = array(); - // fetch for old versions - $matches = glob($escapedVersionName.'*'); - - if ( !$matches ) { - return $versions; - } - - sort( $matches ); - - $files_view = new \OC\Files\View('/'.$uid.'/files'); - $local_file = $files_view->getLocalFile($filename); - $local_file_md5 = \md5_file( $local_file ); - - foreach( $matches as $ma ) { - $parts = explode( '.v', $ma ); - $version = ( end( $parts ) ); - $key = $version.'#'.$filename; - $versions[$key]['cur'] = 0; - $versions[$key]['version'] = $version; - $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($version); - $versions[$key]['path'] = $filename; - $versions[$key]['preview'] = \OCP\Util::linkToRoute('core_ajax_versions_preview', array('file' => $filename, 'version' => $version)); - $versions[$key]['size'] = $versions_fileview->filesize($filename.'.v'.$version); - - // if file with modified date exists, flag it in array as currently enabled version - ( \md5_file( $ma ) == $local_file_md5 ? $versions[$key]['fileMatch'] = 1 : $versions[$key]['fileMatch'] = 0 ); - - } - - // newest versions first - $versions = array_reverse( $versions ); - - foreach( $versions as $key => $value ) { - // flag the first matched file in array (which will have latest modification date) as current version - if ( $value['fileMatch'] ) { - $value['cur'] = 1; - break; + public static function getVersions($uid, $filename) { + $versions = array(); + // fetch for old versions + $view = new \OC\Files\View('/' . $uid . '/files_versions/'); + $files = $view->getDirectoryContent(dirname($filename)); + + $versionedFile = pathinfo($filename, PATHINFO_BASENAME); + + foreach ($files as $file) { + if ($file['type'] === 'file') { + $pos = strrpos($file['path'], '.v'); + $length = $pos - strlen('files_versions/'); + if (substr($file['path'], strlen('files_versions/'), $length) === $versionedFile) { + $version = substr($file['path'], $pos + 2); + $key = $version . '#' . $filename; + $versions[$key]['cur'] = 0; + $versions[$key]['version'] = $version; + $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($version); + $versions[$key]['path'] = $filename; + $versions[$key]['size'] = $file['size']; } } - - return( $versions ); - - } else { - // if versioning isn't enabled then return an empty array - return( array() ); } + // sort with oldest version first + ksort($versions); + + // return newest versions first + return array_reverse($versions); } /** @@ -366,48 +341,49 @@ class Storage { * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename */ private static function getAllVersions($uid) { - if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) { - $versions_fileview = new \OC\Files\View('/'.$uid.'/files_versions'); - $versionsRoot = $versions_fileview->getLocalFolder(''); + $view = new \OC\Files\View('/' . $uid . '/'); + $versionsPath = 'files_versions/'; + $dirs = array($versionsPath); - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($versionsRoot), - \RecursiveIteratorIterator::CHILD_FIRST - ); + while (!empty($dirs)) { + $dir = array_pop($dirs); + $files = $view->getDirectoryContent($dir); - $versions = array(); - - foreach ($iterator as $path) { - if ( preg_match('/^.+\.v(\d+)$/', $path, $match) ) { - $relpath = substr($path, strlen($versionsRoot)-1); - $versions[$match[1].'#'.$relpath] = array('path' => $relpath, 'timestamp' => $match[1]); + foreach ($files as $file) { + if ($file['type'] === 'dir') { + array_push($dirs, $file['path']); + } else { + $versionsBegin = strrpos($file['path'], '.v'); + $relPathStart = strlen($versionsPath); + $version = substr($file['path'], $versionsBegin + 2); + $relpath = substr($file['path'], $relPathStart, $versionsBegin - $relPathStart); + $key = $version . '#' . $relpath; + $versions[$key] = array('path' => $relpath, 'timestamp' => $version); } } + } - ksort($versions); - - $i = 0; - - $result = array(); + ksort($versions); - foreach( $versions as $key => $value ) { - $i++; - $size = $versions_fileview->filesize($value['path']); - $filename = substr($value['path'], 0, -strlen($value['timestamp'])-2); + $i = 0; - $result['all'][$key]['version'] = $value['timestamp']; - $result['all'][$key]['path'] = $filename; - $result['all'][$key]['size'] = $size; + $result = array(); - $filename = substr($value['path'], 0, -strlen($value['timestamp'])-2); - $result['by_file'][$filename][$key]['version'] = $value['timestamp']; - $result['by_file'][$filename][$key]['path'] = $filename; - $result['by_file'][$filename][$key]['size'] = $size; + foreach ($versions as $key => $value) { + $i++; + $size = $view->filesize($value['path']); + $filename = $value['path']; - } + $result['all'][$key]['version'] = $value['timestamp']; + $result['all'][$key]['path'] = $filename; + $result['all'][$key]['size'] = $size; - return $result; + $result['by_file'][$filename][$key]['version'] = $value['timestamp']; + $result['by_file'][$filename][$key]['path'] = $filename; + $result['by_file'][$filename][$key]['size'] = $size; } + + return $result; } /** |