Get.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\FilesMetadata;
  25. use OC\User\NoUserException;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\NotPermittedException;
  29. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  30. use OCP\FilesMetadata\IFilesMetadataManager;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class Get extends Command {
  37. public function __construct(
  38. private IRootFolder $rootFolder,
  39. private IFilesMetadataManager $filesMetadataManager,
  40. ) {
  41. parent::__construct();
  42. }
  43. protected function configure(): void {
  44. $this->setName('metadata:get')
  45. ->setDescription('get stored metadata about a file, by its id')
  46. ->addArgument(
  47. 'fileId',
  48. InputArgument::REQUIRED,
  49. 'id of the file document'
  50. )
  51. ->addArgument(
  52. 'userId',
  53. InputArgument::OPTIONAL,
  54. 'file owner'
  55. )
  56. ->addOption(
  57. 'as-array',
  58. '',
  59. InputOption::VALUE_NONE,
  60. 'display metadata as a simple key=>value array'
  61. )
  62. ->addOption(
  63. 'refresh',
  64. '',
  65. InputOption::VALUE_NONE,
  66. 'refresh metadata'
  67. )
  68. ->addOption(
  69. 'reset',
  70. '',
  71. InputOption::VALUE_NONE,
  72. 'refresh metadata from scratch'
  73. );
  74. }
  75. /**
  76. * @throws NotPermittedException
  77. * @throws FilesMetadataNotFoundException
  78. * @throws NoUserException
  79. * @throws NotFoundException
  80. */
  81. protected function execute(InputInterface $input, OutputInterface $output): int {
  82. $fileId = (int)$input->getArgument('fileId');
  83. if ($input->getOption('reset')) {
  84. $this->filesMetadataManager->deleteMetadata($fileId);
  85. if (!$input->getOption('refresh')) {
  86. return self::SUCCESS;
  87. }
  88. }
  89. if ($input->getOption('refresh')) {
  90. $node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getById($fileId);
  91. if (count($node) === 0) {
  92. throw new NotFoundException();
  93. }
  94. $metadata = $this->filesMetadataManager->refreshMetadata(
  95. $node[0],
  96. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  97. );
  98. } else {
  99. $metadata = $this->filesMetadataManager->getMetadata($fileId);
  100. }
  101. if ($input->getOption('as-array')) {
  102. $output->writeln(json_encode($metadata->asArray(), JSON_PRETTY_PRINT));
  103. } else {
  104. $output->writeln(json_encode($metadata, JSON_PRETTY_PRINT));
  105. }
  106. return self::SUCCESS;
  107. }
  108. }