Expire files from the trash in the background
This commit is contained in:
parent
00568af74d
commit
c80522ed63
3 changed files with 75 additions and 15 deletions
55
apps/files_trashbin/command/expire.php
Normal file
55
apps/files_trashbin/command/expire.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
/**
|
||||
* ownCloud - trash bin
|
||||
*
|
||||
* @author Robin Appelman
|
||||
* @copyright 2015 Robin Appelman icewind@owncloud.com
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue