Skip to content

Commit

Permalink
Merge pull request nextcloud#59 from mwinkens/free-quota
Browse files Browse the repository at this point in the history
Add free quota CLI
  • Loading branch information
icewind1991 authored Dec 9, 2024
2 parents cca9a0d + 998ce10 commit d2cbb47
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ Note: configuring quota is only possible trough the API, no admin interface is c
<command>OCA\GroupQuota\Command\DeleteQuota</command>
<command>OCA\GroupQuota\Command\GetUsed</command>
<command>OCA\GroupQuota\Command\QuotaList</command>
<command>OCA\GroupQuota\Command\GetFree</command>
</commands>
</info>
79 changes: 79 additions & 0 deletions lib/Command/GetFree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @copyright Copyright (c) 2019 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupQuota\Command;

use OC\Core\Command\Base;
use OCA\GroupQuota\Quota\QuotaManager;
use OCA\GroupQuota\Quota\UsedSpaceCalculator;
use OCP\Files\FileInfo;
use OCP\IGroupManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GetFree extends Base {
private $quotaManager;
private $usedSpaceCalculator;
private $groupManager;

public function __construct(
IGroupManager $groupManager,
QuotaManager $quotaManager,
UsedSpaceCalculator $usedSpaceCalculator
) {
parent::__construct();
$this->groupManager = $groupManager;
$this->quotaManager = $quotaManager;
$this->usedSpaceCalculator = $usedSpaceCalculator;
}

protected function configure() {
$this
->setName('groupquota:free')
->setDescription('Get the free space for a group')
->addArgument('name', InputArgument::REQUIRED, 'Name of the group')
->addOption('format', 'f', InputOption::VALUE_NONE, 'Format the quota to be "human readable"');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$groupId = $input->getArgument('name');
$group = $this->groupManager->get($groupId);
if (!$group) {
$output->writeln("<error>Group not found: $groupId</error>");
return -1;
}

$total_quota = $this->quotaManager->getGroupQuota($groupId);
if ($total_quota === FileInfo::SPACE_UNLIMITED) {
$output->writeln($input->getOption('format') ? 'Unlimited' : FileInfo::SPACE_UNLIMITED);
} else {
$used = $this->usedSpaceCalculator->getUsedSpaceByGroup($group);
$free = $total_quota - $used;
$output->writeln($input->getOption('format') ? \OC_Helper::humanFileSize($free) : $free);
}


return 0;
}
}

0 comments on commit d2cbb47

Please sign in to comment.