cleanup move/copy logic in trash

This commit is contained in:
Robin Appelman 2015-05-19 14:22:09 +02:00
parent 9c751f1d87
commit 733784ae41
1 changed files with 57 additions and 20 deletions

View File

@ -37,6 +37,7 @@
namespace OCA\Files_Trashbin;
use OC\Files\Filesystem;
use OC\Files\View;
use OCA\Files_Trashbin\Command\Expire;
class Trashbin {
@ -124,6 +125,7 @@ class Trashbin {
/**
* copy file to owners trash
*
* @param string $sourcePath
* @param string $owner
* @param string $ownerPath
@ -264,32 +266,19 @@ class Trashbin {
}
if ($rootView->is_dir($owner . '/files_versions/' . $ownerPath)) {
/** @var \OC\Files\Storage\Storage $versionStorage */
list($versionStorage, $versionsInternalPath) = $rootView->resolvePath($owner . '/files_versions/' . $ownerPath);
/** @var \OC\Files\Storage\Storage $trashStorage */
list($trashStorage, $trashInternalPath) = $rootView->resolvePath($user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp);
$size += self::calculateSize(new \OC\Files\View('/' . $owner . '/files_versions/' . $ownerPath));
if ($owner !== $user) {
self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView);
}
$trashStorage->moveFromStorage($versionStorage, $versionsInternalPath, $trashInternalPath);
self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp);
} else if ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) {
/** @var \OC\Files\Storage\Storage $versionStorage */
list($versionStorage, $versionsInternalPath) = $rootView->resolvePath($owner . '/files_versions/');
/** @var \OC\Files\Storage\Storage $trashStorage */
list($trashStorage, $trashInternalPath) = $rootView->resolvePath($user . '/files_trashbin/versions/');
/** @var \OC\Files\Storage\Storage $ownerTrashStorage */
list($ownerTrashStorage, $ownerTrashInternalPath) = $rootView->resolvePath($owner . '/files_trashbin/versions/');
foreach ($versions as $v) {
$size += $versionStorage->filesize($versionsInternalPath . $v['path'] . '.v' . $v['version']);
$size += $rootView->filesize($owner . '/files_versions/' . $v['path'] . '.v' . $v['version']);
if ($owner !== $user) {
$ownerTrashStorage->copyFromStorage($versionStorage, $versionsInternalPath . $v['path'] . '.v' . $v['version'], $ownerTrashInternalPath . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp);
$rootView->getUpdater()->update($owner . '/files_trashbin/versions/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp);
self::copy($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $owner . '/files_trashbin/versions/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp);
}
$trashStorage->moveFromStorage($versionStorage, $versionsInternalPath . $v['path'] . '.v' . $v['version'], $trashInternalPath . '/' . $filename . '.v' . $v['version'] . '.d' . $timestamp);
$rootView->getUpdater()->rename($owner . '/files_versions/' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp);
self::move($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $filename . '.v' . $v['version'] . '.d' . $timestamp);
}
}
}
@ -297,6 +286,50 @@ class Trashbin {
return $size;
}
/**
* Move a file or folder on storage level
*
* @param View $view
* @param string $source
* @param string $target
* @return bool
*/
private static function move(View $view, $source, $target) {
/** @var \OC\Files\Storage\Storage $sourceStorage */
list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source);
/** @var \OC\Files\Storage\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $view->resolvePath($target);
/** @var \OC\Files\Storage\Storage $ownerTrashStorage */
$result = $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
if ($result) {
$view->getUpdater()->rename($source, $target);
}
return $result;
}
/**
* Copy a file or folder on storage level
*
* @param View $view
* @param string $source
* @param string $target
* @return bool
*/
private static function copy(View $view, $source, $target) {
/** @var \OC\Files\Storage\Storage $sourceStorage */
list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source);
/** @var \OC\Files\Storage\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $view->resolvePath($target);
/** @var \OC\Files\Storage\Storage $ownerTrashStorage */
$result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
if ($result) {
$view->getUpdater()->update($target);
}
return $result;
}
/**
* Restore a file or folder from trash bin
*
@ -320,7 +353,7 @@ class Trashbin {
// if location no longer exists, restore file in the root directory
if ($location !== '/' &&
(!$view->is_dir('files/' . $location) ||
!$view->isCreatable('files/' . $location))
!$view->isCreatable('files/' . $location))
) {
$location = '';
}
@ -590,6 +623,7 @@ class Trashbin {
/**
* resize trash bin if necessary after a new file was added to ownCloud
*
* @param string $user user id
*/
public static function resizeTrash($user) {
@ -644,6 +678,7 @@ class Trashbin {
/**
* if the size limit for the trash bin is reached, we delete the oldest
* files in the trash bin until we meet the limit again
*
* @param array $files
* @param string $user
* @param int $availableSpace available disc space
@ -746,7 +781,7 @@ class Trashbin {
//force rescan of versions, local storage may not have updated the cache
if (!self::$scannedVersions) {
/** @var \OC\Files\Storage\Storage $storage */
list($storage, ) = $view->resolvePath('/');
list($storage,) = $view->resolvePath('/');
$storage->getScanner()->scan('files_trashbin/versions');
self::$scannedVersions = true;
}
@ -809,6 +844,7 @@ class Trashbin {
/**
* get the size from a given root folder
*
* @param \OC\Files\View $view file view on the root folder
* @return integer size of the folder
*/
@ -820,7 +856,7 @@ class Trashbin {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST);
$size = 0;
/**
/**
* RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
* This bug is fixed in PHP 5.5.9 or before
* See #8376
@ -866,6 +902,7 @@ class Trashbin {
/**
* check if trash bin is empty for a given user
*
* @param string $user
* @return bool
*/