Merge pull request #16322 from owncloud/trash-view

dont go trough the view when moving to trash
This commit is contained in:
Morris Jobke 2015-05-20 14:44:01 +02:00
commit 39d1e99228
5 changed files with 125 additions and 31 deletions

View File

@ -37,6 +37,7 @@
namespace OCA\Files_Trashbin; namespace OCA\Files_Trashbin;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\View;
use OCA\Files_Trashbin\Command\Expire; use OCA\Files_Trashbin\Command\Expire;
class Trashbin { class Trashbin {
@ -124,6 +125,7 @@ class Trashbin {
/** /**
* copy file to owners trash * copy file to owners trash
*
* @param string $sourcePath * @param string $sourcePath
* @param string $owner * @param string $owner
* @param string $ownerPath * @param string $ownerPath
@ -184,25 +186,32 @@ class Trashbin {
// disable proxy to prevent recursive calls // disable proxy to prevent recursive calls
$trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp;
/** @var \OC\Files\Storage\Storage $trashStorage */
list($trashStorage, $trashInternalPath) = $view->resolvePath($trashPath);
/** @var \OC\Files\Storage\Storage $sourceStorage */
list($sourceStorage, $sourceInternalPath) = $view->resolvePath('/files/' . $file_path);
try { try {
$sizeOfAddedFiles = $view->filesize('/files/' . $file_path); $sizeOfAddedFiles = $sourceStorage->filesize($sourceInternalPath);
if ($view->file_exists($trashPath)) { if ($trashStorage->file_exists($trashInternalPath)) {
$view->unlink($trashPath); $trashStorage->unlink($trashInternalPath);
} }
$view->rename('/files/' . $file_path, $trashPath); $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
} catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) { } catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) {
$sizeOfAddedFiles = false; $sizeOfAddedFiles = false;
if ($view->file_exists($trashPath)) { if ($trashStorage->file_exists($trashInternalPath)) {
$view->deleteAll($trashPath); $trashStorage->unlink($trashInternalPath);
} }
\OC_Log::write('files_trashbin', 'Couldn\'t move ' . $file_path . ' to the trash bin', \OC_log::ERROR); \OC_Log::write('files_trashbin', 'Couldn\'t move ' . $file_path . ' to the trash bin', \OC_log::ERROR);
} }
if ($view->file_exists('/files/' . $file_path)) { // failed to delete the original file, abort if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort
$view->unlink($trashPath); $sourceStorage->unlink($sourceInternalPath);
return false; return false;
} }
$view->getUpdater()->rename('/files/' . $file_path, $trashPath);
if ($sizeOfAddedFiles !== false) { if ($sizeOfAddedFiles !== false) {
$size = $sizeOfAddedFiles; $size = $sizeOfAddedFiles;
$query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)");
@ -261,14 +270,15 @@ class Trashbin {
if ($owner !== $user) { if ($owner !== $user) {
self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView); self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView);
} }
$rootView->rename($owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp); self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp);
} else if ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) { } else if ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) {
foreach ($versions as $v) { foreach ($versions as $v) {
$size += $rootView->filesize($owner . '/files_versions' . $v['path'] . '.v' . $v['version']); $size += $rootView->filesize($owner . '/files_versions/' . $v['path'] . '.v' . $v['version']);
if ($owner !== $user) { if ($owner !== $user) {
$rootView->copy($owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $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);
} }
$rootView->rename($owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $filename . '.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);
} }
} }
} }
@ -276,6 +286,50 @@ class Trashbin {
return $size; 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 * Restore a file or folder from trash bin
* *
@ -569,6 +623,7 @@ class Trashbin {
/** /**
* resize trash bin if necessary after a new file was added to ownCloud * resize trash bin if necessary after a new file was added to ownCloud
*
* @param string $user user id * @param string $user user id
*/ */
public static function resizeTrash($user) { public static function resizeTrash($user) {
@ -623,6 +678,7 @@ class Trashbin {
/** /**
* if the size limit for the trash bin is reached, we delete the oldest * 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 * files in the trash bin until we meet the limit again
*
* @param array $files * @param array $files
* @param string $user * @param string $user
* @param int $availableSpace available disc space * @param int $availableSpace available disc space
@ -788,6 +844,7 @@ class Trashbin {
/** /**
* get the size from a given root folder * get the size from a given root folder
*
* @param \OC\Files\View $view file view on the root folder * @param \OC\Files\View $view file view on the root folder
* @return integer size of the folder * @return integer size of the folder
*/ */
@ -845,6 +902,7 @@ class Trashbin {
/** /**
* check if trash bin is empty for a given user * check if trash bin is empty for a given user
*
* @param string $user * @param string $user
* @return bool * @return bool
*/ */

View File

@ -316,9 +316,15 @@ class Storage extends \Test\TestCase {
*/ */
$storage = $this->getMockBuilder('\OC\Files\Storage\Temporary') $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
->setConstructorArgs([[]]) ->setConstructorArgs([[]])
->setMethods(['rename', 'unlink']) ->setMethods(['rename', 'unlink', 'moveFromStorage'])
->getMock(); ->getMock();
$storage->expects($this->any())
->method('rename')
->will($this->returnValue(false));
$storage->expects($this->any())
->method('moveFromStorage')
->will($this->returnValue(false));
$storage->expects($this->any()) $storage->expects($this->any())
->method('unlink') ->method('unlink')
->will($this->returnValue(false)); ->will($this->returnValue(false));

View File

@ -525,6 +525,7 @@ abstract class Common implements Storage {
public function getMountOption($name, $default = null) { public function getMountOption($name, $default = null) {
return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
} }
/** /**
* @param \OCP\Files\Storage $sourceStorage * @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath * @param string $sourceInternalPath
@ -533,6 +534,10 @@ abstract class Common implements Storage {
* @return bool * @return bool
*/ */
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $targetInternalPath);
}
if ($sourceStorage->is_dir($sourceInternalPath)) { if ($sourceStorage->is_dir($sourceInternalPath)) {
$dh = $sourceStorage->opendir($sourceInternalPath); $dh = $sourceStorage->opendir($sourceInternalPath);
$result = $this->mkdir($targetInternalPath); $result = $this->mkdir($targetInternalPath);
@ -575,6 +580,10 @@ abstract class Common implements Storage {
* @return bool * @return bool
*/ */
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
$result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
if ($result) { if ($result) {
if ($sourceStorage->is_dir($sourceInternalPath)) { if ($sourceStorage->is_dir($sourceInternalPath)) {

View File

@ -513,6 +513,10 @@ class Wrapper implements \OC\Files\Storage\Storage {
* @return bool * @return bool
*/ */
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $targetInternalPath);
}
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} }
@ -523,6 +527,10 @@ class Wrapper implements \OC\Files\Storage\Storage {
* @return bool * @return bool
*/ */
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} }

View File

@ -535,4 +535,17 @@ abstract class Storage extends \Test\TestCase {
$this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance))); $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
$this->assertFalse($this->instance->instanceOfStorage('\OC')); $this->assertFalse($this->instance->instanceOfStorage('\OC'));
} }
/**
* @dataProvider copyAndMoveProvider
*/
public function testCopyFromSameStorage($source, $target) {
$this->initSourceAndTarget($source);
$this->instance->copyFromStorage($this->instance, $source, $target);
$this->assertTrue($this->instance->file_exists($target), $target . ' was not created');
$this->assertSameAsLorem($target);
$this->assertTrue($this->instance->file_exists($source), $source . ' was deleted');
}
} }