diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-03-26 19:55:13 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-03-26 19:55:13 +0100 |
commit | c8c722bc6de3a58e10ba42a55a178d3ba9308bae (patch) | |
tree | 6d62aa73cc72596b1bf385cca20e4d8bd9563525 /apps/files_versions | |
parent | 3e57e9d3e557100ba0b51f08a3de7a7e8f79f4bd (diff) | |
parent | 6447962f2a3bd845be9ee494f400958371f6e2f9 (diff) | |
download | nextcloud-server-c8c722bc6de3a58e10ba42a55a178d3ba9308bae.tar.gz nextcloud-server-c8c722bc6de3a58e10ba42a55a178d3ba9308bae.zip |
Merge pull request #15129 from owncloud/version-command-bus
expire versions in a background command
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/command/expire.php | 58 | ||||
-rw-r--r-- | apps/files_versions/lib/storage.php | 27 | ||||
-rw-r--r-- | apps/files_versions/tests/versions.php | 13 |
3 files changed, 92 insertions, 6 deletions
diff --git a/apps/files_versions/command/expire.php b/apps/files_versions/command/expire.php new file mode 100644 index 00000000000..d6523746525 --- /dev/null +++ b/apps/files_versions/command/expire.php @@ -0,0 +1,58 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_Versions\Command; + +use OC\Command\FileAccess; +use OCA\Files_Versions\Storage; +use OCP\Command\ICommand; +use OCP\IUser; + +class Expire implements ICommand { + use FileAccess; + + /** + * @var string + */ + private $fileName; + + /** + * @var int|null + */ + private $versionsSize; + + /** + * @var int + */ + private $neededSpace = 0; + + /** + * @var string + */ + private $user; + + /** + * @param string $user + * @param string $fileName + * @param int|null $versionsSize + * @param int $neededSpace + */ + function __construct($user, $fileName, $versionsSize = null, $neededSpace = 0) { + $this->user = $user; + $this->fileName = $fileName; + $this->versionsSize = $versionsSize; + $this->neededSpace = $neededSpace; + } + + + public function handle() { + \OC_Util::setupFS($this->user); + Storage::expire($this->fileName, $this->versionsSize, $this->neededSpace); + \OC_Util::tearDownFS(); + } +} diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php index fa8fe9ec4bd..01a7935c0c5 100644 --- a/apps/files_versions/lib/storage.php +++ b/apps/files_versions/lib/storage.php @@ -39,6 +39,8 @@ namespace OCA\Files_Versions; +use OCA\Files_Versions\Command\Expire; + class Storage { const DEFAULTENABLED=true; @@ -156,7 +158,7 @@ class Storage { // 1.5 times as large as the current version -> 2.5 $neededSpace = $files_view->filesize($filename) * 2.5; - self::expire($filename, $versionsSize, $neededSpace); + self::scheduleExpire($filename, $versionsSize, $neededSpace); // disable proxy to prevent multiple fopen calls $proxyStatus = \OC_FileProxy::$enabled; @@ -261,7 +263,7 @@ class Storage { } if (!$files_view->is_dir($newpath)) { - self::expire($newpath); + self::scheduleExpire($newpath); } } @@ -296,7 +298,7 @@ class Storage { // rollback if( @$users_view->rename('files_versions'.$filename.'.v'.$revision, 'files'.$filename) ) { $files_view->touch($file, $revision); - Storage::expire($file); + Storage::scheduleExpire($file); return true; }else if ( $versionCreated ) { @@ -500,9 +502,24 @@ class Storage { } /** - * Erase a file's versions which exceed the set quota + * @param string $fileName + * @param int|null $versionsSize + * @param int $neededSpace + */ + private static function scheduleExpire($fileName, $versionsSize = null, $neededSpace = 0) { + $command = new Expire(\OC::$server->getUserSession()->getUser()->getUID(), $fileName, $versionsSize, $neededSpace); + \OC::$server->getCommandBus()->push($command); + } + + /** + * Expire versions which exceed the quota + * + * @param $filename + * @param int|null $versionsSize + * @param int $offset + * @return bool|int|null */ - private static function expire($filename, $versionsSize = null, $offset = 0) { + public static function expire($filename, $versionsSize = null, $offset = 0) { $config = \OC::$server->getConfig(); if($config->getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { list($uid, $filename) = self::getUidAndFilename($filename); diff --git a/apps/files_versions/tests/versions.php b/apps/files_versions/tests/versions.php index 636a0eec984..6852f769a3d 100644 --- a/apps/files_versions/tests/versions.php +++ b/apps/files_versions/tests/versions.php @@ -246,6 +246,8 @@ class Test_Files_Versioning extends \Test\TestCase { // execute rename hook of versions app \OC\Files\Filesystem::rename("test.txt", "test2.txt"); + $this->runCommands(); + $this->assertFalse($this->rootView->file_exists($v1)); $this->assertFalse($this->rootView->file_exists($v2)); @@ -288,8 +290,11 @@ class Test_Files_Versioning extends \Test\TestCase { // execute rename hook of versions app \OC\Files\Filesystem::rename('/folder1/test.txt', '/folder1/folder2/test.txt'); + self::loginHelper(self::TEST_VERSIONS_USER2); + $this->runCommands(); + $this->assertFalse($this->rootView->file_exists($v1)); $this->assertFalse($this->rootView->file_exists($v2)); @@ -333,6 +338,8 @@ class Test_Files_Versioning extends \Test\TestCase { self::loginHelper(self::TEST_VERSIONS_USER); + $this->runCommands(); + $this->assertTrue($this->rootView->file_exists($v1)); $this->assertTrue($this->rootView->file_exists($v2)); @@ -364,6 +371,8 @@ class Test_Files_Versioning extends \Test\TestCase { // execute copy hook of versions app \OC\Files\Filesystem::copy("test.txt", "test2.txt"); + $this->runCommands(); + $this->assertTrue($this->rootView->file_exists($v1)); $this->assertTrue($this->rootView->file_exists($v2)); @@ -417,7 +426,9 @@ class Test_Files_Versioning extends \Test\TestCase { public static function loginHelper($user, $create = false) { if ($create) { - \OC_User::createUser($user, $user); + $backend = new \OC_User_Dummy(); + $backend->createUser($user, $user); + \OC::$server->getUserManager()->registerBackend($backend); } \OC_Util::tearDownFS(); |