-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(files): commands to manage system-tags on files
Resolves #32735 by adding new occ commands to manage system-tags for files Signed-off-by: schaarsc <schaarsc@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
228 additions
and
0 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
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,87 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Command; | ||
|
||
use OCP\SystemTag\ISystemTagManager; | ||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use OCP\SystemTag\TagAlreadyExistsException; | ||
use OC\Core\Command\Info\FileUtils; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class TagAdd extends Command | ||
{ | ||
|
||
public function __construct(private FileUtils $fileUtils, private ISystemTagManager $systemTagManager, private ISystemTagObjectMapper $systemTagObjectMapper) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('files:tag-add') | ||
->setDescription('Add a system-tag to a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, "file id or path") | ||
->addArgument('tags', InputArgument::REQUIRED, "Name of the tag(s) to add, comma separated") | ||
->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$targetInput = $input->getArgument('target'); | ||
$tagsInput = $input->getArgument('tags'); | ||
|
||
if ($tagsInput === '') { | ||
$output->writeln('<error>`tags` can\'t be empty</error>'); | ||
return 3; | ||
} | ||
|
||
$tagNameArray = explode(',', $tagsInput); | ||
|
||
$access = $input->getArgument('access'); | ||
switch ($access) { | ||
case 'public': | ||
$userVisible = true; | ||
$userAssignable = true; | ||
break; | ||
case 'restricted': | ||
$userVisible = true; | ||
$userAssignable = false; | ||
break; | ||
case 'invisible': | ||
$userVisible = false; | ||
$userAssignable = false; | ||
break; | ||
default: | ||
$output->writeln('<error>`access` property is invalid</error>'); | ||
return 1; | ||
} | ||
|
||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
foreach ($tagNameArray as $tagName) { | ||
try { | ||
$tag = $this->systemTagManager->createTag($tagName, $userVisible, $userAssignable); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> created."); | ||
} catch (TagAlreadyExistsException $e) { | ||
$tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); | ||
} | ||
|
||
$this->systemTagObjectMapper->assignTags((string) $targetNode->getId(), 'files', $tag->getId()); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> added."); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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,85 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Command; | ||
|
||
use OCP\SystemTag\ISystemTagManager; | ||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use OCP\SystemTag\TagNotFoundException; | ||
use OC\Core\Command\Info\FileUtils; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class TagDelete extends Command | ||
{ | ||
|
||
public function __construct(private FileUtils $fileUtils, private ISystemTagManager $systemTagManager, private ISystemTagObjectMapper $systemTagObjectMapper) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('files:tag-delete') | ||
->setDescription('Delete a system-tag from a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, "file id or path") | ||
->addArgument('tags', InputArgument::REQUIRED, "Name of the tag(s) to delete, comma separated") | ||
->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$targetInput = $input->getArgument('target'); | ||
$tagsInput = $input->getArgument('tags'); | ||
|
||
if ($tagsInput === '') { | ||
$output->writeln('<error>`tags` can\'t be empty</error>'); | ||
return 3; | ||
} | ||
|
||
$tagNameArray = explode(',', $tagsInput); | ||
|
||
$access = $input->getArgument('access'); | ||
switch ($access) { | ||
case 'public': | ||
$userVisible = true; | ||
$userAssignable = true; | ||
break; | ||
case 'restricted': | ||
$userVisible = true; | ||
$userAssignable = false; | ||
break; | ||
case 'invisible': | ||
$userVisible = false; | ||
$userAssignable = false; | ||
break; | ||
default: | ||
$output->writeln('<error>`access` property is invalid</error>'); | ||
return 1; | ||
} | ||
|
||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
foreach ($tagNameArray as $tagName) { | ||
try { | ||
$tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); | ||
$this->systemTagObjectMapper->unassignTags((string) $targetNode->getId(), 'files', $tag->getId()); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> removed."); | ||
} catch (TagNotFoundException $e) { | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> does not exist!"); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Command; | ||
|
||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use OC\Core\Command\Info\FileUtils; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class TagDeleteAll extends Command | ||
{ | ||
|
||
public function __construct(private FileUtils $fileUtils, private ISystemTagObjectMapper $systemTagObjectMapper) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('files:tag-delete-all') | ||
->setDescription('Delete all system-tags from a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, "file id or path"); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$targetInput = $input->getArgument('target'); | ||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
$tags = $this->systemTagObjectMapper->getTagIdsForObjects($targetNode->getId(), 'files'); | ||
Check failure on line 41 in apps/files/lib/Command/TagDeleteAll.php GitHub Actions / static-code-analysisInvalidArgument
Check failure Code scanning / Psalm InvalidArgument Error
Argument 1 of OCP\SystemTag\ISystemTagObjectMapper::getTagIdsForObjects expects array<array-key, mixed>|string, but int provided
|
||
$this->systemTagObjectMapper->unassignTags((string) $targetNode->getId(), 'files', $tags[$targetNode->getId()]); | ||
$output->writeln("<info>all tags removed.</info>"); | ||
|
||
return 0; | ||
} | ||
} |