-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30095 from owncloud/fix-27412
Fix deleted items auto expiration for users with no quota
- Loading branch information
Showing
5 changed files
with
194 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* @author Viktar Dubiniuk <dubiniuk@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\Files_Trashbin; | ||
|
||
use OC\Files\Filesystem; | ||
use OCP\Files\FileInfo; | ||
use OCP\IConfig; | ||
use OCP\IUserManager; | ||
use OCP\IUser; | ||
|
||
class Quota { | ||
|
||
// percent of free disk space/quota that triggers trashbin cleanup by default | ||
const DEFAULTMAXSIZE = 50; | ||
|
||
/** @var IUserManager */ | ||
protected $userManager; | ||
|
||
/** @var IConfig */ | ||
protected $config; | ||
|
||
public function __construct(IUserManager $userManager, IConfig $config){ | ||
$this->userManager = $userManager; | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Calculate remaining free space for trash bin | ||
* | ||
* @param integer $trashbinSize current size of the trash bin | ||
* @param string $user | ||
* @return int available free space for trash bin | ||
*/ | ||
public function calculateFreeSpace($trashbinSize, $user) { | ||
$userObject = $this->userManager->get($user); | ||
if(is_null($userObject)) { | ||
return 0; | ||
} | ||
$quota = $this->getUserQuota($userObject); | ||
|
||
$userFolder = \OC::$server->getUserFolder($user); | ||
if(is_null($userFolder)) { | ||
return 0; | ||
} | ||
|
||
$free = $quota - $userFolder->getSize(); // remaining free space for user | ||
if ($free > 0) { | ||
// does trashbin size hit purge limit with the current free space | ||
$availableSpace = ($free * $this->getPurgeLimit() / 100) - $trashbinSize; | ||
} else { | ||
$availableSpace = $free - $trashbinSize; | ||
} | ||
|
||
return $availableSpace; | ||
} | ||
|
||
/** | ||
* Get a percentage of free space that should trigger | ||
* cleanup for outdated files in trashbin | ||
* | ||
* @return int | ||
*/ | ||
public function getPurgeLimit(){ | ||
return $this->config->getSystemValue('trashbin_purge_limit', self::DEFAULTMAXSIZE); | ||
} | ||
|
||
|
||
/** | ||
* Get user quota or free space when there is no quota set | ||
* | ||
* @param IUser $user | ||
* @return int|mixed | ||
*/ | ||
protected function getUserQuota(IUser $user) { | ||
$quota = \OC_Util::getUserQuota($user); | ||
if ($quota === FileInfo::SPACE_UNLIMITED) { | ||
$quota = Filesystem::free_space('/'); | ||
// inf or unknown free space | ||
if ($quota < 0) { | ||
$quota = PHP_INT_MAX; | ||
} | ||
} | ||
|
||
return $quota; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* @author Viktar Dubiniuk <dubiniuk@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program 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, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
use OCA\Files_Trashbin\Quota; | ||
use OCP\IConfig; | ||
use OCP\IUserManager; | ||
|
||
class QuotaTest extends \Test\TestCase { | ||
|
||
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $userManager; | ||
|
||
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ | ||
protected $config; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
$this->userManager = $this->getMockBuilder(IUserManager::class) | ||
->getMock(); | ||
$this->config = $this->getMockBuilder(IConfig::class) | ||
->getMock(); | ||
} | ||
|
||
public function testNonExistingUserNoNeedPurge() { | ||
$this->userManager->expects($this->any()) | ||
->method('get') | ||
->willReturn(null); | ||
|
||
$quota = new Quota($this->userManager, $this->config); | ||
$neededSpace = $quota->calculateFreeSpace(100, 'anyuser'); | ||
$this->assertEquals(0, $neededSpace); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters