From c80522ed63e3225ce1f1353581220c3296ed0590 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 2 Mar 2015 15:25:50 +0100 Subject: [PATCH] Expire files from the trash in the background --- apps/files_trashbin/command/expire.php | 55 ++++++++++++++++++++++++++ apps/files_trashbin/lib/trashbin.php | 33 +++++++++------- apps/files_trashbin/tests/trashbin.php | 2 + 3 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 apps/files_trashbin/command/expire.php diff --git a/apps/files_trashbin/command/expire.php b/apps/files_trashbin/command/expire.php new file mode 100644 index 00000000000..842e061eeb9 --- /dev/null +++ b/apps/files_trashbin/command/expire.php @@ -0,0 +1,55 @@ +. + * + */ + +namespace OCA\Files_Trashbin\Command; + +use OC\Command\FileAccess; +use OCA\Files_Trashbin\Trashbin; +use OCP\Command\ICommand; + +class Expire implements ICommand { + use FileAccess; + + /** + * @var string + */ + private $user; + + /** + * @var int + */ + private $trashBinSize; + + /** + * @param string $user + * @param int $trashBinSize + */ + function __construct($user, $trashBinSize) { + $this->user = $user; + $this->trashBinSize = $trashBinSize; + } + + public function handle() { + \OC_Util::setupFS($this->user); + Trashbin::expire($this->trashBinSize, $this->user); + } +} diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 8ce6d668d66..76395cc95d0 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -23,6 +23,7 @@ namespace OCA\Files_Trashbin; use OC\Files\Filesystem; +use OCA\Files_Trashbin\Command\Expire; class Trashbin { // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) @@ -204,13 +205,13 @@ class Trashbin { } $userTrashSize += $size; - $userTrashSize -= self::expire($userTrashSize, $user); + self::scheduleExpire($userTrashSize, $user); // if owner !== user we also need to update the owners trash size if ($owner !== $user) { $ownerTrashSize = self::getTrashbinSize($owner); $ownerTrashSize += $size; - $ownerTrashSize -= self::expire($ownerTrashSize, $owner); + self::scheduleExpire($ownerTrashSize, $owner); } return ($sizeOfAddedFiles === false) ? false : true; @@ -682,26 +683,18 @@ class Trashbin { $freeSpace = self::calculateFreeSpace($size, $user); if ($freeSpace < 0) { - self::expire($size, $user); + self::scheduleExpire($size, $user); } } /** * clean up the trash bin * - * @param int $trashbinSize current size of the trash bin + * @param int $trashBinSize current size of the trash bin * @param string $user - * @return int size of expired files */ - private static function expire($trashbinSize, $user) { - - // let the admin disable auto expire - $autoExpire = \OC_Config::getValue('trashbin_auto_expire', true); - if ($autoExpire === false) { - return 0; - } - - $availableSpace = self::calculateFreeSpace($trashbinSize, $user); + public static function expire($trashBinSize, $user) { + $availableSpace = self::calculateFreeSpace($trashBinSize, $user); $size = 0; $retention_obligation = \OC_Config::getValue('trashbin_retention_obligation', self::DEFAULT_RETENTION_OBLIGATION); @@ -718,8 +711,18 @@ class Trashbin { // delete files from trash until we meet the trash bin size limit again $size += self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace); + } - return $size; + /**@param int $trashBinSize current size of the trash bin + * @param string $user + */ + private static function scheduleExpire($trashBinSize, $user) { + // let the admin disable auto expire + $autoExpire = \OC_Config::getValue('trashbin_auto_expire', true); + if ($autoExpire === false) { + return; + } + \OC::$server->getCommandBus()->push(new Expire($user, $trashBinSize)); } /** diff --git a/apps/files_trashbin/tests/trashbin.php b/apps/files_trashbin/tests/trashbin.php index 17e38015868..8bc52cc9192 100644 --- a/apps/files_trashbin/tests/trashbin.php +++ b/apps/files_trashbin/tests/trashbin.php @@ -210,6 +210,8 @@ class Test_Trashbin extends \Test\TestCase { \OC\Files\Filesystem::unlink($folder . 'user1-4.txt'); + $this->runCommands(); + $filesInTrashUser2AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2); // user2-1.txt should have been expired -- 2.39.5