don't use glob() and getLocalFile(), this can lead to problems on windows servers

This commit is contained in:
Bjoern Schiessle 2013-10-10 14:43:40 +02:00
parent 666e52a46b
commit e56947255e
1 changed files with 61 additions and 85 deletions

View File

@ -238,60 +238,35 @@ class Storage {
* @param $filename file to find versions of, relative to the user files dir * @param $filename file to find versions of, relative to the user files dir
* @returns array * @returns array
*/ */
public static function getVersions($uid, $filename ) { 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(); $versions = array();
// fetch for old versions // fetch for old versions
$matches = glob($escapedVersionName.'*'); $view = new \OC\Files\View('/' . $uid . '/files_versions/');
$files = $view->getDirectoryContent(dirname($filename));
if ( !$matches ) { $versionedFile = pathinfo($filename, PATHINFO_BASENAME);
return $versions;
}
sort( $matches ); foreach ($files as $file) {
if ($file['type'] === 'file') {
$files_view = new \OC\Files\View('/'.$uid.'/files'); $pos = strrpos($file['path'], '.v');
$local_file = $files_view->getLocalFile($filename); $length = $pos - strlen('files_versions/');
$local_file_md5 = \md5_file( $local_file ); if (substr($file['path'], strlen('files_versions/'), $length) === $versionedFile) {
$version = substr($file['path'], $pos + 2);
foreach( $matches as $ma ) { $key = $version . '#' . $filename;
$parts = explode( '.v', $ma );
$version = ( end( $parts ) );
$key = $version.'#'.$filename;
$versions[$key]['cur'] = 0; $versions[$key]['cur'] = 0;
$versions[$key]['version'] = $version; $versions[$key]['version'] = $version;
$versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($version); $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($version);
$versions[$key]['path'] = $filename; $versions[$key]['path'] = $filename;
$versions[$key]['preview'] = \OCP\Util::linkToRoute('core_ajax_versions_preview', array('file' => $filename, 'version' => $version)); $versions[$key]['size'] = $file['size'];
$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;
} }
} }
return( $versions ); // sort with oldest version first
ksort($versions);
} else {
// if versioning isn't enabled then return an empty array
return( array() );
}
// return newest versions first
return array_reverse($versions);
} }
/** /**
@ -366,21 +341,25 @@ 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 * @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) { private static function getAllVersions($uid) {
if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) { $view = new \OC\Files\View('/' . $uid . '/');
$versions_fileview = new \OC\Files\View('/'.$uid.'/files_versions'); $versionsPath = 'files_versions/';
$versionsRoot = $versions_fileview->getLocalFolder(''); $dirs = array($versionsPath);
$iterator = new \RecursiveIteratorIterator( while (!empty($dirs)) {
new \RecursiveDirectoryIterator($versionsRoot), $dir = array_pop($dirs);
\RecursiveIteratorIterator::CHILD_FIRST $files = $view->getDirectoryContent($dir);
);
$versions = array(); foreach ($files as $file) {
if ($file['type'] === 'dir') {
foreach ($iterator as $path) { array_push($dirs, $file['path']);
if ( preg_match('/^.+\.v(\d+)$/', $path, $match) ) { } else {
$relpath = substr($path, strlen($versionsRoot)-1); $versionsBegin = strrpos($file['path'], '.v');
$versions[$match[1].'#'.$relpath] = array('path' => $relpath, 'timestamp' => $match[1]); $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);
}
} }
} }
@ -390,25 +369,22 @@ class Storage {
$result = array(); $result = array();
foreach( $versions as $key => $value ) { foreach ($versions as $key => $value) {
$i++; $i++;
$size = $versions_fileview->filesize($value['path']); $size = $view->filesize($value['path']);
$filename = substr($value['path'], 0, -strlen($value['timestamp'])-2); $filename = $value['path'];
$result['all'][$key]['version'] = $value['timestamp']; $result['all'][$key]['version'] = $value['timestamp'];
$result['all'][$key]['path'] = $filename; $result['all'][$key]['path'] = $filename;
$result['all'][$key]['size'] = $size; $result['all'][$key]['size'] = $size;
$filename = substr($value['path'], 0, -strlen($value['timestamp'])-2);
$result['by_file'][$filename][$key]['version'] = $value['timestamp']; $result['by_file'][$filename][$key]['version'] = $value['timestamp'];
$result['by_file'][$filename][$key]['path'] = $filename; $result['by_file'][$filename][$key]['path'] = $filename;
$result['by_file'][$filename][$key]['size'] = $size; $result['by_file'][$filename][$key]['size'] = $size;
} }
return $result; return $result;
} }
}
/** /**
* @brief Erase a file's versions which exceed the set quota * @brief Erase a file's versions which exceed the set quota